Session Basics

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
neridaj
Forum Commoner
Posts: 40
Joined: Fri Jan 05, 2007 9:55 pm

Session Basics

Post by neridaj »

Hello,

I'm just learning about sessions and want to control access to inner pages of a website i.e., restricting access based on whether or not a user is logged in, and also why my sessions never seem to time out.

something like this, but this always calls display_folders()

Code: Select all

if ($username && $passwd)
// they have just tried logging in
{
  try
  {
    display_folder();
    // if they are in the database register the user id
    $_SESSION['valid_user'] = $username;
  }
  catch(Exception $e)
  {
    // unsuccessful login
    do_html_header('Problem:');
    echo 'You could not be logged in. 
          You must be logged in to view this page.';
    do_html_url('login.php', 'Login');
    do_html_footer();
    exit;
  }      
}

Thanks for any insight,

Jason
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Re: Session Basics

Post by aceconcepts »

By default sessions timeout after 24 minutes.

To check whether a user is logged in, check the status of their session variables:

Code: Select all

 
if(isset($_SESSION['logged_in']) && $_SESSION['logged_in']==1)
{
   //user is logged in
}
else
{
   //not logged in
}
 
You could also check by session_id().
Post Reply