0

I am setting a cookie in an external JavaScript file with the current user time. I want this time to be available to the server, so I am retrieving this from a cookie.

I am able to fetch the value, but only when I refresh the browser twice. If I change my current system time, and refresh the page, I still see the old time; when I refresh again, then I see the updated time.

JavaScript code

var now=new Date();
currTime=now.getHours();
document.cookie = "currentTime = " +currTime;

PHP code

$current_time = $_COOKIE["currentTime"];
echo $current_time;

I am calling the external JavaScript file in hook_nodeapi() using drupal_add_js().

As I said, I am able to get the updated value of current time from cookie but only if I refresh twice.

What could be the problem?

1

I understood the problem. It is because the first time it sets the cookie, and in the second time it becomes available to PHP; hence two refreshes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.