Store Password in Encrypted Format During Registration in PHP

In this article we will learn how to store passwords in encrypted format during registration. We can also login to it by providing the correct credentials. Here the password will be stored in the database in an encrypted format so that no one is able to determine the password when he/she opens the database table.

Table Creation

-- phpMyAdmin SQL Dump
-- version 2.10.1
-- http://www.phpmyadmin.net
--
-- Host: localhost
-- Generation Time: Feb 13, 2013 at 05:52 AM
-- Server version: 5.0.45
-- PHP Version: 5.2.5
SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";
--
-- Database: `home_work`
--
-- --------------------------------------------------------
--
-- Table structure for table `employee`
--
CREATE TABLE `employee` (
`id` int(12) NOT NULL auto_increment,
`name` varchar(100) default NULL,
`address` varchar(100) NOT NULL,
`password` varchar(50) default NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `name` (`name`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=2 ;
--
-- Dumping data for table `employee`
--
INSERT INTO `employee` (`id`, `name`, `address`, `password`) VALUES
(1, 'Raj', 'Pune', 'cac5ff630494aa784ce97b9fafac2500');

Note

Here the password "raj123" is stored in encrypted format.

Now let's move to the code.

Config.php

<?php
$DBHOST = "localhost";
$DBNAME = "home_work";
$DBUSER = "root";
$sLink = mysql_connect($DBHOST,$DBUSER,'') or die('Connection with MySql Server failed');
mysql_select_db($DBNAME, $sLink) or die('MySql DB was not found');
?>

registration.php

<?php
session_start();
include('config.php');
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Employee Registration</title>

<link href="style.css" rel="stylesheet" type="text/css" />
<script language="javascript" type="text/javascript">

function validation()
{
               var formName=document.frm;
               if(formName.name.value == "")
               {
                              document.getElementById("name_label").innerHTML='Please Enter Name';
                              formName.name.focus();
                              return false;
               }
               else
               {
                              document.getElementById("name_label").innerHTML='';
               }

               if(formName.address.value == "")
               {
                              document.getElementById("address_label").innerHTML='Please Enter Address';
                              formName.address.focus();
                              return false;
               }
               else
               {
                              document.getElementById("address_label").innerHTML='';
               }

               if(formName.password.value == "")
               {
                              document.getElementById("password_label").innerHTML='Please Enter Password';
                              formName.password.focus();
                              return false;
               }
               else
               {
                              document.getElementById("password_label").innerHTML='';
               }
}

</script>
</head>

<?php
if($_REQUEST["action"]=='register')
{
$name=mysql_real_escape_string($_POST['name']);
$address=mysql_real_escape_string($_POST['address']);
$password=mysql_real_escape_string($_POST['password']);
$password=md5($password); // Encrypted Password
$sql="Insert into employee(name,address,password) values('$name','$address','$password');";
$result=mysql_query($sql);
echo "Registration Successfully Completed";
echo "<script>window.location.href='Success.php'</script>";
}
?>
<body>
 
<form method="post" id="frm" name="frm" action="registration.php?action=register" onSubmit="return validation();">
<table width="500" border="0">
<tr>
 <td class="deepbluetextbold"><b>Employee Registration</b></td>
 </tr>
  <tr>
    <td class="colouredCell">Name*</td>
    <td>
      <input type="text"  name="name" id="fullname" autocomplete="off" value="<?php echo $_REQUEST[name]; ?>"/>&nbsp; <label id="name_label" class="level_msg"></label>    </td>
  </tr>
  <tr>
    <td class="colouredCell">Address*</td>
    <td><input type="text" name="address"  id="email" autocomplete="off" value="<?php echo $_REQUEST[address]; ?>"/>&nbsp; <label id="address_label" class="level_msg"></label></td>
  </tr>
  <tr>
    <td class="colouredCell">Password*</td>
    <td><input type="password" name="password" id="password" autocomplete="off"/>&nbsp; <label id="password_label" class="level_msg"></td>
  </tr>
</table>
  </tr>
  <tr>
    <td>
      <input type="submit" name="Submit" value="Register" />   </td>
    <td>&nbsp;</td>
  </tr>
</table>
</form>
</body>
</html>


Success.php

<?php
session_start();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Success</title>
</head>
<body>
<form action="Success.php" method="post" name="Success">
<b>Registration Successfully Completed</b><br/>
<a href="login.php">Login?</a>
</form>
</body>
</html>

login.php

<?php
session_start();
include('config.php');
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Login</title>
<link href="style.css" rel="stylesheet" type="text/css" />
<script language="javascript" type="text/javascript">
 
function validation()
{
               var formName=document.frm;
               if(formName.name.value == "")
               {
                              document.getElementById("name_label").innerHTML='Please Enter Name';
                              formName.name.focus();
                              return false;
               }
               else
               {
                              document.getElementById("name_label").innerHTML='';
               }
 
               if(formName.password.value == "")
               {
                              document.getElementById("password_label").innerHTML='Please Enter Password';
                              formName.password.focus();
                              return false;
               }
               else
               {
                              document.getElementById("password_label").innerHTML='';
               }
}

</script>
</head>

<?php

if($_REQUEST["action"]=='login')
{
$_SESSION["name"]=$_POST["name"];
$name=mysql_real_escape_string($_SESSION["name"]);
$password=mysql_real_escape_string($_POST['password']);
$password=md5($password); // Encrypted Password
$sql="SELECT id FROM employee WHERE name='$name' and password='$password'";
$result=mysql_query($sql);
$count=mysql_num_rows($result);

if($count==1)
{
header("location: welcome.php");
}
else
{
echo "Your Login Name or Password is invalid";
}
}

?>
<body>
<form method="post" id="frm" name="frm" action="login.php?action=login" onSubmit="return validation();">
<table width="500" border="0">
<tr>
 <td class="deepbluetextbold"><b>Login</b></td>
 </tr>
  <tr>
    <td class="colouredCell">Name*</td>
    <td>
      <input type="text"  name="name" id="fullname" autocomplete="off" value="<?php echo $_REQUEST[name]; ?>"/>&nbsp; <label id="name_label" class="level_msg"></label>    </td>
  </tr>
  <tr>
    <td class="colouredCell">Password*</td>
    <td><input type="password" name="password" id="password" autocomplete="off"/>&nbsp; <label id="password_label" class="level_msg"></td>
  </tr>
</table>
  </tr>
  <tr>
    <td>
      <input type="submit" name="Submit" value="login" />   </td>
    <td>&nbsp;</td>
  </tr>
</table>
</form>
</body>
</html>

welcome.php

<?php
session_start();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Welcome</title>
</head>
<body>
<form action="welcome.php" method="get" name="welcome">
<?php echo '<b>Welcome:::</b>'.$_SESSION["name"]; ?>
</form>
</body>
</html>

Running the application

Run the WampServer, then write the following line in the URL:

http://localhost/Register login Encrypt Decrypt Php/

PHP1.jpg

Provide data for registration

PHP2.jpg

After providing the data, control moves to the success page

PHP3.jpg

Click the login link and provide the correct credentials for login

PHP4.jpg

After providing the correct credentials

PHP5.jpg


Similar Articles