logout not destroying variables

Discussion in 'PHP' started by mcf1992, Sep 22, 2011.

  1. #1
    Account.php

    <?php session_start(); ?><html xmlns="http://www.w3.org/1999/xhtml">  <head>  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />  <LINK href="includes/css/style.css" rel="stylesheet" type="text/css"><title>Account Dashboard</title>  </head>  <body><?phpinclude('header.php');
    include('dbsettings.php');
    include('navbar	.php');
    
    $con = mysql_connect("$host","$user","$password");  if (!$con)    {    die('Could not connect: ' . mysql_error());    }  
    mysql_select_db("$db_name", $con);  $username= $_SESSION['username']; // Editted 
    $sql="SELECT * FROM `user` WHERE `username`='{$username}'";  
    $result = mysql_query("$sql");  ?><div class= "accountsummary"><?phpif($row = mysql_fetch_array($result)){
            $_SESSION['id'] = $row['id'];         $_SESSION['firstname'] = $row['firstname'];         $_SESSION['lastname'] = $row['lastname'];         $_SESSION['address'] = $row['address'];echo "ID:";       echo $_SESSION['id'];echo "<BR />";echo "First Name:"; echo $_SESSION['firstname'];echo "<BR />";echo "Last Name:"; echo $_SESSION['lastname'];echo "<BR />";echo "Address:"; echo $_SESSION['address'];
    
        } if($_SESSION['id']== ''){  echo '<META HTTP-EQUIV="Refresh" Content="0; URL=login.php">';    exit; }   
      ?>  </div>    <p class="logout">  <a href=logout.php>Logout</a>  </p>    <p style="position: absolute; top: 100px; left: 230px; background-color: white; margin: 15px; "> Welcome, <?php echo "$username"; ?> </p>  <?php
    mysql_close($con); include('footer.php') ?>
    
    </body>  </html>
    PHP:
    logout.php
    <?phpsession_start();
    if(session_start())
    {  session_destroy();}
    header("location: login.php");?>
    PHP:
     
    mcf1992, Sep 22, 2011 IP
  2. jazzcho

    jazzcho Peon

    Messages:
    326
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #2
    jazzcho, Sep 22, 2011 IP
  3. mcf1992

    mcf1992 Peon

    Messages:
    14
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Already tried that did not work
     
    mcf1992, Sep 22, 2011 IP
  4. blueparukia

    blueparukia Well-Known Member

    Messages:
    1,564
    Likes Received:
    71
    Best Answers:
    7
    Trophy Points:
    160
    #4
    Unset each one individually:

    
    <?php
        unset($_SESSION['id']);
        //etc.
    ?>
    
    PHP:
     
    blueparukia, Sep 22, 2011 IP
  5. mcf1992

    mcf1992 Peon

    Messages:
    14
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    I tried that, yet no luck.
     
    mcf1992, Sep 22, 2011 IP
  6. fortehlolz

    fortehlolz Banned

    Messages:
    111
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #6
    <?php
    session_start();
    unset($_SESSION['id']);
    
    //or
    
    $_SESSION['id']=false;
    session_destroy();
    unset($_SESSION['id']);
    
    PHP:
     
    fortehlolz, Sep 23, 2011 IP
  7. JohnnySchultz

    JohnnySchultz Peon

    Messages:
    277
    Likes Received:
    4
    Best Answers:
    7
    Trophy Points:
    0
    #7
    session_destroy(); is not really the answer in logging-out your user, maybe setting the $_SESSION to a blank array may be an answer..

    $_SESSION = array();
     
    JohnnySchultz, Sep 23, 2011 IP
  8. Vooler

    Vooler Well-Known Member

    Messages:
    1,146
    Likes Received:
    64
    Best Answers:
    4
    Trophy Points:
    150
    #8
    Even while using global $_SESSION array you first need to initialize sessions.

    <?php
    session_start();
    #here second call to session_start doesn't make sense and would probably issue warning and return false always
    session_destroy();
    header("location: login.php"); 
    
    #if using php5 remove last ending tags to prevent any extra output after closing tags
    ?>
    PHP:
    or


    <?php
    session_start();
    
    unset($_SESSION['id']); #this one was right too
    header("location: login.php"); 
    
    #if using php5 remove last ending tags to prevent any extra output after closing tags
    ?>
    PHP:
    Ty it and let me know.

    regards
     
    Vooler, Sep 23, 2011 IP
  9. NLZ13

    NLZ13 Well-Known Member

    Messages:
    166
    Likes Received:
    3
    Best Answers:
    1
    Trophy Points:
    113
    #9
    I always used this and it never failed me:
    <?php
    session_start();
    ob_start();
    session_destroy();
    header("location: main.php");
    ?>
    Code (markup):
     
    NLZ13, Sep 23, 2011 IP