CodeIgniter Forums
How to work with cookie and redirect in codeigniter 4 - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: How to work with cookie and redirect in codeigniter 4 (/showthread.php?tid=80078)



How to work with cookie and redirect in codeigniter 4 - venance - 09-10-2021

Hello everyone. I'm working with codeigniter4 where I want to be able to store cookie and then redirect
to another page. If I set a cookie without including redirection process, Cookie stores successfully, But the issue is when I do redirection, the cookie did't saved anymore.
How can I solve and make cookie saved and redirect

PHP Code:
public function loginProcessor(){
    helper(['cookie']);
    $checkStatus true;

    if ($checkStatus){
        set_cookie('nameCookie''CookieSomething'time() + 60*60*24*30);
    }
    return redirect()->to('dashboard');




RE: How to work with cookie and redirect in codeigniter 4 - InsiteFX - 09-10-2021

Your if statement is always true with checkStatus because you are setting it to true all the time in the method.

It will never reach the else clause.

It would also help if you explain what you want to do with the cookie etc;


RE: How to work with cookie and redirect in codeigniter 4 - venance - 09-10-2021

I have solved my issue by using php function
PHP Code:
setcookie() 
or using
PHP Code:
redirect()->to('urlname')->setCookie("name""value"time());