countnikon Posted May 30, 2006 Share Posted May 30, 2006 I'm trying to set a cookie to hold a particular $_GET['id'] so that it holds a value for 4 days. The reason being is that I always want a value set for the site. Here's the code.[code]if(isset($_GET['id'])){ setcookie('cins','',time()-345600); echo "Unset Cookie ".$_GET['id']." ".$_COOIKE['cins']."<br>"; setcookie('cins',$_GET['id'],time()+345600); echo "Set Cookie ".$_GET['id']." ".$_COOKIE['cins']."<br>";}if(isset($_COOKIE['cins'])) $cins=$_COOKIE["cins"];echo "Set CINS ".$cins."<br>";[/code]However, when I change id's this is what happens.[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]Unset Cookie 400327Set Cookie 400327 810011Set CINS 810011[/quote]It unsets the cookie, sets the cookie to be the new value, and still uses the old cookie value to set $cins.any ideas what could be going on? I appreciate the help. Link to comment https://forums.phpfreaks.com/topic/10776-cookie-help-its-acting-weird/ Share on other sites More sharing options...
werty37 Posted May 30, 2006 Share Posted May 30, 2006 The following code typically does not work as you might expect: [code] setcookie(‘name’, ‘tintin’); $membername = $_COOKIE[‘membername’]; print(“The member name is $membername<BR>”);[/code]The cookie will not be set until the current page’s HTTP headers arrives at the client.I am a newbie too. Please correct me if i am wrong. <?php ob_start(); session_start();if(isset($_GET['id'])){ setcookie('cins','',time()-345600); ob_flush(); echo "Unset Cookie ".$_GET['id']." ".$_COOKIE['cins']."<br>"; setcookie('cins',$_GET['id'],time()+345600); ob_flush(); echo "Set Cookie ".$_GET['id']." ".$_COOKIE['cins']."<br>";}if(isset($_COOKIE['cins'])) $cins=$_COOKIE["cins"];echo "Set CINS ".$cins."<br>"; Link to comment https://forums.phpfreaks.com/topic/10776-cookie-help-its-acting-weird/#findComment-40328 Share on other sites More sharing options...
countnikon Posted May 30, 2006 Author Share Posted May 30, 2006 I tried it out and it was still a no go. I also tried moving the $cins=$_COOKIE['cins']; to after the header information and it still didn't work.Well after reading the PHP Manual, I read that the cookie is not available until the next page refresh. I figured out what I should do now. I should have read the manual first. Sorry ya'll.For future reference, here is what I had to do.[code]<?PHPif(isset($_GET['id'])){ setcookie('cins','',time()-345600); setcookie('cins',$_GET['id'],time()+345600);}if(!isset($_GET['id'])) $cins=$_COOKIE['cins'];else $cins=$_GET['id'];?>[/code] Link to comment https://forums.phpfreaks.com/topic/10776-cookie-help-its-acting-weird/#findComment-40344 Share on other sites More sharing options...
werty37 Posted May 30, 2006 Share Posted May 30, 2006 You got it right. But i dont know why there is problem with buffering.I ve got problems with this code... Do u have any idea.[code]<?php ob_start('ob_gzhandler'); session_start();if (!isset($_COOKIE['PHPSESSID'])){session_set_cookie_params("3600","/test","localhost","0");session_start();ob_flush();if (!isset($_COOKIE['PHPSESSID'])) { echo "Cookies are disabled."; }}else { echo session_id(); }?>[/code]Thanks Link to comment https://forums.phpfreaks.com/topic/10776-cookie-help-its-acting-weird/#findComment-40350 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.