PHP Cookies not working

No Comments

PHP COOKIES

If you have reached here after going through lots of search results then you are welcomed. You may or may not get the exact content you wanted in this page. This page is NOT meant for the PHP COOKIES tutorial. If you are searching tutorial then you are suggested to Google it. It is believed that you know general knowledge about storing and getting cookies values.

PHP Cookie Troubleshoot for “expire” parameter with tutorial. Check PHP setcookie() ‘s “expire” parameter and understand it first. You may face PHP Cookie Problem due to different time zone!  Example is demonstrated with PHP Cookie Script.

This Troubleshoot is believed to help those whose hosting server is in different geographic location than the developer’s local machine.

PHP Cookies not working ?

setcookie

setcookie(name, value, expire, path, domain);

Parameter “expire”

The third parameter expire is compared to the client’s time which can differ from server’s time. You may have understand by here.

Lets suppose server of your site is in US and the visitor from Asia, now if you kept the value of  expire parameter in the following way then you may not get the proper result.

<?php
$cookie_name =“visitor”;
$cookie_value = “cookie is enabled”;
$expire = time() + 3600; // current time of server PLUS one hour (60min x 60sec)

// Assumed: cookie is set to expire after one hour on visitor’s machine
setcookie($cookie_name, $cookie_value, $expire);

echo $_COOKIE["visitor"]; // result is meant to be displayed after refreshing the page
?>

This is because the GMT is more than an hour in Asian Countries compared to that of US.

If you want cookies until the webpage is not closed then you can use the code in the following way.

<?php
$cookie_name =“visitor”;
$cookie_value = “cookie is enabled”;
$expire = 0; //delete cookie after browser is closed

// Assumed: cookie is set to expire after visitor exits from the webpage
setcookie($cookie_name, $cookie_value, $expire);

echo
$_COOKIE["visitor"]; // result is meant to be displayed after refreshing the page
?>

If you want your PHP COOKIE to be deleted on visitor’s machine only after one hour the visitor leaved the page then you need more logic and help of JavaScript.

First way: determine visitor’s time from computer’s time of visitor

if $_GET["visitor_computer_time"] is not set then
echo JavaScript Code,

that writes
<meta http-equiv=”refresh” content=”0″ url=”http://example.com/cookie_check.php?visitor_computer_time” />
tag  on document to refresh and pass visitors computer’s time

use JavaScript to retrieve visitor’s local time  and add it on url attribute of meta tag like
url=”http://example.com/cookie_check.php?visitor_computer_time=”+UTC()
JavaScript functions
valueOf(), UTC(),setDate(),setUTCDate(),getTimezoneOffset()
can be used to determine time

GET[visitor_computer_time] now and compare with server’s time.
Determine difference and set expire on setcookie function

Second way: determine visitor’s time from IP Address of the visitor

GET visitors_IP_address, find Time Zone .
determine time difference by finding  server’s Time Zone and comparing with visitor Time Zone
finally set cookie and keep proper value in expire Parameter.

The first way is suggested to determine visitor’s time than the second way because in the second way you have to subscribe to IP Address Database Info Service Providers through API to get Time Zone of IP Address of visitor which require periodic updates.