HOW TO CREATE LOGIN AND LOGOUT USING SESSION IN PHP

in #utopian-io6 years ago (edited)

What Will I Learn?

This tutorial will teach you how to create a simple login page and logout page with html and make it active with php session.

Requirements

  • It is required that you have good knowledge of HTML5, PHP and MySql.
  • You have a database setup already and contains data (name and password), if not check: how to create MySQL database automatically using PHP script and MySQLi queries(Object Oriented Programming).
  • WAMP/MAMP/LAMP/XAMPP or any other PHP/MySQL web stack package.
  • Text editor; Notepad++, atom, sublime text2 etc. of your choice

Difficulty

  • Intermediate ~ Advanced

Tutorial Contents

If you have advanced to making website that contains sensitive information, you would require your users to login and logout to access information, simply to prevent unauthorized access to that data.

This tutorial allows you to use the codes below. You can also use the idea to get you page to work.

Without detailed explanation, it is assumed you understand how POST and GET work with the form tag in html. Let us begin;

STEP 1: CREATE YOUR LOGIN PAGE AND NAME IT INDEX.php
If you don’t have a login page, you could copy the code below;

<!DOCTYPE html>

<html>

<head>

<meta charset="utf-8">

<title>Form</title>

</head>

<body>

<form class="form" action="login.php" method="post">

<table>

<tr>

<td><label for="">Name:</label><input type="text" name="entry" placeholder="Name or Email"></td>

</tr>

<tr>

<td><label for="">Password:</label><input type="password" name="pass" placeholder="**********"></td>

</tr>

<tr>

<td><input type="submit" name="submit" value="Login">

<a href="reg_form.php">Register</a></td>

</tr>

</table>

</form>

</body>

</html>

1.png

STEP 2:CREATE THE LOGIN.php PAGE
On this page we put the SESSION_START(); this allows you to maintain variables across multiple page loads for the user. Copy the code below

<?php

SESSION_START();

include "db_config.php"; // include your database connection page here

if ($_SERVER['REQUEST_METHOD'] == 'POST'){

  $entry = $_POST['entry'];

  $password = md5($_POST['pass']);

 

$selected_db = mysqli_select_db ($_dbx, 'my_database');

  if(!$selected_db){

    echo "No Database Selected";

  }

 

$table_query = "SELECT * FROM fisrt_table WHERE NAME = '$entry' AND PASSWORD = '$password'";

$table_result = mysqli_query($_dbx,$table_query);

$row = mysqli_fetch_array($table_result,MYSQLI_ASSOC);

$count = mysqli_num_rows ($table_result);

 

if ($count == 1){

  $last = 120 + time();

setcookie(logged, date("F jS - g:i a"), $last);

  header('location:success.php');

}else{

  echo "incorrect Username or Password";

}

}

$_dbx->close();

?>

2.png

db_config.php refers to my database connecton page, you put your own there to allow the variables to be pulled.

STEP 3: CREATE YOUR SUCCESS PAGE TO TEST IF WORKING.

<?php

echo "Hello!!,";

if (!isset($_COOKIE['logged'])){

  header('location:index.php');

}

 ?>

 

<!DOCTYPE html>

<html>

<head>

<meta charset="utf-8">

<title>Home</title>

</head>

<body>

<p>Welcome to success page</p>

<p><a href="logout.php">Logout</a></p>

</body>

</html>

3.png

STEP 4: CREATE THE LOGOUT PAGE
This destroys all the variables stored on the page loads.

<?php

session_destroy();

$last = -10 + time(); //how long you wish the session to last

setcookie(logged, date("F jS - g:i a"), $last);

header('location:index.php');

 ?>

4.png

Curriculum

Other Topics on Utopian



Posted on Utopian.io - Rewarding Open Source Contributors

Sort:  

Your contribution cannot be approved because it is a duplicate. It is very similar to a contribution that was already accepted here.
Also, all you did was write the codes without putting them to work.

NOTE:
There are new set of rules try to read them carefully for your future contributions.

You can contact us on Discord.
[utopian-moderator]

Coin Marketplace

STEEM 0.26
TRX 0.11
JST 0.032
BTC 64615.49
ETH 3112.63
USDT 1.00
SBD 3.84