Page 1 of 1

[SOLVED] protect page

Posted: Fri Aug 20, 2004 10:40 am
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');
}

?>

Posted: Fri Aug 20, 2004 11:00 am
by hawleyjr
So what is your question?

Posted: Fri Aug 20, 2004 3:44 pm
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?

Posted: Fri Aug 20, 2004 3:52 pm
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

Posted: Fri Aug 20, 2004 8:32 pm
by Mexican Hat
Thanks, I seem to have every thing working now.