Jump to content

Cookie And When It Takes Effect


djp1988

Recommended Posts

Hi, I am setting a cookie based on a get variable that determines the language the visitor has chosen, the script works as it should... It would seem that when the cookie is created or when it's value is modified, the first page it loads in doesn't take notice of it's new value, or existence if it had previously been unset, here is the top of my php in the header:

<?php session_start();ob_start();if(isset($_GET['l'])){	$language = $_GET['l'];}else{	$language = 'en';}switch ($language){	case 'en':		$cookie = 'english';		break;	case 'fr':		$cookie = 'french';		break;}if(!isset($_COOKIE['language'])){		setcookie("language", $cookie, time()+31000000, "/");}else{	if($_COOKIE['language'] != $cookie){			setcookie("language", $cookie, time()+31000000, "/");	}}?>...<?php echo "The cookie is telling me : ".$_COOKIE['language'];?>

So when i come in first time, the cookie value is non existent, but if I have a look in the preferences and go see what all my cookies are, the cookie I wanted to create just then in that code is there, so it was set, but it seems the script on the same page ignores this and acts as if the cookies wasn't modified / setIf i refresh or change the value of $_GET['l'] then the message tells me the previous value, and now the new value has been changed in the browser, but isn't being displayed by the php.What is wrong with my script which makes the rest of that page not have the correct value of the cookie ?Thanks for any help...

Link to comment
Share on other sites

That's the way it is. Cookie values are posted to your script when your script first runs, same as post and get values. That is to say, the $_COOKIE array is set once only, and does not get updated after you call setcookie(); if you want to use the cookie value you sent, you'll just have to keep track of it.

Link to comment
Share on other sites

It looks like Dad is replying also, but cookies are only sent to the server when you load a page. If you set a cookie, it's not going to be sent to the server by the browser until the next request. Cookies are set using a header from the server, and by the time the server sends it's response header to set a cookie, the browser has already sent its cookies in the request headers.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...