If this is your first visit, please click the Sign Up now button to begin the process of creating your account so you can begin posting on our forums! The Sign Up process will only take up about a minute of two of your time.

Results 1 to 4 of 4
  1. #1
    Junior Member
    Join Date
    Nov 2006
    Member #
    OK, so there are apparently a few ways to end sessions or delete session data, and I'm having trouble finding a simple explanation of what the difference is between them. Here's an example of each method I've seen:
    unset($_SESSION); // method one
    $_SESSION = array(); // method two
    session_destroy(); // method three
    setcookie(session_name(), "", time()-3600); // method four
    How is each method different? Which should you use when? Which method(s) do you personally prefer to use and why?

    Thanks for your help!


  3. #2
    WDF Staff mlseim's Avatar
    Join Date
    Apr 2004
    Cottage Grove, Minnesota
    Member #
    726 times
    I'm not enough of a PHP expert to know the exact differences.

    But this is the method I usually use:
    (where "user" is the session name I registered)

    header ("location: index.php");

    It unregisters the sessions and returns to the index page.
    The script is usually called by a log-out button or link.

  4. #3
    Senior Member Steax's Avatar
    Join Date
    Dec 2006
    Bandung, Indonesia
    Member #

    1 & 2 are the same. Method 1 (unsetting) removes the contents of a variable (but removing the type of a special variable might be unsafe) while method 2 resets it as a blank array.

    Method 3 replaces the session ID.

    Method 4 works by changine your computer's session ID in its cookies. I'm not so sure on this one.

    I usually just unset individual session variables. Like
    PHP Code:
    $_SESSION['somevar'] = false
    Note on code: If I give code, please note that it is simply sample code to demonstrate an effect. It is not meant to be used as-is; that is the programmer's job. I am not responsible to give you support or be held liable for anything that happens when using my code.

  5. #4
    Senior Member
    Join Date
    Jun 2005
    Atlanta, GA
    Member #
    1 times
    Basically, 1 & 2 don't `end' the session -- they just reset the server's variables associated with it. 3 replaces the session id, so it doesn't end the session; it just changes it to a new, fresh one with no variables associated with it. And 4 will actually clear the session id, which basically disassociates that computer from any session, which is the closest I suppose you get to actually `ending' the session, as the client computer is then no longer associated with any session.

Remove Ads

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
All times are GMT -6. The time now is 04:41 AM.
Powered by vBulletin® Version 4.2.3
Copyright © 2020 vBulletin Solutions, Inc. All rights reserved.
vBulletin Skin By: