CodeIgniter Forums

Full Version: input->set_cookie or setcookie not work
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]helloworld7[/eluser]
Hi. I'm having problem with setting cookies using the input set_cookie or the php setcookie function.

It doesn't have any errors on the page but when I print the cookie using input->cookie() or just $_COOKIE then all I can see is just the php session id.

Array ( [PHPSESSID] => spib39ei0c4u7k9mk8ir464uc7 )

I'm added the native session class but it shouldn't affect it?

I don't know how to troubleshooting this. Where should I check? Any help will be appreciated.

Thanks.

El Forum

[eluser]toopay[/eluser]
If you use ci session, that actually means you stored your data into cookies! If you want to store something in cookie, just use direct setcookie()

El Forum

[eluser]helloworld7[/eluser]
What do you mean direct setcookie? The php setcookie? Did you read my message? I already said I did use the php setcookie but not working.

I hope someone can read my message and help me out. Thanks.

El Forum

[eluser]toopay[/eluser]
The cookie should be available, on the next http request, after you did setcookie(). How your code right now, i mean how you check that the cookie is not written?

El Forum

[eluser]helloworld7[/eluser]
I tried two ways.

1) $cookie = array('name' => 'sual', 'value' => 'test', 'expire' => time()+3600,
'domain' => '.somedomain.com', 'path' => '/', 'secure' => TRUE);
$this->input->set_cookie($cookie);
on a controller (A) then redirect to another controller (B)

2) setcookie("TestCookie", 1, time()+3600);
on a controller (A) then redirect to another controller (B)


Then controller (B) just print_r($_COOKIE);

Both ways always just print out only PHPSESSIONID like this.

Array ( [PHPSESSID] => lqcorhnpisl842fk0mrqpkist0 )

And I keep hitting refresh on B and there's only the id.

Should these from config.php matters? I just keep it as the default.

$config['cookie_prefix'] = "";
$config['cookie_domain'] = "";
$config['cookie_path'] = "/";
$config['cookie_secure'] = FALSE;

I don't know what else to check. Hope anyone can help me on this. Thanks.

El Forum

[eluser]toopay[/eluser]
You can do this simple foo bar example. Dont set anything else, like expiration time etc, we can fix that later.
Code:
// In some test controller
public function foo()
{
   setcookie('foo','From foo cookies');
   redirect('controllername/bar');
}
public function bar()
{
   // escape any harmfull script
   $bar = $this->input->cookie('foo',TRUE);
   var_dump($bar);
}

El Forum

[eluser]helloworld7[/eluser]
Hi. Thank you. Almost have this figure it out. I did the test you gave me and it worked. Then I tried to move the bar function to my controller B and didn't get the same result as before but I think I may know what's the issue.

I had controller A as /login then controller B as /myaccount/home (I actually have a /myaccount/ dir).

Is it because it's different path? The cookie is only available only for /login and ./*?

But method 1 I had ‘domain’ => ‘.somedomain.com’ on cookie, is that mean it should work for all path for that domain?

If that's the issue. How do I set my cookie so that I can retrieve it from all controller(even with different directory)?

El Forum

[eluser]toopay[/eluser]
Heres you can set cookie properties...
Code:
// In some test controller
public function foo()
{
   setcookie('foo','From foo cookies', time()+3600, '/', '.yoursite.com', 1);
   redirect('controllername/bar');
}
public function bar()
{
   // escape any harmfull script
   $bar = $this->input->cookie('foo',TRUE);
   var_dump($bar);
}