PHP Login and Logout Example

Hello folks! welcome to another edition of our tutorial on PHP. In this tutorial post, am going to be showing you guys how you can create a real-time login form using PHP.

PHP Login with Sessions

PHP login script is used to offer authentication to different web pages. The script executes after submitting the user login details to the server.

Login Page

A login page should be as follows and works based on sessions. If the user closes the session, it is going to erase the session data.

RECOMMENDED POST: PHP Complete Form Example


   // error_reporting(E_ALL);
   // ini_set("display_errors", 1);

<html lang = "en">
      <link href = "css/bootstrap.min.css" rel = "stylesheet">
         body {
            padding-top: 40px;
            padding-bottom: 40px;
            background-color: #ADABAB;
         .form-signin {
            max-width: 330px;
            padding: 15px;
            margin: 0 auto;
            color: #017572;
         .form-signin .form-signin-heading,
         .form-signin .checkbox {
            margin-bottom: 10px;
         .form-signin .checkbox {
            font-weight: normal;
         .form-signin .form-control {
            position: relative;
            height: auto;
            -webkit-box-sizing: border-box;
            -moz-box-sizing: border-box;
            box-sizing: border-box;
            padding: 10px;
            font-size: 16px;
         .form-signin .form-control:focus {
            z-index: 2;
         .form-signin input[type="email"] {
            margin-bottom: -1px;
            border-bottom-right-radius: 0;
            border-bottom-left-radius: 0;
         .form-signin input[type="password"] {
            margin-bottom: 10px;
            border-top-left-radius: 0;
            border-top-right-radius: 0;
            text-align: center;
            color: #017572;
      <h2>Enter Username and Password</h2> 
      <div class = "container form-signin">
            $msg = '';
            if (isset($_POST['login']) && !empty($_POST['username']) 
               && !empty($_POST['password'])) {
               if ($_POST['username'] == 'webdesigntutorialz' && 
                  $_POST['password'] == '1234') {
                  $_SESSION['valid'] = true;
                  $_SESSION['timeout'] = time();
                  $_SESSION['username'] = 'webdesigntutorialz';
                  echo 'You have entered valid use name and password';
               }else {
                  $msg = 'Wrong username or password';
      </div> <!-- /container -->
      <div class = "container">
         <form class = "form-signin" role = "form" 
            action = "<?php echo htmlspecialchars($_SERVER['PHP_SELF']); 
            ?>" method = "post">
            <h4 class = "form-signin-heading"><?php echo $msg; ?></h4>
            <input type = "text" class = "form-control" 
               name = "username" placeholder = "username = webdesigntutorialz" 
               required autofocus></br>
            <input type = "password" class = "form-control"
               name = "password" placeholder = "password = 1234" required>
            <button class = "btn btn-lg btn-primary btn-block" type = "submit" 
               name = "login">Login</button>
         Click here to clean <a href = "logout.php" tite = "Logout">Session.


Following code below will erase the session data -

   echo 'You have cleaned session';
   header('Refresh: 2; URL = login.php');


When the above code is executed, it will produce the following result -


Alright guys! This is where we are rounding up for this tutorial post. In our next tutorial guide, we are going to be discussing about PHP Facebook Login.

Feel free to ask your questions where necessary and i will attend to them as soon as possible. If this tutorial was helpful to you, you can use the share button to share this tutorial.

Follow us on our various social media platforms to stay updated with our latest tutorials. You can also subscribe to our newsletter in order to get our tutorials delivered directly to your emails.

Thanks for reading and bye for now.