set_cookie & redirect() didn't work - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28) +--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30) +--- Thread: set_cookie & redirect() didn't work (/showthread.php?tid=73563) |
set_cookie & redirect() didn't work - zjonsnowz - 05-08-2019 Hello, Here is my User controller code. I don't know why the function "set_cookie" didn't work. It redirected to the home page, but it didn't set the cookie in the browser. Please help. PHP Code: <?php namespace App\Controllers; RE: set_cookie & redirect() didn't work - kilishan - 05-09-2019 You should set the cookie on the response object like: Code: $this->response->setCookie(); RE: set_cookie & redirect() didn't work - zjonsnowz - 05-09-2019 Thanks kilishan . I changed to set the cookie on the response object and it still didn't work with the redirect(). I have to set the header on the response object for redirecting So, I changed from: PHP Code: set_cookie('login', md5($this->request->getVar('password')), time() + 86400); To: PHP Code: $this->response->setCookie('login', md5($this->request->getVar('password')), time() + 86400)->setHeader('Location', '/'); Also, $this->response->deleteCookie('login') didn't work so I have to use: PHP Code: $this->response->setCookie('login', '')->setHeader('Location', '/'); Is this the correct way to do like this? |