Thursday, June 13, 2013

PHP store Password in Encrypted Format During Registration.

This article it for how to store passwords in encrypted format during registration. we can to do make how to create login to by providing the right credentials. This password will be stored in the database in encrypted format, So We hope you can to help from this article. thanks

Creation of Table

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');
?>

welcome.php

<?php
session_start();
?>
<html>
<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>

login.php

<?php
session_start();
include('config.php');
?>
<html>
<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>

Success.php

<?php
session_start();
?>
<html>
<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>

registration.php 

<?php
session_start();
include('config.php');
?>
<html>
<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>

Thanks no more today ..............

0 comments:

Post a Comment