Solved

Strange problem with Cookies

Posted on 2010-11-25
13
368 Views
Last Modified: 2012-06-21
Hello there,
I am developing a site in php and this is the first time in years I have this kind of problem, and can't figure it out...

The problem is that I create some cookies, erase them and when I refresh the page the cookies are still there.

Example:
<?php 

print_r($_COOKIE);
exit();
?>

Open in new window


Returns:
Array ( [fbs_149865948383849] => \"access_token=149865948383849|2.PfG_sklQhUD7dSKqe_lzIQ__.3600.1290704400-698363010|c0usGkjKPEsASYTZqjF_Y-Rwxj8&base_domain=pregunpedia.com&expires=1290704400&secret=nQ3fNvMekK6D11hbT8XH6Q__&session_key=2.PfG_sklQhUD7dSKqe_lzIQ__.3600.1290704400-698363010&sig=a440d03a60808e5ee02e14a941a1eea6&uid=698363010\" [conecta_fb] => iniciado_fb [PHPSESSID] => 015fa2b540ec4185cebd6cab2f7da027 )

I have a page called logout.php, I execute it and at the bottom of this page I have

print_r($_COOKIES) 

Open in new window


and returns no value, afterwards I leave that page (logout.php)  and return to index.php that only has print_r($_COOKIE); exit(); and returns the cookies again!!

I use this function
function DeleteAllCookies(){
           if (isset($_SERVER['HTTP_COOKIE']))
           {
              $cookies = explode(';', $_SERVER['HTTP_COOKIE']);
              foreach($cookies as $cookie)
              {
                 $parts = explode('=', $cookie);
                 $name = trim($parts[0]);
                 unset($_COOKIE[$name]);
              }
          }}


DeleteAllCookies();

unset($_SESSION['conecta_fb']);
unset($_SESSION['plataforma']);

print_r($_COOKIE);
echo "<hr>";
print_r($_SESSION);
header("location: ".$_SERVER['HTTP_REFERER']);

Open in new window


Has anyone has any idea of what I can be happening?

Thank's in Advanced.

DAniel.
0
Comment
Question by:visionari
  • 8
  • 3
  • 2
13 Comments
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 34213118
This may happen if you have more than one copy of the browser running.  All your browser instances share the same cookie jar.
0
 
LVL 110

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 34213132
In the code example posted above, line 22 will fail because print_r produces browser output.  And I might want to do the delete-all-cookies function this way:
function deleteAllCookies()
{
    foreach ($_COOKIE as $name = $nothing)
    {
        setcookie($name, NULL, time()-3600);
    }
}

Open in new window

0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 34213141
Here is my teaching example of a Logout script.  See if this makes sense to you.  If not, please post back and I'll try to help.

best regards, ~Ray
<?php // RAY_logout_example.php
error_reporting(E_ALL);

// MAN PAGE HERE: http://us.php.net/manual/en/book.session.php
// ALWAYS START THE SESSION ON EVERY PAGE
session_start();

// SEE IF THE CLIENT IS ALREADY LOGGED OUT
if (empty($_SESSION["uid"]))
{

// CLIENT IS NOT LOGGED IN
    echo "<br/>YOU ARE NOT LOGGED IN \n";
    echo "<br/>CLICK HERE TO <a href=\"RAY_login_example.php\">LOG IN</a>\n";
    die();
}

// SET THE EXPIRATION FOR COOKIES
define('COOKIE_LIFE', 60*60*24); // A 24-HOUR DAY IN SECONDS ( = 86,400 )
$cookie_expires	= time() - date('Z') - COOKIE_LIFE;

// CLEAR THE INFORMATION FROM THE $_SESSION ARRAY
$_SESSION = array();

// IF THE SESSION IS KEPT IN COOKIE, FORCE SESSION COOKIE TO EXPIRE
if (isset($_COOKIE[session_name()]))
{
    setcookie(session_name(), '', $cookie_expires, '/');
}

// TELL PHP TO ELIMINATE THE SESSION
session_destroy();


// OPTIONAL - CLEAR ALL COOKIES
// foreach ($_COOKIE as $key => $value)
// {
//    setcookie($key, '', $cookie_expires, '/');
// }

// OPTIONAL - REDIRECT TO THE HOME PAGE
// header("Location: /");
// exit;


// GIVE THE CLIENT A LINK TO THE LOGIN
echo "<br/>YOU ARE LOGGED OUT\n";
echo "<br/>CLICK HERE TO <a href=\"RAY_login_example.php\">LOG IN</a>\n";

Open in new window

0
The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

 

Author Comment

by:visionari
ID: 34214687
Thank's, but didn't work, this is very strange... I must be doing something wrong... I am using the facebook connect (as I am using fb connect and some extra cookies, I would need to kill all cookies), should have any problems as they are all in the same domain, put it fails for some reason...

Thank's all :)
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 34214736
What did you see when you ran this command:

var_dump($_COOKIE);
0
 

Author Comment

by:visionari
ID: 34214748
I see this:

array(3) { ["PHPSESSID"]=> string(32) "4f3995a7cc46b0d2bc7c9cdc28832af1" ["fbs_149865948383849"]=> string(308) "\"access_token=149865948383849|2.nMeKITzS_oQII4iZDtgXzA__.3600.1290733200-698363010|o7M1EoDsoJ0h04nN2baOIfiSy8k&base_domain=daniel.com&expires=1290733200&secret=nzlVtYkyI6jCK7s_cHoktQ__&session_key=2.nMeXMTzS_oQII4iZDtgXzA__.3600.1290733200-698363010&sig=81d6e6fd2528064597f1c518bc508d62&uid=698363010\"" ["conecta_fb"]=> string(11) "iniciado_fb" }
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 34214802
You may find that easier to read if you echo "<pre>"; before the output.  And did you make sure that you have only ONE instance of the browser running?  Then if you run a script to delete the cookies, they will be gone.  

You can use Firefox to see your cookies.  On my implementation, it's something like Tools => Options => Privacy => remove individual cookies.
0
 
LVL 11

Expert Comment

by:bansidhar
ID: 34218911
unset($_COOKIE[$name]); won't help. What you are doing is removing the variable from the server side. Cookies are stored in browser so even if you delete it from the server side on next request the browser will send all those and will be re-created. so you have to ask the browser to remove the cookie from its side.

Removing cookie is bit strange. you have to set the cookie you want to remove with  exactly all the parameters used to set as you set it but with a pre date to remove it. This triggers the removal mechanism in your browser. Now if you do a redirect the any decent browser is supposed to clear the cookie.
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 34218935
@bansidhar: Exactly what I was trying to get across with the code example at ID:34213132.  I do not think all the parameters have to be the same when you remove a cookie.  The name has to be the same, and the expiration has to be in the past, but the other parameters do not seem to matter.
0
 
LVL 11

Expert Comment

by:bansidhar
ID: 34218993
other parameters also need to be the same (learned the hard way ;) ) you can have blank string for the value, but path, domain, secure and httponly parameters have to be the same
0
 
LVL 110

Assisted Solution

by:Ray Paseur
Ray Paseur earned 500 total points
ID: 34219062
Interesting - I will have to set up a test and see if I can get two cookies with the same name and different paths.
0
 

Author Closing Comment

by:visionari
ID: 34293468
couldn't solve problem...
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 34294481
OK, Here are the results of my tests with cookies.  You can set a cookie with an identical name to an existing cookie if the paths are different.  However the browser will only return the cookies that are appropriate for your current path.  It looks like PHPwill find the most precise cookie, so if you have cookies for '/' and for '/xyz' and you are running in the '/xyz' path, you will see the cookie set for '/xyz' in the $_COOKIE array and will not see the cookie for '/'  However if you are running outside of the '/xyz' path you will see the cookie for '/' -- this makes sense.

So if you've gotten cookies set by a specific directory path, you will need to be running a script that is in that directory path to see the cookies, and to unset them.
0

Featured Post

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
This article discusses how to create an extensible mechanism for linked drop downs.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

713 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question