Simple Login, Logout In PHP
Step :1. Make
database of sessions in phpmyadmin.
Step:2. Create
table sessions and users
Step:3. Create
columns in sessions (session_id, userid, username, ip_addr,
session_start, session_end).
Create
columns in users (id,username,password).
Step:4. Make file connect.php.
<?php
$mysql_host
= 'localhost';
$mysql_user
= 'root';
$mysql_password
= '';
$mysql_database
= 'sessions';
mysql_connect($mysql_host,
$mysql_user, $mysql_password);
mysql_select_db($mysql_database);
?>
Step:5 Make file login.php.
<?php
error_reporting(0);
session_start();
$sid
= session_id();
$userid = $_SESSION['userid'];
$username = $_SESSION['username'];
?>
<!DOCTYPE html>
<html lang = "en">
<head>
<meta charset = "utf-8">
<title>Login</title>
</head>
<body>
<?php
if ($userid && $username) {
echo "You are already logged in as
<b>$username</b>. Do you wish to
<a href
='./logout.php'>logout</a>?";
}
Else
{
$form = "<form action = './login.php' method = 'POST'>
<table>
<tr>
<td>Username:</td>
<td><input type = 'text' name = 'user'
/></td>
</tr>
<tr>
<td>Password:</td>
<td><input type = 'password' name = 'password'
/></td>
</tr>
<tr>
<td></td>
<td><input type = 'submit' name = 'loginbtn'
value = 'Login'/></td>
</tr>
</table>
</form>";
if ($_POST['loginbtn']) {
$user = $_POST['user'];
$password = $_POST['password'];
if ($user) {
if ($password) {
require("connect.php");
$query = "SELECT * FROM users WHERE username =
'$user'";
$query_run = mysql_query($query);
$numrows = mysql_num_rows($query_run);
if ($numrows == 1) {
$row = mysql_fetch_assoc($query_run);
$dbid = $row['id'];
$dbuser = $row['username'];
$dbpass = $row['password'];
if ($password == $dbpass) {
$_SESSION['userid'] =
$dbid;
$_SESSION['username'] =
$dbuser;
$http_client_ip = $_SERVER['HTTP_CLIENT_IP'];
$http_x_fowarded_for = $_SERVER['HTTP_X_FOWARDED_FOR'];
$remote_addr = $_SERVER['REMOTE_ADDR'];
if (!empty($http_client_ip)) {
$ip = $http_client_ip;
}
elseif
(!empty($http_x_fowarded_for)) {
$ip = $http_x_fowarded_for;
}
else
{
$ip = $remote_addr;
}
$time = time();
$actual_time = date('d M Y @ H:i:s', $time);
mysql_query("INSERT INTO sessions
VALUES ('$sid', '$dbid', '$dbuser', '$ip', '$actual_time', '')");
echo "You have been logged in as
<b>$dbuser</b>. Click <a href = './logout.php'>here<a>
to logout.";
}
else
{
echo "You did not enter the correct password.
$form";
}
}
else
{
echo "The username: <b>$user</b> was not
found. $form";
}
mysql_close();
}
else
{
echo "You must enter your password. $form";
}
}
else
{
echo "You must enter your username. $form";
}
}
else
{
echo $form;
}
}
?>
</body>
</html>
Step:6 Make file logout.php.
<?php
error_reporting(0);
session_start();
$sid
= session_id();
$userid = $_SESSION['userid'];
$username = $_SESSION['username'];
?>
<!DOCTYPE html>
<html lang = "en">
<head>
<meta charset = "utf-8">
<title>Logout</title>
</head>
<body>
<?php
$time = time();
$actual_time = date('d M Y @ H:i:s', $time);
if ($userid && $username) {
session_destroy();
require("./connect.php");
mysql_query("UPDATE sessions
SET session_end = '$actual_time' WHERE session_id = '$sid' AND session_end =
''");
header( 'Location: ./login.php' ) ;
mysql_close();
}
else
{
echo "You are not logged in.";
}
?>
</body>
</html>
3 comments:
Fair enough for a beginner.
very good information is provided about login form ....thank you sir
clearly understand each step....so more grow information
Post a Comment