Jump to content

What could cause a cookie to become unset?


carlg

Recommended Posts

This is a unique problem in which I'm struggling on.  Earlier I thought the problem was global variables, but it is not the problem.

 

It is the cookies which is causing me the problem.

 

So here's the story.

 

I had an entire site working perfectly on my test server in my basement (php 5.2)

I signed up for VPS access.

Installed this entire site on my new VPS server  (php 4.3.9)

 

I have the exact same code running on both servers, but it works on my test server in the basement, but not on the VPS server.

 

For some reason one of my cookie variables is becoming unset.

 

I use the setcookie function to set the cookie and $_COOKIE['name'] to get the value out.

 

I ripped out all of the cookie processing code on the VPS server and put it in separate source files with no other code from the site and it worked perfectly fine.  The cookie value was retained as expected.

 

Where should I look to fins an error like this?

Where would you look first?

 

This is driving me crazy and there is really no way I can post the code of my entire site here.  I thought about ripping out the cookie related code and posting it here, but when I did, it worked.

 

Please help

 

Thanks

 

Carl

 

Link to comment
Share on other sites

Ok, but this code works fine when it is isolated.

 

Here is the cookie setting

 

if ($staylogged)
                        {
                                //user chooses to stay logged from this machine
                                setcookie(profile_id, $profile_id, time()+360000000, "/");
                        }
                        else
                                //user does not want to stay logged from this machine
                        {
                                setcookie(profile_id, $profile_id, 0, "/");
                        }

 

 

Here is where I get the value of the cookie

 

if (!empty($_COOKIE['profile_id']))
{
     //do stuff
}
else
{
    //do other stuff
}

 

 

Here is where the cookie gets unset, but this code does not get executed until the user logs out.  It does not get executed in my scenario.

 

if ($_GET['logout'])
{
        $profile_id = "";
        setcookie("profile_id", '',time() - 3600,"/");
        $o_id = '';
        $o_uname='';
}

Link to comment
Share on other sites

thig is cookies are one of the few things in php that can work different on different browsers and such. So you should be filling out all of the cookie fields to minimize room for error

 

for example you should be doing something like the following

$sitedomain = ereg_replace('www.', '', $_SERVER['SERVER_NAME']);

setcookie ("profile_id, $profile_id, time()+360000000, "/", "$sitedomain", "0")

 

 

Link to comment
Share on other sites

GREAT NEWS!!!!!

 

I finally figured out the problem!!!!!!

 

I was outputing the <html> tag before setting the cookie.  This seems to be a problem with the older version of php, but not with the newer version of php.

 

When I did my little sample test above, I was not outputing anything before setting the cookie, that's why it was working.

 

Again, thanks for all of your input.

 

Carl

 

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.