Problem with Logout Script

Discussion in 'PHP' started by gilgil2, Jan 4, 2012.

  1. #1
    Hi

    I am using the following code to try and log out users but it is not working, any ideas why?
    1. <?php
    2. session_start();
    3. if(isset($_SESSION['authenticated']))
    4. unset($_SESSION['authenticated']);
    5. echo 'you are not logged in';
    6. ?>



    The code I used to start the session is:

    1. $_SESSION['authenticated']=true;



    And that seems to work with the log in but I am unable to log out now.

    Also for the following code how would I then remove the cookie?
    1. setcookie('username', $_POST['username'], time()+60*60*24*365, 'www.website.com');
    2. setcookie('password', $_POST['password'], time()+60*60*24*365, 'www.website.com');



    I came up with:

    1. $past = time() - 100;
    2. setcookie(username, gone, $past);
    3. setcookie(password, gone, $past);



    Sorry if I am completely wrong, I am new at this!

    Thanks for any help
     
    gilgil2, Jan 4, 2012 IP
  2. AlexanderZ

    AlexanderZ Member

    Messages:
    28
    Likes Received:
    1
    Best Answers:
    1
    Trophy Points:
    48
    #2
    You should not store passwords in plain text in cookies. For deleting cookies your code is correct, but I would use an even earlier date. Just pass 0 in instead of $past. Try setting $_SESSION['authenticated'] to false instead of unsetting. Also, if you are redirecting after unsetting, you need to write back the data to the file, otherwise the redirect cancels the save.
     
    AlexanderZ, Jan 4, 2012 IP
    gilgil2 likes this.
  3. Matt-APEXCGI

    Matt-APEXCGI Peon

    Messages:
    99
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    • if(isset($_SESSION['authenticated']))
    • unset($_SESSION['authenticated']);
    From above you can use to check the cookies, if they exists it read them, if they don't simply it gives error that you are not logged in.
     
    Matt-APEXCGI, Jan 4, 2012 IP
  4. vikasvyas

    vikasvyas Greenhorn

    Messages:
    7
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    11
    #4
    1. if(isset($_SESSION['authenticated']))
    2. unset($_SESSION['authenticated']);
      use this i think this will help you :D
     
    vikasvyas, Jan 9, 2012 IP
  5. bartolay13

    bartolay13 Active Member

    Messages:
    735
    Likes Received:
    14
    Best Answers:
    1
    Trophy Points:
    98
    #5
    dont make things complicated. destroy all sessions upon logout

    session_start();
    session_destroy();
    header("/");
     
    bartolay13, Jan 11, 2012 IP