Login logout session in php





In this login logout example in PHP we used 3 file

  • login.php
  • index.php
  • logout.php

login_user.sql

CREATE TABLE `login_user` (
  `id` int(11) NOT NULL,
  `name` varchar(60) NOT NULL,
  `user_name` varchar(20) NOT NULL,
  `password` varchar(20) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

login.php

0) {
        $con = mysqli_connect('127.0.0.1:3306','root','','admin') or die('Unable To connect');
        $result = mysqli_query($con,"SELECT * FROM login_user WHERE user_name='" . $_POST["user_name"] . "' and password = '". $_POST["password"]."'");
        $row  = mysqli_fetch_array($result);
        if(is_array($row)) {
        $_SESSION["id"] = $row['id'];
        $_SESSION["name"] = $row['name'];
        } else {
         $message = "Invalid Username or Password!";
        }
    }
    if(isset($_SESSION["id"])) {
    header("Location:index.php");
    }
?>


User Login


Enter Login Details

Username:

Password:


index.php



User Login




Welcome . Click here to Logout.
Please login first .";
?>


logout.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.

Login logout session in php

Example

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

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

Welcome to my website

Output

Login logout session in php

This is your "login.php" file.

$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';

}

}

}

?>

"-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

"http://www.w3.org/1999/xhtml">

"Content-Type" content="text/html; charset=iso-8859-1" />

login

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

"color:#C23D29;padding: 0 10px 15px 38px;">"$error"; ?>

"login" action="" method="post">

"text-align:center;">Username : "text" name="username">

"text-align:center;">Password : "password" name="password">

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

Output

Login logout session in php

Login logout session in php

This is your "secure.php" file.

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.

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

"float:right;">

include('secure.php');

if($_SESSION['name']){

echo '';

}else{

echo '';

}

?>

Welcome to the my secret area

Output

Login logout session in php

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

session_start();

if(session_destroy())

{

header("Location: login.php");

}

?>Output

Login logout session in php

How can I logout of function in PHP?

The process is: - Click Log In button on index. php - Enter username and password to access authenticate index file. - Click log out button, which references the logout. php file - it SHOULD clear the cache and return the user to the top level index.

How do I keep a user logged in PHP?

Hence the user can log in without having to enter the Username and Password again until the life of that cookie expires. The example code given below is the way how to remember password checkbox works through PHP. $name = mysqli_real_escape_string( $connect , $_POST [ "user_name" ]);

How do I create a logout button?

Create a Logout button on the form. Right-click the form, and select Create a New Field > Button. ... To create a logout button..

How do we check if a user is logged in in PHP?

session_start(); Check if $_SESSION["loggedIn" ] (is not) true - If not, redirect them to the login page.