LOGIN PAGE WITH SESSION

login.php

<?php
   ob_start();
   session_start();
?>

<html lang = "en">
   
   <head>
      <title>MK</title>
      <link href = "css/bootstrap.min.css" rel = "stylesheet">
      
      <style>
         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;
            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>
      
      <h2>Enter Username and Password with Session</h2> 
      <div class = "container form-signin">
         
         <?php
            $msg = '';
            
          	
                  $_SESSION['valid'] = true;
                  $_SESSION['timeout'] = time();
                  $_SESSION['username'] = 'mahesh';
				$_SESSION['passsword'] = 1234;
			

                 





 if (!empty($_POST['username']) == $_SESSION['username'] && !empty($_POST['password']) ==  $_SESSION['passsword'])
 {
				
               if ($_POST['username'] == 'mahesh' && 
                  $_POST['password'] == '1234') {
                  $_SESSION['valid'] = true;
                  $_SESSION['timeout'] = time();
                  $_SESSION['username'] = 'mahesh';
			

                  
                  header('Location: http://localhost:8089/login/welcome.php');
			echo ( $_SESSION['username']);
				  
               }else {
                  $msg = 'Wrong username or password';
echo ("Username  ". $_SESSION['username'] .'<br> Password '.$_SESSION['passsword']);

               }
            }




         ?>
      </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 = mahesh" 
               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>
			
         
         
      </div> 
      <h2><a href="logout.php">Clear Session</a></h2>
   </body>
</html>
welcome.php
<html>
<head>

<title>Welcome </title>
</head>

<body>
<h1>Welcome </h1> 
<h2>Mahesh Karande</h2>

<h2><a href="logout.php">Sign Out</a></h2>
</body>
</html>
logout.php
<?php
   session_start();
   unset($_SESSION["username"]);
   unset($_SESSION["password"]);
   
   echo 'You have cleaned session';
   header('Refresh: 2; URL = login.php');
?>

Posted in PHP

Leave a comment