Jump to content

Disabling page view if not logged in.


kinks

Recommended Posts

OK, when a user logs in, set a variable, along with all the other variables you set, such as username, id, email, etc.:

 

$_SESSION['logged'] = TRUE;

 

Next, whenever you want to hide/disable something, or do something for non logged in users, do this, here are some examples:

 

Redirect to login page:

session_start();
if(!$_SESSION['logged']){
     header("Location: login.php");
     exit;
}

 

Display a login form or a user navigation bar:

session_start();
if(!$_SESSION['logged']){
     echo '<form action="login.php" method="post">
          <p><input type="text" value="Username" name="username" /></p>
          <p><input type="password" name="password" /></p>
     </form>';
}else{
     echo '<a href="home.php">Home</a> | <a href="logout.php">Logout</a>';
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.