login and logout


Create HTML form for user register form. the file name (form.html)

<html>
<body>

<form action=”register.php” method=”post” name=”myform” onsubmit=”return validation()”>
<table border=’1’bgcolor=#99999>
<tr> <td>username</td><td><input type=”text” name=”username” id=”username”></td></tr>
<tr> <td>password</td><td><input type=”password” name=”pw” id=”pw”></td></tr>
<tr> <td>fname</td><td><input type=”text” name=”fname” id=”fname”></td></tr>
<tr> <td>email</td><td><input type=”text” name=”email” id=”email”></td></tr>
<tr> <td><input type=”submit”></td></tr>

</form>
</table>
</body>
</html>

 

Create  user Register  form in database. the file name (register.php)

<?php
mysql_connect(“localhost”,”root”,””);
mysql_select_db(“ratnesh”);
mysql_query(“INSERT INTO login(fname,username,password,email) VALUES(‘$_POST[fname]’,’$_POST[username]’,’$_POST[pw]’,’$_POST[email]’)”) or die(“cannot execute the query”);
echo “user registered”;

?>

Create php code for connectivity for database . the file name (config.php)

<?php

$hostname = ‘localhost’;        // Your MySQL hostname. Usualy named as ‘localhost’, so you’re NOT necessary to change this even this script has already online on the internet.
$dbname   = ‘ratnesh’; // Your database name.
$username = ‘root’;             // Your database username.
$password = ”;                 // Your database password. If your database has no password, leave it empty.

// Let’s connect to host
mysql_connect($hostname, $username, $password) or DIE(‘Connection to host is failed, perhaps the service is down!’);
// Select the database
mysql_select_db($dbname) or DIE(‘Database name is not available!’);

?>

Create Login pages and form . the file name is (login.php)

<?php

// Inialize session
session_start();

// Check, if user is already login, then jump to secured page
//if (isset($_SESSION[‘username’])) {
if( isset($username) && isset($password) && $username !== ” && $password !== ” ) {

header(‘Location: securedpage.php’);
}

?>
<html>

<head>
</head>

<body>

<h3>User Login</h3>

<table border=”0″>
<form method=”POST” action=”loginproc.php”>
<tr><td>Username</td><td>:</td><td><input type=”text” name=”username” size=”20″></td></tr>
<tr><td>Password</td><td>:</td><td><input type=”password” name=”password” size=”20″></td></tr>
<tr><td>&nbsp;</td><td>&nbsp;</td><td><input type=”submit” value=”Login”></td></tr>
</form>
</table>

</body>

</html>

 Create code for loginprocess . the file name is (loginpro.php)

<?php

// Inialize session
session_start();

// Include database connection settings
include(‘config.php’);
//$username = $_POST[‘username’]
//$password = $_POST[‘password’]

// Retrieve username and password from user database according to user’s input
$login = mysql_query(“SELECT * FROM login WHERE (username = ‘” . mysql_real_escape_string($_POST[‘username’]) . “‘)
and (password = ‘” . mysql_real_escape_string(($_POST[‘password’])) . “‘)”);

// Check username and password match
if (mysql_num_rows($login) == 1) {
// Set username session variable
$_SESSION[‘username’] = $_POST[‘username’];

// Jump to secured page
header(‘Location: securedpage.php’);
}
else {
// Jump to login page
header(‘Location: login.php’);
}

?>

Create code for secured pages for login pages . the file name is (securedpages.php)

<?php

// Inialize session
session_start();

// Check, if username session is NOT set then this page will jump to login page
if (!isset($_SESSION[‘username’])) {
header(‘Location: login.php’);
}

?>
<html>

<head>
<title>Secured Page</title>
</head>

<body>

<p>This is secured page with session: <b><?php echo $_SESSION[‘username’]; ?></b>
<br>You can put your restricted information here.</p>
<p><a href=”logout.php”>Logout</a></p>

</body>

</html>

The create log-out pages . the file name is (logout.php)

<?php

// Inialize session
session_start();

// Delete certain session
unset($_SESSION[‘username’]);
// Delete all session variables
// session_destroy();

// Jump to login page
header(‘Location: login.php’);

?>

How to forget password

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”&gt;
<html xmlns=”http://www.w3.org/1999/xhtml”&gt;
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=utf-8″ />
<title>Untitled Document</title>
<link href=”Style.css” rel=”stylesheet” type=”text/css” />
<script type=”text/javascript”>
function validateForm()
{
var x=document.forms[“login”][“username”].value;
if (x==null || x==””)
{
alert(“Please enter your E-Mail”);
return false;
}
}
</script>
</head>

<body>
<div>
<div>
<h1>Next Algorithm</h1>
</div>
</div>
<div id=”content”>
<div>
<h1 align=”center”>CHANGE PASSWORD

<form method=”post” action=”#” name=”login” onsubmit=”return validateForm()”>
<table align=”center” border=”1px”>
<tr>
<td>Username</td>
<td> <input type=”email” name=”username” style=”height:30px;” </td>
</tr>
<tr>
<td> New Password</td>
<td> <input type=”password” name=”password” style=”height:30px;”  /></td>
</tr>
<tr>
<td>Confirm password</td>
<td> <input type=”password” name=”confirm” style=”height:30px;”  /></td>
</tr>
<td colspan=”2″  align=”center”> <input type=”submit” name=”submit” value=”submit” style=”width:110px; height:50px;” /></td>
</table>
</form>
<?php
mysql_connect(“localhost”, “root”, “”)or die(“cannot connect”);
mysql_select_db(“loginform”)or die(“cannot select DB”);
if(isset($_POST[‘submit’]))

{
$username=$_POST[‘username’];
$password=$_POST[‘password’];
$confirm=$_POST[‘confirm’];
if($password==$confirm)
{
$sql=”UPDATE record SET password=’$password’ WHERE username=’$username'”;
$result=mysql_query($sql);

echo “Password successfully changed!”;
} else {
echo “Please Enter Correct Password”;
}

}

mysql_close();
?>
</h1>
</div>
</div>
<div>
<p style=”text-align:center; font-size:20px; color:#996699;”>Powered By Next Algorithm</p></div>
</div>
</body>
</html>

 

 

Leave a comment