PHP Login Example

    Login page allows you to authenticate the user access to the webpage. The script will run after the user provides the details and submit the entries.  Below is the login form example:

    Example:

    <?php
       ob_start();
       session_start();
    ?>
    <html lang = "en">
       <head>
          <title>XXX.com</title>
          <link href = "css/bootstrap.min.css" rel = "stylesheet">
    <style>
             body {
    .form-signin input[type="email"] {
                margin-bottom: -1px;
                border-bottom-right-radius: 0;
                border-bottom-left-radius: 0;
                border-color:#017572;
             }
             .form-signin input[type="password"] {
                margin-bottom: 10px;
                border-top-left-radius: 0;
                border-top-right-radius: 0;
                border-color:#017572;
             }
             h2{
                text-align: center;
                color: #017572;
             }
          </style>
       </head>
       <body>
    <div class = "container form-signin">
             <?php
                $msg = '';
                if (isset($_POST['login']) && !empty($_POST['username']) 
                   && !empty($_POST['password'])) {
                   if ($_POST['username'] == 'user' && 
                      $_POST['password'] == '1234') {
                      $_SESSION['valid'] = true;
                      $_SESSION['timeout'] = time();
                      $_SESSION['username'] = 'user';
                      echo 'You have entered valid details';
                   }else {
                      $msg = 'Wrong details';
                   }
                }
             ?>
          </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 = user" 
                   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>
             </form>
             Click here to clean <a href = "logout.php" tite = "Logout">Session.
          </div>
       </body>
    </html>

    Output-

    People are also reading: