Delete cookie not working (PHP)


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 6 of 6

Thread: Delete cookie not working (PHP)

  1. #1
    Join Date
    Dec 2004
    Posts
    64

    Question Delete cookie not working (PHP)

    I am using PHP, running on Windows Server 2003. Tested in Firefox/IE latest versions.

    I want to delete the cookie when the user has logged out, but having no joy whatsoever! I've researched this to death, and found the code to be valid, but it doesn't work - can anyone shed some light on this pls?

    Part 1 of the code creates the cookie when logging in:
    <?php
    if (isset($HTTP_POST_VARS['username'])) {
    setcookie("myCookie", $HTTP_POST_VARS['username'], time()+3600);
    }
    ?>


    Part 2 of the code is the logout code:
    <?php
    //initialize the session
    session_start();

    // ** Logout the current user. **
    $logoutAction = $_SERVER['PHP_SELF']."?doLogout=true";
    if ((isset($_SERVER['QUERY_STRING'])) && ($_SERVER['QUERY_STRING'] != "")){
    $logoutAction .="&". htmlentities($_SERVER['QUERY_STRING']);
    }

    if ((isset($_GET['doLogout'])) &&($_GET['doLogout']=="true")){
    //to fully log out a visitor we need to clear the session varialbles
    session_unregister('MM_Username');
    session_unregister('MM_UserGroup');


    // delete the cookie when logging out
    setcookie("myCookie", "", time()-3600);


    $logoutGoTo = "../index.php";
    if ($logoutGoTo) {
    header("Location: $logoutGoTo");
    exit;
    }
    }
    ?>


    Part 3 of the code is the link itself:
    <?php
    echo '<a href="'. $logoutAction. '" target="_self">Logout</a>';
    }
    ?>

  2. #2
    Join Date
    Dec 2004
    Posts
    64
    Anyone??

  3. #3
    Join Date
    Jan 2005
    Posts
    413
    Well I'm not a php coder but in other languages, the cookie expiration date being set to some time in the past will delete the cookie

  4. #4
    Join Date
    Dec 2004
    Posts
    64
    Hi
    Sure, which is what I have done, like this:

    setcookie("myCookie", "", time()-3600);

    Maybe the code isn't being ran at all (it is currently part of the logout code) - so what I will do is redirect the user to a dedicated logout.php page, where the cookie is deleted.

    I will post an update soon...

  5. #5
    Join Date
    Dec 2004
    Posts
    64

    Thumbs up Success!

    Ok, aside from wanting to swear at this nonsense, I removed the cookie deletion code from the logout code, put the delete cookie code on a seperate page and viola! it works.

  6. #6
    Join Date
    Jan 2005
    Posts
    413

Similar Threads

  1. Replies: 3
    Last Post: 06-23-2006, 05:44 AM
  2. Connecting to java chat server from php
    By prasad in forum Java
    Replies: 0
    Last Post: 05-15-2006, 09:45 AM
  3. Replies: 0
    Last Post: 02-03-2006, 07:49 PM
  4. help reg creating ms access file
    By vbcoder in forum VB Classic
    Replies: 9
    Last Post: 04-28-2005, 12:36 PM
  5. httpcookie problem
    By AAnthony in forum .NET
    Replies: 0
    Last Post: 06-11-2001, 05:32 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center