Jump to content

Cookie Help. It's acting Weird


countnikon

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.