[SOLVED] protect page

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
User avatar
Mexican Hat
Forum Newbie
Posts: 19
Joined: Sat Mar 08, 2003 8:35 pm
Location: Baltimore, Maryland
Contact:

[SOLVED] protect page

Post by Mexican Hat »

What I want to do is have user1.php (the page the user logs into) protected so only the user loging in can go to that page but I don't want any thing to complicated. Here is my code:

login.php

Code: Select all

<?php

session_start();

if (($username == 'user1') && ($password == 'pass1'))
{
    $_SESSION['username'] = $username;
    header('location: user1.php');
}
elseif (($username == 'user2') && ($password == 'pass2'))
{
    $_SESSION['username'] = $username;
    header('location: user2.php');
}
elseif (($username == 'user3') && ($password == 'pass3'))
{
    $_SESSION['username'] = $username;
    header('location: user3.php');
}
else
{
    header('location: error.html');
}

?>
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Post by hawleyjr »

So what is your question?
User avatar
Mexican Hat
Forum Newbie
Posts: 19
Joined: Sat Mar 08, 2003 8:35 pm
Location: Baltimore, Maryland
Contact:

Post by Mexican Hat »

I want the .php pages that the users log into protected, so you can't just type in the page url you need to be logged in to access them. How would you do that?
Draco_03
Forum Regular
Posts: 577
Joined: Fri Aug 15, 2003 12:25 pm
Location: Montreal, Canada

Post by Draco_03 »

Upon loggin if login succesful you create a session variable and you chek it in each pages
example:
login pages when he succesfully log in

Code: Select all

$_SESSION['name'] = $username;
i juste assigned his username in a session variable

In all your "protected" pages you add

Code: Select all

if(!isset($_SESSION['name'])){
	echo("Need to log in first");
}else{
//whatever you pages is
User avatar
Mexican Hat
Forum Newbie
Posts: 19
Joined: Sat Mar 08, 2003 8:35 pm
Location: Baltimore, Maryland
Contact:

Post by Mexican Hat »

Thanks, I seem to have every thing working now.
Post Reply