This tutorial will tell you how to username and password a page.
Here is the code. I shall walk you through it.
- Code: Select all
<?php
/* Title: password.php
* Author: DooBDee
* Website: http://www.doobdee.net
* Description: Required a username and password to view certain elements
*/
if ($_POST['submit'])
{
// Your Username
$username_value = '';
// Your Password :: Use md5 encryption
$password_value = '';
$username = isset($_POST['username']) ? stripslashes($_POST['username']) : '';
$password = isset($_POST['password']) ? md5(stripslashes($_POST['password'])) : '';
if ($username === $username_value)
{
if ($password === $password_value)
{
echo 'Username And Password Correct'; // Show what you want when log-in is complete
}
else
{
echo 'Your Username or password is incorrect'; // Show error message if incorrect
}
}
else
{
echo 'Your Username or password is incorrect'; // Show error message if incorrect
}
exit;
}
?>
<html>
<head>
<title>Username and password required for page view</title>
</head>
<body>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data">
<p>Enter your username and password:</p>
<input name="username" value="" type="text"><br />
<input name="password" value="" type="password"><br />
<input name="submit" value="View Page" type="submit">
</body>
</html>
When the page loads,
- Code: Select all
if ($_POST['submit'])
- Code: Select all
<html>
<head>
<title>Username and password required for page view</title>
</head>
<body>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data">
<p>Enter your username and password:</p>
<input name="username" value="" type="text"><br />
<input name="password" value="" type="password"><br />
<input name="submit" value="View Page" type="submit">
</body>
</html>
The form action, namely this part:
- Code: Select all
<form action="<?php echo $_SERVER['PHP_SELF']; ?>"
Firstly it looks at:
- Code: Select all
// Your Username
$username_value = '';
// Your Password :: Use md5 encryption
$password_value = '';
And set it to those variables. To get md5 encryption for a password, go to this site: http://pajhome.org.uk/crypt/md5/
The script then goes on to set the data filled out by the form to a variable, using stripslashes and isset and md5 for the password as shown below:
- Code: Select all
$username = isset($_POST['username']) ? stripslashes($_POST['username']) : '';
$password = isset($_POST['password']) ? md5(stripslashes($_POST['password'])) : '';
Once this has been done, it compares the username and the username value and compares them if the two values are equal in both value and data type. (notice the ===).
- Code: Select all
if ($username === $username_value)
- Code: Select all
if ($password === $password_value)
- Code: Select all
echo "Username And Password Correct"; // Show what you want when log-in is complete
I hope this has been helpful.
Kind regards,
DoobDee




