SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Wizard rctneil's Avatar
    Join Date
    Jun 2005
    Posts
    1,890
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Logging Out Problem

    I have made a simple members sytem which uses sessions to know when a user is logged in or out. and i am havng trouble with the logging out code. Below is the code i am using:

    Logging Out Code:
    PHP Code:
    // The logging out stuff
    IF ($_SESSION['logged']=="1") {
    echo 
    "<a href=\"ridetracker.php?act=logout\">Log Out</a>";
    }

    if(
    $_GET['act'] == 'logout'){
        
    //$_SESSION['username']="";
        //$_SESSION['logged']="0";
        //$_SESSION['userid']="0";
        
    unset($_SESSION['username']);
        unset(
    $_SESSION['id']);
        unset(
    $_SESSION['logged']); 


        echo 
    "<center><p>You are now logged out from the Ride Tracker.</p></center>";

    For some reason, when you clcik the log out link the page refreshes and you are still logged in. To actually log out the link needs to be clicked a second time and then it refreshes again you get the log out complete message?

    Any idea?

    Thanks

    Neil

  2. #2
    Dinah-Moe Humm mudshark's Avatar
    Join Date
    Dec 2003
    Posts
    1,072
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This works for me:
    PHP Code:
    <?php
    session_start
    ();
    $_SESSION['logged'] = 1;

    if(
    $_GET['act'] == 'logout'){ 
        unset(
    $_SESSION['username']); 
        unset(
    $_SESSION['id']); 
        unset(
    $_SESSION['logged']);

        echo 
    'Logged out.';
    }

    if(
    $_SESSION['logged'] == 1) { 
        echo 
    "<a href=\"?act=logout\">Log Out</a>"
    }
    ?>

  3. #3
    SitePoint Wizard rctneil's Avatar
    Join Date
    Jun 2005
    Posts
    1,890
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey,
    Can you explain that code for me please. The session start code is actaully right at the top of the page from where the code i posted is taken from. The sessions logged = 1 is set when you log in so why won't the code i posted work?

    Thanks

    Neil

  4. #4
    Dinah-Moe Humm mudshark's Avatar
    Join Date
    Dec 2003
    Posts
    1,072
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I basically reversed your code. My version checks for the presence of $_GET['act'] first, if set, it unsets all session vars which means the second if then becomes false (will not execute). A matter of precedence, I guess...

  5. #5
    SitePoint Evangelist Waffles's Avatar
    Join Date
    Nov 2005
    Posts
    435
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    <?php
    session_start
    ();

    if((
    $_SESSION['logged'] == '1') && ($_GET['act'] == 'logout')) {
       unset(
    $_SESSION['logged']);
       
    // etc
       
    session_destroy(); // perhaps?

    } else {

       echo 
    'You aren\'t logged in.';

    }

    if(
    $_SESSION['logged'] == '1') {

    echo 
    '<a href="?act=logout">Logout</a>';

    }
    ?>
    this also adds support if they aren't logged in and try to access the page.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •