CodeIgniter Forums

Full Version: set cookie not work before response redirect
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I need to add remember_me key into cookie. but when i put set_cookie before redirect, set_cookie not working and not set data:

PHP Code:
    public function loginPost()
    {

        $rules = [
            'email' => ['label' => lang('Auth.email'), 'rules' => 'required|valid_email'],
            'password' => ['label' => lang('Auth.password'), 'rules' => 'required|min_length[6]']
        ];

        if (! $this->validate($rules))
        {
            return redirect()->back()->withInput()->with('errors'$this->validator->getErrors());
        }

        $data = array(
            'email' => $this->request->getPost('email'),
            'password' => $this->request->getPost('password'),
            'remember_me' => (bool)$this->request->getPost('remember_me'),
        );

        $result $this->authModel->attemptsLogin($data);

        if ($result) {
            $config config('App');
            helper('cookie');
            set_cookie('remember_me',
                'xxx',
                3600,
                $config->cookieDomain,
                $config->cookiePath,
                $config->cookiePrefix,
                $config->cookieSecure,
                $config->cookieHTTPOnly
            
);
            return redirect()->route('dashboard');
        } else {
            return redirect()->route('login');
        }
    


response set cookie not work before response redirect?!

Note: i know this method(put after redirect) working true but this method nor clear code: 

PHP Code:
        return redirect()->route('dashboard')
            ->setCookie('remember_me''xxx'3600);