Login And Logout With Session In PHP

Introduction

This article explains login and logout with session in PHP. You will first create a database and a table named login and then create a login form with simply two fields, username and password. Then you will make a connection with your MySQL table "login" and enter some PHP code. I will use a session for authentication purposes in login and logout.

table artitecture

Example

First of all, create the "index.php" file as in the following:

<html>

<head>

<style>

a{

float:left;

text-decoration:none;

padding:0px 2px 0px;

}

h1{

color:#008844;

font-size:20px;

text-align:center;

}input{

background-color:#33FFFF;

color:#000;

}

</style>

</head>

<body>

<div style="border: 1px solid #4A0000;color:#4A0000;margin: auto;width:700px;height:100px;background-color: #CCCCCC;">

<div style="float:right;">

<a href="signup.php"><input type="submit" name="submit" value="Signup"></a>&nbsp;<a href="Login.php"><input type="submit" name="submit" value="login"></a>

</div>

</div>

<div style="border: 1px solid #4A0000;color:#4A0000;margin: auto;width:700px;height:500px;background-color: #CCCCCC;">

<h1>Welcome to my website </h1>

</div>

</body>

</html>

 

Output

home page

This is your "login.php" file.

<?php

$error="";

mysql_connect("localhost","root","")or dir(mysql_error());

mysql_select_db('demo');

session_start();

if(isset($_POST['submit'])){

$username = $_POST['username'];

$password = $_POST['password'];

if(isset($username) && isset($password)){

$query="SELECT id FROM login WHERE username='$username' and password='$password'";

$result=mysql_query($query);

$row=mysql_fetch_array($result);

$id=$row['id'];

$count=mysql_num_rows($result);

if($count==1)

{

//session_register("username");

$_SESSION['name']=$username;

 

header("location: welcome.php");

}else{

$error = 'please enter username and password';

}

}

}

?>

<!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>login</title>

<style>

input{

color:#4A0000;

border:1px solid #4A0000;

}

</style>

</head>

<body>

<div style="  background-color: #CCCCCC;border: 1px solid #4A0000;color: #4A0000; margin: auto;padding: 19px 0 18px;width: 295px;">

<div style="color:#C23D29;padding: 0 10px 15px 38px;"><?php echo "$error"; ?></div>

<form name="login" action="" method="post">

<div style="text-align:center;">Username :&nbsp;<input type="text" name="username"></div></br>

<div style="text-align:center;">Password :&nbsp;<input type="password" name="password"></div></br>

<div style="text-align:center;"><input type="submit" name="submit" value="login" style="margin: 0 -64px 2px 115px;"></div>

</form>

</div>

</body>

</html>

 

Output

when submit blank form

login form

 

This is your "secure.php" file.

<?php

mysql_connect("localhost","root","")or dir(mysql_error());

mysql_select_db('demo');

session_start();

$check=$_SESSION['name'];

$query=mysql_query("select username from login where username='$check' ");

$data=mysql_fetch_array($query);

$user=$data['username'];

if(!isset($user))

{

header("Location: login.php");

}

?>

This is your "welcome.php" file.

 

<html>

<head>

<style>

a{

float:left;

text-decoration:none;

}h1{

color:#008844;

font-size:20px;

text-align:center;

}

</style>

</head>

<body>

<div style="border: 1px solid #4A0000;color: #4A0000; margin: auto;width: 700px;">

<div style="float:right;">

<?php

include('secure.php');

if($_SESSION['name']){

echo '<a href="logout.php"><input type="submit" name="submit" value="logout"></a>';

}else{

echo '<a href="login.php"><input type="submit" name="submit" value="login"></a>';

}

?>

</div>

<h1>Welcome to the my secret area <?php echo $user; ?></h1>

</div>

</body>

</html>

 

Output

logout form

 

When you will click the on "logout.php" button your session will be clean and redirect to the login page.

 

<?php

session_start();

if(session_destroy())

{

header("Location: login.php");

}

?>

Output

click logout


Similar Articles