Login and Logout Code in php using Session Example:


To perform this tutorial we have 5 files like below.

  • config.php
  • style.css
  • index.php
  • login.php
  • welcome.php
  • logout.php

Let’s understand step by step about all files. First of all we have one database named “test” and have one table named “test_user”. We will use this database for our tutorial. See my config.php file.

config.php

 

  • <?php $db_hostname = ‘localhost’; // Database hostname $db_username = ‘root’; // Database username $db_password = ”; // Database password $db_name = ‘test’; // Database name $conn = mysqli_connect($db_hostname, $db_username, $db_password, $db_name); if(!$conn) { echo “Unable to connect database”.mysqli_error($conn);die; } else { // echo “Database connected successfully”; } ?>;

 

Now we will create a html login form. In which we have two text fields named email and password. User will fill up this form using his email and password. See my html login form code.

index.php

 

  • <!DOCTYPE html>
  • <html>
  • <head>
  • <title>Login form using php</title>
  • <link rel=“stylesheet” type=“text/css” href=“style.css”>
  • <script type=“text/javascript” src=“jquery-1.11.1.js”></script>
  • <! <script type=“text/javascript” src=“main.js”></script> >
  • </head>
  • <body>
  • <?php session_start(); if(isset($_SESSION[‘email’]) &amp;amp;&amp;amp; !empty($_SESSION[‘email’])) { header(‘location:welcome.php’); } $error = [ “email_error” => ,
  • “password_error” => ,
  • “error_msg” =>
  • ];
  • $form_data = [
  • “email” => ,
  • “password” =>
  • ];
  • if(!empty($_SESSION[‘error’]))
  • {
  • $error = $_SESSION[‘error’];
  • }
  • if(!empty($_SESSION[‘form_data’]))
  • {
  • $form_data = $_SESSION[‘form_data’];
  • }
  • ?>
  • <h1><center>Login form using php</center></h1>
  • <form action=“submit.php” method=“post” onsubmit=“return validate();” id=“form_submission_ajax”>
  • <table class=“form-table”>
  • <tr>
  • <td><label>Email:</label></td>
  • <td><input type=“email” name=“email” id=“email” value=“<?php echo $form_data[‘email’]; ?>”></td>
  • </tr>
  • <tr>
  • <td></td>
  • <td id=“email_error” class=“error”><?php echo $error[‘email_error’]; ?></td>
  • </tr>
  • <tr>
  • <td><label>Password:</label></td>
  • <td><input type=“password” name=“password” id=“password” value=“<?php echo $form_data[‘email’]; ?>”></td>
  • </tr>
  • <tr>
  • <td></td>
  • <td id=“password_error” class=“error”><?php echo $error[‘password_error’]; ?></td>
  • </tr>
  • <tr>
  • <td></td>
  • <td><input type=“submit” name=“submit” value=“Submit”></td>
  • </tr>
  • <tr>
  • <td></td>
  • <td class=“error”><?php echo $error[‘error_msg’]; ?></td>
  • </tr>
  • <tr>
  • <td></td>
  • <td>Test Email : test@test.com</td>
  • </tr>
  • <tr>
  • <td></td>
  • <td>Test Password : abcd</td>
  • </tr>
  • </table>
  • </form>
  • </body>
  • <script type=“text/javascript”>
  • function validate()
  • {
  • var valid = true;
  • var email = $(‘#email’).val();
  • var password = $(‘#password’).val();
  • if(email== || email==null)
  • {
  • valid=false;
  • $(‘#email_error’).html(“* Please enter email.”);
  • }
  • else
  • {
  • $(‘#email_error’).html(“”);
  • }
  • if(password== || password==null)
  • {
  • valid=false;
  • $(‘#password_error’).html(“* Please enter password.”);
  • }
  • else
  • {
  • $(‘#password_error’).html(“”);
  • }
  • if(valid==false)
  • {
  • return false;
  • }
  • else
  • {
  • return true;
  • }
  • }
  • </script>
  • </html>
  • <?php $_SESSION[‘error’] = “”; $_SESSION[‘form_data’] = “”; ?>

 

Related:  PHP – Send Email From Localhost XAMPP

In the index.php, jQuery validation is occur. So if user does not fill up the any field, it will throw an error with appropriate message. Now when user click on the submit button in index.php file. It will redirect the data into login.php file. Login.php file will check that is email and password are matched or not. If not then it will throw an error with appropriate message. See my login.php file

login.php

 

  • <?php session_start(); $valid = true; $error = []; $form_data = []; if(!empty($_POST[‘email’])) { $email = $_POST[‘email’]; $email_data = array(“email” => $email);
  • $form_data = array_merge($form_data, $email_data);
  • $email_error = array(“email_error” => “”);
  • $error = array_merge($error, $email_error);
  • if (!filter_var($email, FILTER_VALIDATE_EMAIL))
  • {
  • $valid = false;
  • $email_error = array(“email_error” => “* Valid emial format is ‘abc@xxxx.com’.”);
  • $error = array_merge($error, $email_error);
  • }
  • }
  • else
  • {
  • $valid = false;
  • $email = “”;
  • $email_data = array(“email” => $email);
  • $form_data = array_merge($form_data, $email_data);
  • $email_error = array(“email_error” => “* Email is required.”);
  • $error = array_merge($error, $email_error);
  • }
  • if(!empty($_POST[‘password’]))
  • {
  • $password = md5($_POST[‘password’]);
  • $password_data = array(“password” => $password);
  • $form_data = array_merge($form_data, $password_data);
  • $password_error = array(“password_error” => “”);
  • $error = array_merge($error, $password_error);
  • }
  • else
  • {
  • $valid = false;
  • $password = “”;
  • $password_data = array(“password” => $password);
  • $form_data = array_merge($form_data, $password_data);
  • $password_error = array(“password_error” => “* Password is required.”);
  • $error = array_merge($error, $password_error);
  • }
  • if($valid == true)
  • {
  • include ‘config.php’;
  • mysqli_select_db($conn, $db_name);
  • $sql = “SELECT * FROM test_user WHERE email =’$email’ AND password=’$password'”;
  • $query = mysqli_query($conn, $sql);
  • if(!$query)
  • {
  • echo “Query does not exist.”;
  • die;
  • }
  • $row = mysqli_num_rows($query);
  • if($row==1)
  • {
  • $_SESSION[‘email’] = $email;
  • header(‘Location:welcome.php’);
  • }
  • else
  • {
  • $error_msg = array(“error_msg” => “* Your email and password does not match.”);
  • $error = array_merge($error, $error_msg);
  • $_SESSION[‘error’] = $error;
  • $_SESSION[‘form_data’] = $form_data;
  • header(‘Location:index.php’);
  • }
  • }
  • else
  • {
  • $error_msg = array(“error_msg” => “* Please fill the information.”);
  • $error = array_merge($error, $error_msg);
  • $_SESSION[‘error’] = $error;
  • $_SESSION[‘form_data’] = $form_data;
  • header(‘Location:index.php’);
  • }
  • ?>

 

Login.php file check the email and password sent from index.php, if they match with database then it will let you on welcome page (i.e. welcome.php). Otherwise it will throw an error with appropriate massage and redirect to the index.php file. If credential match then it will store in the session and shows in the welcome.php file. See below welcome.php file code.

Related:  50+ PHP Interview Questions And Answers For Freshers

welcome.php

 

  • <?php session_start(); if(!isset($_SESSION[‘email’]) &amp;amp;&amp;amp; empty($_SESSION[‘email’])) { header(‘location:index.php’); } ?>
  • <h2>Welcome <?php echo $_SESSION[‘email’]; ?></h2>
  • You are successfully login in the application. Thank you!
  • You can <a href=“logout.php”>Logout</a> from here.

 

In the welcome.php file, there is one welcome message for successfully login message. There is also logout link. Here we use logout.php file for the successfully logout user. In logout.php file, i destroy the session data. See the below logout.php file.

logout.php

 

  • <?php session_start(); $_SESSION[‘email’] = ; unset($_SESSION[‘email’]); session_destroy(); header(‘location:index.php’); ?>

 

If user click on the logout link, Session would destroy and redirect to index.php file.

I hope that you may like tutorial on login logout using session in php, please share it with your friends. If you have any query regarding this tutorial (login and logout code in php using session) please let me know in the comment i will help you to solve it. Watch our other awesome tutorials. Thank you for reading this article. 🙂

Por journey

system analyst lawyer journalist ambientalist

Deixar um comentário