Basic Password Protection
Views Today: 1, Total Views: 3864, Submitted by: Python on 15-06-2006
There can be a number of reasons why you would want to password protect a specififc area or page of your website but the obvious one is to prevent unauthorized users from seeing the content. Perhaps you want an administration panel or perhaps even a page just to keep notes and sensitive information.
Well whatever your reason for password protecting a page this tutorial will show you how to do it with the use of PHP.
Its fairly simply and there are three main steps.
1. User enteres password
2. Check if password is correct
3. Give/Deny access based on result of step 2
So then step 1 - user enteres password. This is achieved by a simple HTML form with one text input and one submit button. The user types in the password into the text box and presses the button. The code for the form is as follows:
Code
<form method="post" action="login.php">
<input type="password" name="password">
<input type="submit" name="Submit" value="Submit">
</form>
Save the above code into a file called form.html - remember the name of this. There are two things to notice in the above code which you should pay special attention to. Firstly the action of the form. It is set to "login.php". This is the file which will check to see if the password entered is correct or not. Then the name="password" part of the text input. We are giving it the name "password" because we will use this later on in login.php
Step 2 - Check if password is correct. This is where the PHP starts because at this stage we will be taking the password submitted by the user in the HTML form above and checking to see if it is correct or not. In order for the login.php file to know what was typed into the password box of the form PHP has a special variable which holds this.
Now before I continue Ill explain what a variable is. A variable is something which holds a value. Thing of it as a variable being a motorbike and its rider being a number of word. The motorbike (variable) holds the rider (value). All variables start off with the dollar symbol ($).
Ok then, lets continue...
The variable which PHP uses in this example to get the value from the form is $_POST[password].
In our script we will also be using the variable $correct which will hold what the correct password is. So lets take a look at the code:
Code
<?php
$correct = "abc";
if($_POST[password] == $correct)
{
echo "Password was correct";
} else {
echo "Password was wrong";
}
?>
Save the above code into a file called login.php
Your probably a bit confused now but heres what that code is doing. First of all it is setting the variable $correct and giving it the value of "abc". You can set variables by using the = symbol.
Then we are using whats called an If statement. It is checking to see whether the submitted password whichi is the value in $_POST[password] is equal to the value in $correct.
If it is then it will do whatever is in between the first two curly brackets. In this example it displays the sentence "Password was correct" onto the screen. If it wasnt correct though it will ignore the first two curly brackets and display whats in between the third and fourth brackets. In this example it says "Password was wrong".
Try running the script for yourself. Try typing in "abc" into the form and press submit. And then try typing in something else and press submit. You should see that the correct password is "abc".
And thats it. Thats a very basic password protection method.
User Comments
If you want to comment on this tutorial you must first register or login.
Warning: mysql_fetch_row(): supplied argument is not a valid MySQL result resource in /home/cinedict/public_html/tutorialdash/tutorialview.php on line 287
Post A Comment
Fill out the following form to comment on this tutorial.



Syndicate
Although simple this is a very good way of password protecting a page. Well done