Tutorial :How to set a cookie in php?



Question:

I set a cookie and then check if exist like this

if(isset($_COOKIE["fan"]))  {  //Do Nothing  }  else  {  $cookie = "yes";  $expire=time()+60*60*24*30;  setcookie("fan", $cookie, $expire);  include_once("../inc/functions.php");  echo fan_page();      }  

When I test on my local machine, it works, but when i upload to production server, it doesn't work.

What am I doing wrong?

Thanks In Advance!

Marc


Solution:1

You probably need to set the domain for the cookie. Locally it defaults, but in production you may come across some issues if it's not set explicitly.

See the arguments for setcookie; http://www.php.net/manual/en/function.setcookie.php

I also suggest looking in your browser cache to see if it is being set.


Solution:2

A cookie set for one path/hostname may override a cookie set for another path/hostname even if it is newer.

For instance, if there is already a cookie set for "www.example.com" and you set one for "example.com", when you read back the same cookie you'll get the one that was set for "www.example.com".

Try setting the cookie for the more specific hostname.

This may be part of the issue.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »