Change Password in PHP

Introduction

In this article we will learn how to change a password of a registered user. The registered user must login with his/her login credentials (user name and password). After successful login a Change Password link will be visible. Here by clicking the link a new page will appear where the user must enter the new password and confirm the password and then click on the update button to change his/her password respectively.

Table structure

-- phpMyAdmin SQL Dump
-- version 2.10.1
-- http://www.phpmyadmin.net
--
-- Host: localhost
-- Generation Time: Jun 04, 2012 at 05:16 PM
-- Server version: 5.0.45
-- PHP Version: 5.2.5

SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";

--
-- Database: `Work`
--

-- --------------------------------------------------------

--
-- Table structure for table `user`
--

CREATE TABLE `user` (
`id` int(11) NOT NULL auto_increment,
`name` varchar(255) NOT NULL,
`address` varchar(255) NOT NULL,
`area` varchar(255) NOT NULL,
`city` varchar(255) NOT NULL,
`pin` int(11) NOT NULL,
`state` varchar(255) NOT NULL,
`country` varchar(255) NOT NULL,
`mobile` varchar(50) NOT NULL,
`email` varchar(255) NOT NULL,
`user` varchar(255) NOT NULL,
`password` varchar(200) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;

--
-- Dumping data for table `user`
--

INSERT INTO `user` (`id`, `name`, `address`, `area`, `city`, `pin`, `state`, `country`, `mobile`, `email`, `user`, `password`) VALUES
(1, 'Raj', 'New Ag colony plot no 4 block 3', 'Nayapalli', 'Bhubaneswar', 751013, 'Orissa', 'India', '9987654321', '[email protected]', 'Raj Kumar', 'raj123'),
(2, 'Ravi', 'Bsnl colony plot no 6 block 4', 'Sahid Nagar', 'Bhubaneswar', 751012, 'Orissa', 'India', '9876543210', '[email protected]', 'Ravi Kumar', 'ravi123');

Now let's move to the coding part.

For creating a database connection:

config.php

<?php

$sDbHost = 'localhost';
$sDbName = 'Work';
$sDbUser = 'root';
$sDbPwd = '';

$dbConn = mysql_connect ($sDbHost, $sDbUser, $sDbPwd) or die ('MySQL connect failed. ' . mysql_error());
mysql_select_db($sDbName,$dbConn) or die('Cannot select database. ' . mysql_error());

?>

Login.php

<?php
session_start();
include("config.php");
if($_REQUEST["btn_login"]=="Login")
{
$uname=$_POST["txt_login"];
$password=$_POST["txt_pwd"];
$sql="select * from user where user='".$uname."' and password='".$password."'";
//echo $sql;
//exit;
$res=mysql_query($sql);
if($rs=mysql_num_rows($res)>0)
{
$_SESSION["uname"]=$uname;
//echo $_SESSION["uname"];

echo "<script>location.href='welcome.php'</script>";
}
else
{
echo "<script>location.href='Login.php?qs=invalid'</script>";
}
}
?>
<script language="javascript" type="text/javascript">
function validate()
{
if(document.frm_login.txt_login.value=="")
{
alert("Please Enter user name");
document.frm_login.txt_login.focus();
return false;
}
if(document.frm_login.txt_pwd.value=="")
{
alert("Please Enter password");
document.frm_login.txt_pwd.focus();
return false;
}
return true;
}
</script>
<form name="frm_login" method="POST" action="Login.php" onSubmit="return validate();">
<table border="0" width="100%" cellspacing="0" cellpadding="0">
<tr>
<td colspan="2" width="100%" align="center"><br><br><b><font size=3>LOGIN FORM</font></b>&nbsp;
<p>&nbsp;</p>
</tr>
<tr>
<td width="20%" align="center"><img src="img/key_img.jpg"></td>
<td width="75%" height="200">
<table border="0" width="100%" cellpadding="4">
<tr>
<td colspan="2" align="center"></td>
</tr>
<tr>
<td colspan="2" align="center"><font color="#EB3C37"></font></td>
</tr>
<tr>
<td colspan="2" align="center"><br></td>
</tr>
<?php
if($_REQUEST["qs"]=="invalid")
{
echo "<strong>Invalid Username or Password</strong>";
}
?>
<tr>
<td width="30%" align="right">Username:</td>
<td width="70%"><input type="text" name="txt_login" autocomplete="off"></td>
</tr>
<tr>
<td align="right">Password:</td>
<td><input type="password" name="txt_pwd"></td>
</tr>
<tr>
<td>&nbsp;</td>
<td><input type="submit" name="btn_login" value="Login"></td>
</tr>
</table>
<tr>
<td>&nbsp;</td>

</tr>
</td>
</tr>
</table>
</form>

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="post" name="welcome">
<?php echo '<b>Welcome:</b>'.$_SESSION["uname"]; ?>
</form>
<a href="Changepassword.php">Changepassword</a>
</body>
</html>

Changepassword.php

<?php
session_start();
include("config.php");
if($_REQUEST["Submit"]=="Update")
{
$sql="update user set password ='$_REQUEST[newpassword]' where user='$_SESSION[uname]'";
//echo $sql;
mysql_query($sql);
header("Location:Changepassword.php?msg=updated");
}


?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD><TITLE>Change password</TITLE>
<script language="javascript" type="text/javascript">
function validate()
{

var formName=document.frm;

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


if(formName.cpassword.value == "")
{
document.getElementById("cpassword_label").innerHTML='Enter ConfirmPassword';
formName.cpassword.focus();
return false;
}
else
{
document.getElementById("cpassword_label").innerHTML='';
}


if(formName.newpassword.value != formName.cpassword.value)
{
document.getElementById("cpassword_label").innerHTML='Passwords Missmatch';
formName.cpassword.focus()
return false;
}
else
{
document.getElementById("cpassword_label").innerHTML='';
}
}
</script>
<style type="text/css">
<!--
.style1 {font-weight: bold}
.style7 {
color: yellow;
font-size: 24px;
}
.style9 {
color: #FF6666;
font-weight: bold;
}
.style12 {
color: #666666;
font-weight: bold;
}
.style14 {color: #CC0033; font-weight: bold; }
-->
</style>
<META http-equiv=Content-Type content="text/html; charset=windows-1252">
</HEAD>
<BODY>

<form action="Changepassword.php" method="post" name="frm" id="frm" onSubmit="return validate();">
<table width="47%" border="1" cellspacing="0" cellpadding="0">
<tr>
<td colspan="2" align="center"></td>
</tr>
<tr bgcolor="#666666">
<td colspan="2"><span class="style7">Change Password</span></td>
</tr>
<?php if($_REQUEST[msg]=="updated") { ?>
<tr bgcolor="#666666">
<td colspan="2"><span class="style7">Password has been changed successfully.</span></td>
</tr>
<?php } ?>
<tr>
<td bgcolor="#CCCCCC"><span class="style14">New Password:</span></td>
<td bgcolor="#CCCCCC"><input type="password" name="newpassword" id="newpassword" size="20" autocomplete="off"/>&nbsp; <label id="newpassword_label" class="level_msg"></td>
</tr>
<tr>
<td bgcolor="#CCCCCC"><span class="style14">Confirm Password:</span></td>
<td bgcolor="#CCCCCC"><input type="password" name="cpassword" id="cpassword" size="20" autocomplete="off">&nbsp; <label id="cpassword_label" class="level_msg"></td>
</tr>

<tr bgcolor="#666666">
<td colspan="2" align="center"><input type="submit" name="Submit" value="Update" onSubmit="return validate();"/></td>
</tr>

</table>
<a href="Login.php">Login</a>
</form>
</BODY>
</HTML>

Output

PWDPHP1.jpg


Provide valid login details

PWDPHP2.jpg

After successful login

PWDPHP3.jpg

Change the old password by providing new password

PWDPHP4.jpg

Password changed successfully

PWDPHP5.jpg
Conclusion

So here we learned how to change the password in PHP.
 


Similar Articles