Session tracking/authentication

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Franky
    New Member
    • Mar 2007
    • 14

    Session tracking/authentication

    Users can log into my site via a php login script or registration form (for new users). Login checks that the credentials are correct, and the registration creates a new entry in a mysql db.

    At this point a new session is started, a session variable ($_SESSION['logged_in']) is set to yes, and they are redirected using header(location ).

    Can anyone suggect code to place on subsequent pages that will check on arrival if the current user has a session started and that the variable 'logged_in' is set to "yes" (if so, do page, else display "you are not authorised, etc")?
  • federicog
    New Member
    • Mar 2007
    • 30

    #2
    You could use TRUE instead of yes, I'm not sure of how PHP deals with this, but a boolean should take up less memory than a string and in this cases booleans are much more intuitive (you either are logged in or not, there aren't any other possible options).

    Anyway, to check if the user is logged in you could do.

    [PHP]session_start() ;

    if(!isset($_SES SION['logged_in']))
    header("Locatio n: login.php");
    [/PHP]

    That should work.
    Just remember that both session_start() and header() must be placed before ANY output is sent to the browser.

    Good Luck!

    Comment

    Working...