Jump to content

need some advice


stelthius

Recommended Posts

ok what i want to accomplish,

 

i have a bunch of pages i want to protect with a login system, but i dont want people to be able to access these pages without being logged in so say for instance they logged in and went to page www.mysite.com/page3.html and then logged out and went to www.mysite.com/page3.html i want it to some how say you need to login and not let them view the pages untill they do login, im not a pro im a very very new to php so any help or advice is very much appretiated.

 

 

thanks Rick

Link to comment
Share on other sites

Basically upon logging in you create a $_SESSION variable (or a $_COOKIE, but if you don't want the variables to last longer than the browser is open I'd recommend using $_SESSION because they're more secure) and then each page checks to make sure those $_SESSION variables exist.

 

Very basic example:

 

Upon logging in you'd do this:

 

// Assuming you've already validated their login
$_SESSION['userid'] = $userid; // Where the userid is the id of the user (the id could be their username or the row id in database table for the given user)
$_SESSION['authlevel'] = $authlevel; // If you want to only show certain sections of the page based on permission level you'd use something like this

 

At the top of each page you'd do something like this:

 

// For making sure they're logged in do the following, make sure you do this before the headers have been sent (before any html code)
if(!isset($_SESSION['userid'])){
  header("Location: login.php");
}
// put html code here, they'll have to be logged in to view it

// For making sure they have needed permission levels to view the following content, this does not have to be before the headers, 
// it can be anywhere on the page where you wish to restrict permissions
if($_SESSION['authlevel'] == 2){
// show level 2 permission content
}

 

This is a very basic example, but the concept is there.  Let me know if you need further details.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.