Jump to content


Photo

Cookie Help. It's acting Weird


  • Please log in to reply
3 replies to this topic

#1 countnikon

countnikon
  • Members
  • PipPip
  • Member
  • 19 posts

Posted 30 May 2006 - 02:14 PM

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.

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>";

However, when I change id's this is what happens.
[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]
Unset Cookie 400327
Set Cookie 400327 810011
Set 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.

#2 werty37

werty37
  • Members
  • PipPipPip
  • Advanced Member
  • 49 posts

Posted 30 May 2006 - 06:34 PM

The following code typically does not work as you might expect:

setcookie(‘name’, ‘tintin’);
   $membername = $_COOKIE[‘membername’];
   print(“The member name is $membername<BR>”);

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>";

#3 countnikon

countnikon
  • Members
  • PipPip
  • Member
  • 19 posts

Posted 30 May 2006 - 07:18 PM

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.
<?PHP
if(isset($_GET['id']))
{
  setcookie('cins','',time()-345600);
  setcookie('cins',$_GET['id'],time()+345600);
}
if(!isset($_GET['id']))
  $cins=$_COOKIE['cins'];
else
  $cins=$_GET['id'];
?>


#4 werty37

werty37
  • Members
  • PipPipPip
  • Advanced Member
  • 49 posts

Posted 30 May 2006 - 07:29 PM

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.

<?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(); }
?>

Thanks




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users