Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    Oct 2010
    Posts
    68
    Thanks
    29
    Thanked 0 Times in 0 Posts

    Logout script not clearing $_session variables

    I'm trying to use this php to clear out the session variables and return to the index page. However it's not clearing them out. Any ideas?

    logout.php
    PHP Code:
    <?php 
        session_start
    ();
            unset(
    $_SESSION['user_id']);
            unset(
    $_SESSION['username']);
        
    session_destroy();
        
    header("Location: http://aaronhaas.com/pitchshark6/index.php?vid_id=1"); 
    ?>
    then in my navigation I'm using this code to either display their username and a logout link to logout.php or if they are not logged in display a sign in link.

    PHP Code:
    <?php
             
    // if logged in
        
    if (isset($_SESSION['user_id'])) 
        {
            
    // display 
            
    echo "<a href='#'>".$_SESSION['username']."</a>&nbsp; &nbsp;";
            echo 
    "<a href='scripts/logout.php'>Log Out</a>&nbsp; &nbsp;";
        }
            
    // if not logged in
        
    else
        {      
    // display login link
            
    echo "<a href='login.php'>Sign In</a>";
        }
    ?>
    here is my super simple login script
    PHP Code:
    $username $_POST['username'];
    $password $_POST['password'];
                
    //Check if the username or password boxes were not filled in
    if(!$username || !$password){
        
    //if not display an error message
        
    echo "<center>You need to fill in a <b>Username</b> and a <b>Password</b>!</center>";
    }else{
        
    // find user by username and password - working
        
    $userQuery 'SELECT * FROM users WHERE user_name ='.'"'$username.'" AND password='.'"'$password.'"' 
        
    $users mysql_query($userQuery);

        
    $user mysql_fetch_array($users);
        
    $_SESSION['user_id'] = $user['user_id'];
        
    $_SESSION['username'] = $user['username'];
        
    header("Location: http://aaronhaas.com/pitchshark6/index.php?vid_id=1");
        
        } 

  • #2
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,502
    Thanks
    8
    Thanked 1,089 Times in 1,080 Posts
    Look at example #1:
    http://php.net/manual/en/function.session-destroy.php

    Also ...
    Just try commenting-out the unset lines.


  •  

    Posting Permissions

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