Jump to content

Archived

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

countnikon

Cookie Help. It's acting Weird

Recommended Posts

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 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.

Share this post


Link to post
Share on other sites
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>";

Share this post


Link to post
Share on other sites
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]
<?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'];
?>
[/code]

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.