Register

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
    Posts
    6
    Member #
    14378
    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:
    Code:
    <?php
    
    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!

  2.  

  3. #2
    WDF Staff mlseim's Avatar
    Join Date
    Apr 2004
    Location
    Cottage Grove, Minnesota
    Posts
    7,715
    Member #
    5580
    Liked
    717 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)

    <?php
    session_start();
    session_unregister(user);
    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
    Location
    Bandung, Indonesia
    Posts
    1,207
    Member #
    14572
    AFAIK...

    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
    Location
    Atlanta, GA
    Posts
    4,146
    Member #
    10263
    Liked
    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:24 AM.
Powered by vBulletin® Version 4.2.3
Copyright © 2019 vBulletin Solutions, Inc. All rights reserved.
vBulletin Skin By: PurevB.com