PHP User Login Example


The PHP development team announces the immediate availability of PHP 7.1.4. Several bugs have been fixed. All PHP 7.1 users are encouraged to upgrade to this version.

index.php

<?php session_start(); 
if(isset($_SESSION['is_login'])){
header('Location:login.php');
    exit;
}
if (isset($_POST['submit'])) {
$getUser = $_POST['username'];
    $getPass = $_POST['userpass'];
    if($getUser=='demo' && $getPass=='demo'){
    $_SESSION['set_user'] = $getUser;
    $_SESSION['is_login'] = true;
header('Location:login.php');
    exit;
    } else {
    header('Location:index.php');
    exit;
    }
} else {
?>
 <!DOCTYPE html>
 <html>
 <head>
  <title>Login</title>
  <style type="text/css">
  *{
  font-family: arial;
  font-size: 12px;
  margin: 2px;
  }
  </style>
 </head>
 <body>
  <form method="post" action="index.php">
<h3>User Login</h3>
  Username : <input type="text" name="username"/><br/><br/>
  Password : <input type="text" name="userpass"/><br/><br/>
  <input type="submit" value="Login" name="submit">
  </form>
 </body>
 </html>
 <?php }
  ?>

login.php

<?php  
session_start();
if(!isset($_SESSION['is_login'])){
header('Location:index.php');
    exit;
}

if (isset($_POST['submit'])) {
$getUser = $_POST['username'];
    $getPass = $_POST['userpass'];
    if($getUser=='demo' && $getPass=='demo'){
    $_SESSION['set_user'] = $getUser;
    $_SESSION['is_login'] = true;
    } else {
    header('Location:index.php');
    exit;
    }
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Welcome Guest</title>
<style type="text/css">
  *{
  font-family: arial;
  font-size: 12px;
  }
  </style>
</head>
<body>
<h3>Welcome Guest <u><?= $_SESSION['set_user'] ?></u></h3>
<a href="logout.php">Logout</a>
</body>
</html>

logout.php

<?php 
session_start();
session_destroy();
header('Location:index.php');
exit;
 ?>


Output






Share on Google Plus

About Pukar

This is a short description in the author block about the author. You edit it by entering text in the "Biographical Info" field in the user admin panel.
    Blogger Comment

0 comments:

Post a Comment