Gw Temp

Menu

Tutorial - 'Password Protecting pages' by skyrider

An item about Programming Languages posted on

Blurb

skyrider shows you how to protect a single page with a password in PHP.

Body

Hi, Skyrider here again and this time to show you how to password protect a single page.

Now, this shoudl be very simple. Create a new file and call itpassword.php. Inside it, make a simple form:

<form action="<? echo $PHP_SELF; ?>" method="POST">
<b>Password</b><input type="password" name="pass"><br>
<input type="submit" name="submit" value="submit"><br>

Wonderful! Now make a new file, and call it whatever you wish. Inside it, It shall contain the following code.


if (!$submit)
{
echo '<b><center>You must supply a password to view this page!</center></b><br>'
include './password.php';
}
else
{
if ($pass!="password")
{
echo '<b><center>ERROR: INVALID PASSWORD</center></b><br>';
include './password.php';
}
else
{
include './yourfile.php';
}}
?>

Now, if you are experienced with php, or even know the basics, this should've been a breeze. But just incase you are new, I will explain it to you.

if (!$submit)
{
echo '<b><center>You must supply a password to view this page!</center></b><br>'
include './password.php';

That code right there, basically checks to see if you have pressed the submit button. And if they didn't, include the password form.

}
else
{
if ($pass!="password")
{
echo '<b><center>ERROR: INVALID PASSWORD</center></b><br>';
include './password.php';
}

And if they DID press submit, this code checks to see if the entered password was correct.

else
{
include './yourfile.php';
}}
?>

Then that will include the page you wanted to view if all the error checking failed.

You can do this in many different ways and in different languages, which can also be more secure, but this is only a basic password requiring script.

Now, if you read the tutorial, you should be able to password protect files!