Topic: [Tutorial] How to make a Password Protected Page

Hello,

here is a tutorial on how to make a password protected page

demo: http://joeyelectric.com/demos/password.php

=========================================
In this tutorial we will use the crypt(); command to encrypt our passwords

files?
-password.php
==========================================

First open a new document in a text editor I use Notepad ++ (Free Opensource text editor)
Now Put this code in their

<html>
<head>
<title> Password Test </title>
</head>
<body>
<center>
<h1> Password Test </h1>

</body>
</html>

Next we will create the form:
you will notice that for the action="" we have used "<?php echo $_SERVER['PHP_SELF']; ?>" this will make it so that all the form is processed in password.php

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<input type="text" name="password">
<input type="submit" name="enter" value="submit">
</form>

now we will add the functions to the script

<?php

if(isset($_GET['enter'])){

$user_input = $_POST['password']; //This is the password the user typed
$user_input_crypt = crypt($user_input); //This will encrypt the password
$password = crypt('mypassword'); //This is the actual password before it is encrypted about to be encrypted


if (crypt($user_input, $password) == $password) { // This determines whether the passwords match or not 
   echo "<font color='green'>Correct Password!</font>"; // if the password is correct this is displayed
} else {

  echo "<font color='red'>Incorrect Password!</font><br><br>";
  echo "The Password you typed came back as: $user_input_crypt <br> it should have been: $password"; // if not this is displayed
  
  }

} else {

$password = crypt('sc55207');




?>

Now here is the code all put together:

<html>
<head>
<title> Password Test </title>
</head>
<body>
<center>
<h1> Password Test </h1>


<?php

if(isset($_GET['enter'])){

$user_input = $_POST['password'];
$user_input_crypt = crypt($user_input);
$password = crypt('sc55207');


if (crypt($user_input, $password) == $password) {
   echo "<font color='green'>Correct Password!</font>";
} else {

  echo "<font color='red'>Incorrect Password!</font><br><br>";
  echo "The Password you typed came back as: $user_input_crypt <br> it should have been: $password";
  
  }

} else {

$password = crypt('sc55207');




?>

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<input type="text" name="password">
<input type="submit" name="enter" value="submit">
</form>
</body>
</html>

Thumbs up