Digital World Freelancer

The Digital World Freelancer group is applying latest Technologies Like HTML, CSS, CSS3, JavaScript, JQuery, PHP and more...... for website designing & developing... reach at our organization http://www.digitalworldfreelancer.com

Facebook Page

Monday, October 13, 2014

How to do Secure Login and Logout in PHP? and How to use Session in Login and Logout System in PHP?

it should need 7 Steps for this login-logout system, they are

Start.............
  Here i am using mysql............

1 Step) create database and table

           database name --> digitalworld
           table name --> login

please create fields in login table:  id, username, password

2 Step) connection.php

<?php
 $conn=mysql_connect("localhost","root","") or die('can not connect with localhost');
 $db  =mysql_select_db("digitalworld") or die('can not connect with database');
?>

3 Step) index.php

<?php
session_start();
?>
<?php
@$mess=$_GET['value'];
if($mess)
{ ?>
<script>
alert('Wrong Credentials...try again');
</script>
<?php } ?>
<html>
<head>
      <title>Digital World Freelancer</title>
</head>
<body>
<form method="post" name="login" action="login.php">
<label for="name" class="labelname"> Username </label>
<input type="text" name="username" id="userid" required="required" /><br />
<label for="name" class="labelname"> Password </label>
<input type="password" name="password" id="passid" required="required"  /><br />
<input type="submit" name="submit" id="submit"  value="Login" />
</form>
</body>
</html>

4 Step) login.php
 
<?php
include('connection.php');
session_start();
{
    $user=mysql_real_escape_string($_POST['username']);
    $pass=mysql_real_escape_string($_POST['password']);
    $fetch=mysql_query("SELECT id FROM `login` WHERE
                         username='$user' and password='$pass'");
    $count=mysql_num_rows($fetch);
    if($count!="")
    {
    session_register("sessionusername");
    $_SESSION['login_username']=$user;
    header("Location:dashboard.php");
    }
    else
    {
       header('Location:index.php');
    }

}
?>

5 Step) session.php
 
  <?php
include('connection.php');
session_start();
$check=$_SESSION['login_username'];
$session=mysql_query("SELECT username FROM `login` WHERE username='$check' ");
$row=mysql_fetch_array($session);
$login_session=$row['username'];
if(!isset($login_session))
{
header("Location:index.php");
}
?>

6 Step) dashboard.php

<?php
include("session.php");
?>

<h3 align="center"> Hellow <?php echo $login_session; ?></h3>
<h2 align="center" >Welcome to login system</h2>

<h4 align="center">  click here to <a href="logout.php">LogOut</a> </h4>

7 Step) logout.php
 

<?php
session_start();
if(session_destroy())
{
header("Location: index.php");
}
?>


You can try this login and logout steps now.....

No comments: