Very Simple Password Protection Script by Adrian In this tutorial I will teach you how to make a very simple text based password protection script.
View a demo
here, use the username
user and the password
demo.
I will first give you all the files then I'll explain them.
form.html
CODE
<html>
<head>
<title>Login</title>
</head>
<body>
<form name="login" method="post" action="protected.php">
Username:
<input name="formuser" type="text" id="formuser">
<br>
Password:
<input name="formpass" type="password" id="formpass">
<br>
<input type="submit" name="Submit" value="Submit">
<input type="reset" name="Reset" value="Reset">
</form>
</body>
</html>
protected.php
CODE
<?php
$user = "your_username_here";
$password = "your_password_here";
$formuser = $_POST["formuser"];
$formpass = $_POST["formpass"];
if(($formuser == $user) && ($formpass == $password)){
//Protected Content here
}
else {
echo("Login Incorrect, Please click back and try again");
}
?>
I wont explain form.html because its a simple html form but I will explain protected.php.
CODE
$user = "user";
$password = "demo";
This is the code which specifies the username and password. You cannot view these if you go to the protected.php file and press View Source.
CODE
$formuser = $_POST["formuser"];
$formpass = $_POST["formpass"];
These two get the information from the form and puts it into two different variables.
CODE
if(($formuser == $user) && ($formpass == $password)){
This checks to see if the username from the form is the same as the variable specified above and the same for the password.
CODE
}
else {
echo("Login Incorrect, Please click back and try again");
}
The first } closes the if statement. The rest says what will happen if the login is incorrect. In this case the script will write, Login Incorrect, Please click back and try again.
Hope you found this useful. Its a simple tutorial but is useful for small scripts and could be modified to use to cookies for multiple pages.
-Adrian
EDIT: This is an old tutorial. I have made a new one with slightly more features (support for multiple pages, cookies) but it is still a nice simple script. To view it please go to
this topic
This post has been edited by Adrian: May 4 2006, 11:43 AM