#1
  1. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2009
    Posts
    694
    Rep Power
    7

    $_SESSION issues


    Apparently I must not be thinking right today. I made a login, which set 2 session variables, and I cannot get the to echo when called later. And the issue I am havin now is checking if a user is logged in and, if not, redirecting them to the login page. Anyone see something wrong with this?
    PHP Code:
        <?php
          session_start
    ();
          if(!isset(
    $_SESSION['loggedin']) && $_GET['loc'] != 'login') {
            
    header('index.php?loc=login');
          }
        
    ?>
    EDIT: The variable $_SESSION['loggedin'] can not be set, or even a blank string, because this is the only reference that exists of it so far.
    Last edited by Triple_Nothing; February 3rd, 2013 at 12:12 PM.
  2. #2
  3. Wiser? Not exactly.
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    May 2001
    Location
    Bonita Springs, FL
    Posts
    5,958
    Rep Power
    4035
    Originally Posted by Triple_Nothing
    PHP Code:
            header('index.php?loc=login'); 
    In order to redirect you have to send the Location: header.
    Code:
    header('Location: index.php?loc=login');
    Recycle your old CD's, don't just trash them



    If I helped you out, show some love with some reputation, or tip with Bitcoins to 1N645HfYf63UbcvxajLKiSKpYHAq2Zxud
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2009
    Posts
    694
    Rep Power
    7
    Thank you very much. I so missed that. Now, as far as my echo to such. Just to make sure it is set, I altered the login check to:
    PHP Code:
        <?php
          session_start
    ();
          if(!isset(
    $_SESSION['user']) && $_GET['loc'] != 'login') {
            
    header('Location: index.php?loc=login');
          }
        
    ?>
    And it continues, telling me $_SESSION['user'] is set. Now, a little down the page I run:
    PHP Code:
    Logged in as: <? echo $_SESSION['user']; ?><br />
    via an include(); file, but get no username to show. Any ideas?
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2009
    Posts
    694
    Rep Power
    7
    Never mind. I just noticed I tossed a short tag in there. Fixed it and all is good. Thanks for the help.
  8. #5
  9. Wiser? Not exactly.
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    May 2001
    Location
    Bonita Springs, FL
    Posts
    5,958
    Rep Power
    4035
    Note that after a header() redirect you need to stop the script, such as with exit;. If you do not do that, the script will continue to process and execute whatever else the script does. For example if your page were there to delete something from your DB, it will still delete it even though the user was not logged in and got redirected.
    Recycle your old CD's, don't just trash them



    If I helped you out, show some love with some reputation, or tip with Bitcoins to 1N645HfYf63UbcvxajLKiSKpYHAq2Zxud
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2009
    Posts
    694
    Rep Power
    7
    Thanks.

IMN logo majestic logo threadwatch logo seochat tools logo