6 replies
I have this little script I'm trying to simply logout here it is..

PHP Code:
<?php
session_start
();
  include(
"connect.php");
$username1 $_SESSION['username'];

if (!isset(
$_SESSION['$username'])) {
     echo 
"Sorry Your Not Logged In" $_session['username'];
    exit(); 
    
}
else if (isset(
$_GET['logout'])) {
session_destroy();
echo 
"You Have Been Successfully Logged Out";

    
}
  
?>
ok so what I'm saying is if the session isn't set would mean they are not logged in to logout right? then echo the sorry.. and exit.

else if the logout button was pressed destroy the session and echo success pretty straight forward

now if I change that and echo out that $username1 = $_SESSION['username'] echos fine so that tells me that the session is set ??? dosn't it?

issue is i'm always getting the sorry your not logged in but not the username??

what am I doing wrong here?
#session
  • Profile picture of the author Brad Hodge
    Check you session object. it seems to me that the session object does not exist. So if does not exist, then how will you retrieve the username from there. You will receive a NULL and getting nothing on the screen.

    I reckon that is your problem.
    {{ DiscussionBoard.errors[6144987].message }}
    • Profile picture of the author aisdbuilder
      Hi Solidsoul,

      I think your problem is in the code below where you're printing out the $_SESSIO['username'] instead of the variable previously set, $username1. but the conditional statement is firing because that same var doesn't exist. Also the conditional statement has a $ in front of the array key.

      Changing this:

      if (!isset(
      $_SESSION['$username'])) {
      echo
      "Sorry Your Not Logged In" . $_session['username'];
      exit();

      }
      --------------------------------------------------
      To This:

      if (!isset(
      $_SESSION['username'])) {
      echo
      "Sorry Your Not Logged In" .
      $username1;
      exit();

      }

      Will hopefully solve the problem.
      {{ DiscussionBoard.errors[6148777].message }}
  • Profile picture of the author Earnie Boyd
    Everyone so far has missed the lowercase $_session instead of the uppercase $_SESSION used to output the user name. $_session isn't a global variable while $_SESSION is. Also if !isset($_SESSION['$username']) is not the same as !isset($_SESSION['username']). And if $_SESSION['username'] isn't set then you're not going to be able to echo it.

    You probably want to code it like this

    <?php
    session_start();
    include("connect.php");

    if (!isset($_SESSION['username'])) {
    echo "Sorry Your Not Logged In";
    exit();
    }
    elseif (isset($_GET['logout'])) {
    session_destroy();
    echo "You Have Been Successfully Logged Out";
    }
    else {
    $username1 = $_SESSION['username'];
    }

    ?>
    Signature
    {{ DiscussionBoard.errors[6153121].message }}
  • Profile picture of the author jminkler
    you also have to destroy the cookies!

    session_destroy() wont work without also killing the cookies

    /**
    * If you wish to kill the session, then you must
    * delete the session cookie.
    * An http request is needed to effectively
    * set the cookie to permanent inactive status;
    * only the browser can remove the cookie.
    *
    **/

    if ( isset( $_COOKIE[ $session_name ] ) ) {
    if ( setcookie(session_name(), '', time()-3600, '/') ) {
    header("Location: http://localhost/some_other_page.php");
    exit();
    }
    else
    {
    // setcookie() fails when there is output sent prior to calling this function.
    }
    }

    recommend also PHP Security by Chris Schifflet ... don't reinvent the wheel here, too many potholes ..
    {{ DiscussionBoard.errors[6155522].message }}
  • Profile picture of the author jminkler
    Also ...
    think about it ...
    "issue is i'm always getting the sorry your not logged in but not the username??"

    Why would you get a username here if they are not logged in!
    {{ DiscussionBoard.errors[6155532].message }}

Trending Topics