PHP: Login with Session | Login session Example-Mini Project

Technosmarter PHP

In this tutorial, we will use session for login and access all pages,
What is session?
The session is used to access the various pages of an entire website. A session creates a temporary directory on the server. With the help of session variables and their value are stored. Session login ,expiry time ,destroyed .

In this small project we will use the session for access the all pages of website . We will create pages .


1. Index.html (session start)
2. Login.php (session start)
3. Loginprocess.php (session start)
4. Page1.php (session start)
5. Page2.php (session start)
6. Page3.php (session start)
7. Logout.php (session start)(destroyed session)

The session is recommended starts the session on every page at the beginning of the page. Index.html is a home page .

When a user wants to visit index.html without the log in the session return to login page. The user can not directly access the index.html page.

If any user wants to access any page session return to login page. First of all users should login then access the all pages direct till the session destroyed ?

Implementation –

First of all, create a database structure.


CREATE TABLE  `users` (

 `id` INT( 50 ) NOT NULL ,
 `uname` VARCHAR( 40 ) NOT NULL ,
 `upassword` VARCHAR( 40 ) NOT NULL
) ENGINE = INNODB DEFAULT CHARSET = latin1;

Now create the main page index.html page . It is index page of a project . First of all we will start the session by session_start();session_start(); function at the beginning page . After that we will check if session is note set than return to login page (login.php )

index.html

<?php 
session_start ();
if(!isset($_SESSION["login"]))

	header("location:login.php"); 

?>

Hey ! welcome to main page .

<a href="page1.php">

PAGE 1

<a href="page2.php">

PAGE 2

<a href="page3.php">

PAGE 3

<a href="logout.php">

Logout

Now create the main page index.html page . It is index page of a project . First of all we will start the session by session_start(); function at the beginning page . After that we will check if session is note set than return to login page (login.php )

login.php

<html>
<head>
</head>

<center>
<fieldset>
<legend>Login </legend>
<form action="loginprocess.php" method="POST">

Username:

Password:


<?php if(isset($_REQUEST["err"])) $msg="Invalid username or Password"; ?>

<?php if(isset($msg)) { echo $msg; } ?>

</form> </fieldset> </center> </html>



The loginprocess.php file will handle the part of database base connectivity and login process. You know that we must start the session at the beginning of the page. First of all start the session. Database connectivity and else login process.

If user id and password matched the redirect to index.html page else session error shows result please enter valid id and password.



loginprocess.php

<?php
session_start ();
$cser=mysqli_connect("localhost","root","","project") or die("connection failed:".mysqli_error());

if(isset($_REQUEST['sub']))
{
$a = $_REQUEST['uname'];
$b = $_REQUEST['upassword'];

$res = mysqli_query($cser,"select* from users where uname='$a'and upassword='$b'");
$result=mysqli_fetch_array($res);
if($result)
{
	
	$_SESSION["login"]="1";
	header("location:index.php");
}
else	
{
	header("location:login.php?err=1");
	
}
}
?>

We will include the page 1 ,page2 ,and page 3 as example to access . All pages include session_start (); At the beginning of page .

User can access the all pages by login. If session is not set then redirect to login.php page .

Page1.php

<?php 
session_start ();
if(!isset($_SESSION["login"]))
	header("location:login.php");


?>

Welcome to page 1

<a href="page1.php">

PAGE 1

<a href="page2.php">

PAGE 2

<a href="page3.php">

PAGE 3

<a href="logout.php">

Logout

Logout

Page2.php

<?php 
session_start ();
if(!isset($_SESSION["login"]))
	header("location:login.php");


?>

Welcome to page 2

<a href="page1.php">

PAGE 1

<a href="page2.php">

PAGE 2

<a href="page3.php">

PAGE 3

<a href="logout.php">

Logout

Logout

Page3.php

<?php 
session_start ();
if(!isset($_SESSION["login"]))
	header("location:login.php");


?>

Welcome to page 3

<a href="page1.php">

PAGE 1

<a href="page2.php">

PAGE 2

<a href="page3.php">

PAGE 3

<a href="logout.php">

Logout

Logout

Logout(session destroyed)

session_destroy(); is used to destroy all sessions. If a user clicks on logout then how cant access any page without login again.

Logout.php

<?php
session_start ();
session_destroy();
header("location:index.php")
?>