We can build many personal & business applications, where the key feature will be a session.

Let’s see some real life examples, when we need to use sessions.

Project 1: let’s make an application, there will 3 phases –

  • registration module
  • login module
  • Party-entry module.

If a user wants to enter into party then, he/she needs to register first. Then he/she need to login into that. After login he/she only can enter into ‘party-entry’ page.

Let’s see coding implementation below…

  • File – index.html
<!DOCTYPE html>
<html lang=”en”>
<head>
    <meta charset=”UTF-8″>
    <meta http-equiv=”X-UA-Compatible” content=”IE=edge”>
    <meta name=”viewport” content=”width=device-width, initial-scale=1.0″>
    <title>home</title>
</head>
<body>
    <br>
    <button>
        <a href=”register.html”>
            register
        </a>
    </button>
    <br>
    <br>
    <button>
        <a href=”login.html”>
            login
        </a>
    </button>
    <br> <br>
    <button>
        <a href=”party.php”>
            party-entry
        </a>
    </button>
   
</body>
</html>
output

File – register.html

<!DOCTYPE html>
<html lang=”en”>
<head>
    <meta charset=”UTF-8″>
    <meta http-equiv=”X-UA-Compatible” content=”IE=edge”>
    <meta name=”viewport” content=”width=device-width, initial-scale=1.0″>
    <title>register</title>
</head>
<body>
    <br>
    <form action=”register.php” method=”post”>
        <font style=”font-size: 20px;”>Email</font>
        <br>
        <input type=”email” name=”email” required>
        <br><br>
 
        <font style=”font-size: 20px;”>Password</font>
        <br>
        <input type=”password” name=”passd”>
        <br>
        <br>
        <input type=”submit” value=”register”>
    </form>
</body>
</html>
output

File – register.php

<?php
error_reporting(0);
// setup database connection
$conn = mysqli_connect(‘localhost’,’root’,”,’project1′);
if($conn){
//echo “database connected!”;
}
 
$mail  =  $_POST[’email’];
$pass  =  $_POST[‘passd’];
$date = date(‘d/m/y’);
 
$sql=”INSERT INTO `registers` (`enrollid`, `emails`, `pass`, `dateofreg`) VALUES (NULL, ‘$mail’, ‘$pass’, ‘$date’)”;
$query=mysqli_query($conn,$sql);
 
if($query){
    echo “<br> query executed!”;
    echo “<br> <font size=’20px’ color=’green’>
    registration complete
    </font>”;
}
?>
 
<html>
    <body>
        <br><br>
        <br>
<button>
    <a href=”index.html”>go to homepage</a>
</button>
    </body>
</html>
output

File – phpmyadmin(after registration)

Note: if you want to enter into party without login then what happened?? let’s see the image below…

Let’s login first….

  • File – login.html
<!DOCTYPE html>
<html lang=”en”>
<head>
    <meta charset=”UTF-8″>
    <meta http-equiv=”X-UA-Compatible” content=”IE=edge”>
    <meta name=”viewport” content=”width=device-width, initial-scale=1.0″>
    <title>login</title>
</head>
<body>
    <font style=”font-size: 20px;”>Enter details for login</font>
    <br><br>
    <form action=”login.php” method=”post”>
        <font style=”font-size: 20px;”>Email</font>
        <br>
        <input type=”email” name=”email” required>
        <br><br>
 
        <font style=”font-size: 20px;”>Password</font>
        <br>
        <input type=”password” name=”passd”>
        <br>
        <br>
        <input type=”submit” value=”login”>
    </form>
</body>
</html>
output

File – login.php

<?php
error_reporting(0);
// setup database connection
$conn = mysqli_connect(‘localhost’,’root’,”,’project1′);
if($conn){
echo “database connected!”;
}
 
$mail  =  $_POST[’email’];
$pass  =  $_POST[‘passd’];
$sql=”SELECT * FROM registers WHERE emails=’$mail’ && pass=’$pass'”;
$query=mysqli_query($conn,$sql);
$row = mysqli_fetch_assoc($query);
 
if($row[‘enrollid’]){
    echo “<br> query executed!”;
    echo “<br> <font size=’20px’ color=’green’>
    login complete
    </font>”;
 
    session_start();
    $_SESSION[‘loggedin’]=’true’;
}
else{ echo “login failed!”; }
?>
<html>
    <body>
        <br><br>
        <br>
<button>
    <a href=”party.php”>go to party</a>
</button>
    </body>
</html>
output

Note: now we successfully logged-in into system and we can visit party.php without any problem, due to active of session variable.

  • File – party.php(after successfull login)