note 25256 rejected from function.setcookie by nicos

From: Date: Tue, 17 Sep 2002 10:01:30 +0000
Subject: note 25256 rejected from function.setcookie by nicos
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-36809@lists.php.net to get a copy of this message
I would like to buy brendanr@optushome.com.au a beer or three! Thanks to his example above (26-Feb-2002 11:42) I finally managed to get things straight in my head. The difficulty was not necessarily in setting a cookie - there are copious examples on how to do that! - but more with reading cookies! With his example, I was able to change $vars, play with settings and find out how all this fitted together! I love you! If the following 'simpleton' example will at least go half way to helping someone else, the all the better! #!/usr/local/bin/php (my server requires php files to be run as a script, similar to a PERL CGI for example. If yours doesn't, just delete this first line and I think it should work!) <? $lang=strtok($_SERVER["HTTP_ACCEPT_LANGUAGE"],','); //my site is multi-linguag, therefore I'd like to know the client's language setting if(empty($HTTP_COOKIE_VARS['user_name'])) { // set cookie and store the creation time within the value of the cookie //I've places a few $vars here and there as on-screen displays, so you can see what's happening! $cookie_var1 .= "HTTP_COOKIE_VARS[user_name] IS EMPTY : CREATING COOKIE : "; setcookie("user_name", "meetn2veg"."|"."$lang"."|".time(),3600); $cookie_var1 .= "COOKIE CREATED<BR>"; } else { // check if cookie has expired, if so delete it $cookie_life = 3600; $cookie_info = explode("|", $HTTP_COOKIE_VARS["user_name"]); $cookie_user = $cookie_info[0]; $cookie_lang = $cookie_info[1]; $cookie_time = $cookie_info[2]; $life_left = $cookie_life - (time()-$cookie_time); if((time()-$cookie_time) > $cookie_life) { // cookie has expired so delete it. $cookie_var1 .= "COOKIE EXPIRED : "; setcookie("user_name", ""); $cookie_var1 .= "COOKIE DELETED<BR>"; } else { $cookie_var1 .= "COOKIE STILL ALIVE! COOKIE HAS $life_left seconds to live!<BR>"; } } ?> <HTML> <HEAD> <TITLE>Basic Baking</TITLE> </HEAD> <BODY MARGINHEIGHT="0" MARGINWIDTH="0" TOPMARGIN="0" LEFTMARGIN="0" BGCOLOR="#FFFFFF"> <? echo "\$_SERVER[\"HTTP_ACCEPT_LANGUAGE\"] = "; echo $_SERVER["HTTP_ACCEPT_LANGUAGE"]; echo "<BR>"; echo "\$_SERVER[\"HTTP_USER_AGENT\"] = "; echo $_SERVER["HTTP_USER_AGENT"]; echo "<BR>"; echo "\$HTTP_COOKIE_VARS[\"user_name\"] = "; echo $HTTP_COOKIE_VARS["user_name"]; echo "<BR>"; echo "\$_COOKIE[\"user_name\"] = "; echo $_COOKIE["user_name"]; echo "<P>"; echo "\$cookie_var1 = $cookie_var1<BR>"; echo "\$cookie_user = $cookie_user<BR>"; echo "\$cookie_lang = $cookie_lang<BR>"; echo "\$cookie_time = $cookie_time<P>"; ?> </BODY> </HTML> Just place this in one file, if server requires php files to be run as scripts, leave the first lin ( #!/usr/local/bin/php ), changing to suit your servers' config and chmod 755, look at it from a browser and just keep pressing F5 !!!! Change a few settings, upload and F5 again to see what happens. I can't say it works on all flavours, all browsers, at this precise moment, I don't care! It works for me! And after 3 weeks on and off trying to suss this one out, I'm chuffed to bits! Hope it helps someone else.

« previous php.notes (#36809) next »