How to create user login, forgot password and logout page using php script

phpimg

To create a login page in php

Note: Create a table tbl_login with uname and pwd
//config.inc.php

<?php
class Config {
public $dbHostname = ‘localhost’;
public $dbUsername = ‘root’;
public $dbPassword = ‘xxx’;
public $dbName = ‘dbname’;
}

?>

//index.php

<?php
session_start();
include(“config.inc.php”);
class Login
{
public $strUsername = null;
private $strPassword = null;
public $arrErrors = array();
private $objConfig = null;
private $objDbConn = null;

function __construct()
{
$this->objConfig = new Config();
}

function processRequest()
{
$this->strUsername = trim($_POST[“uname”]);
$this->strPassword = trim($_POST[“pwd”]);
}

function validateRequest()
{
$blnValidationSuccess = true;
if($this->strUsername == “”) {
$this->arrErrors[“uname”] = “Username is required”;
$blnValidationSuccess = false;
}

if($this->strPassword == “”) {
$this->arrErrors[“pwd”] = “Password is required”;
$blnValidationSuccess = false;
}
if($blnValidationSuccess) {
$objResult = mysql_query(“select * from tbl_login where uname='” . $this->strUsername . “‘ and pwd = ‘” . $this->strPassword . “‘ ” ,$this->objDbConn);
if(mysql_num_rows($objResult)<=0) {
$this->arrErrors[‘usernameorpassword’] = “Username or Password is wrong”;
$blnValidationSuccess = false;
}
}
return $blnValidationSuccess;
}

function closeDbConnection()
{
mysql_close($this->objDbConn);
}

function run()
{
if(isset($_POST[“issubmit”])) {
$this->processRequest();
$this->validateRequest();
//connect to db
$this->objDbConn = mysql_connect($this->objConfig->dbHostname, $this->objConfig->dbUsername, $this->objConfig->dbPassword);
mysql_select_db($this->objConfig->dbName);
$this->closeDbConnection();
}
}
}
$objLogin = new Login();
$objLogin->run();
?>
<html>
<head>
<title> Login Page</title>
</head>
<body>
<div>
<div>
<form action=”index.php” method=”post”>
<input type=”hidden” name=”issubmit” value=”true” />
<?php
if(isset($objLogin->arrErrors[‘usernameorpassword’])) {
echo “<div style=’color: #FF0000′>”. $objLogin->arrErrors[‘usernameorpassword’] . “</div>”;
}
?>
User Name<input type=”text” name=”uname” value=”<?php echo $objLogin->strUsername; ?>” />
<?php
if(isset($objLogin->arrErrors[‘uname’])) {
echo “<div style=’color: #FF0000′>”. $objLogin->arrErrors[‘uname’] . “</div>”;
}
?>
<br/>
Password <input type=”password” name=”pwd” value=”” />
<?php
if(isset($objLogin->arrErrors[‘pwd’])) {
echo “<div style=’color: #FF0000′>”. $objLogin->arrErrors[‘pwd’] . “</div>”;
}
?>
<br />
<input type=”submit” name=”submit” value=”submit” />
</form>
</div>
</body>
</html>

To create a logout page in php

//logout.php

<?php
session_start();
session_destroy();
?>
<html>
<title>Logout</title>
<body>
<h3> You are successfully Logged out</h3>
<a href=’index.php’> CLICK HERE TO LOGIN AGAIN </a>
</body>
</html>