Welcome Guest, Not a member yet? Register   Sign In
How to use cookies - sometimes not working
#1

Hello!



I don't know how to describe my problem, because actually everything runs. But not I did the same thing like I did just a few lines later in an other function of my controller, I did not work...



I use the cookie-helper to set and get two cookies to identify the user and the device. In the add-function it works. Both cookies are set and I can confirm on all pages that this user is a regular one. Or if not, it redirects as expected. This is, how I set it:



PHP Code:
set_cookie('device_id'$deviceId31536000); 


The same line, I copyed and pasted minutes ago to be sure, that everything is the same Smile , did not its job if the user is register a new device.



PHP Code:
 set_cookie('app_id'$appId31536000);
 set_cookie('device_id'$deviceId31536000);

 echo get_cookie('device_id') .' / '$deviceId;
 exit


$deviceId is filled, the cookie is not. It's totally the same. I moved this lines to the begin and the end of the register-function so be sure, that there happens nothing between the call of the controller/function and the action to set the cookie.



PHP Code:
public function device($step NULL$code NULL)
{
        $data['step'] = $step;
        $data['code'] = $code;

        set_cookie('app_id'$step31536000);
        set_cookie('device_id'$code31536000);

        echo $code .' # 'get_cookie('device_id') .' / '$step .' # 'get_cookie('app_id');
        exit;
[...] 

And the answer is: 23423 # / check # 



The function a few lines below:



PHP Code:
public function add()
{
        $deviceId generateID();
        set_cookie('device_id'$deviceId31536000);
        echo 'here: 'get_cookie('device_id');
[...] 




And the answere is: here: b737004617ee83fdd8fe62fb059d734259210b3c



I'm confused about this. Same Controller, only different functions. What is going on here? How to use cookie-helper in all functions?



Huh

Reply
#2

Cookies do not update until the next page refresh.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#3

(This post was last modified: 01-21-2022, 01:58 AM by paulbalandan.)

https://forum.codeigniter.com/thread-770...ght=cookie

https://forum.codeigniter.com/thread-770...ght=cookie
Reply
#4

Thanks, that's it, the page has to refresh, what I do mostly like this

PHP Code:
return redirect()->to('/foo')->withCookies(); 

But in my specal case, without "withCookies()". Add this to my redirect did it Smile BTW I had this idea, there must be some action with the browser to set the cookie but reject it...
Reply




Theme © iAndrew 2016 - Forum software by © MyBB