PHP login system

Published
2022-06-19
浏览次数 :  253

//login php
//basicly add the login form and select the post form username or password from the databse to see if they are match if not show error message if success redirect to the welcome page

$login = 0;
$invalid = 0;


if ($_SERVER['REQUEST_METHOD'] == 'POST') {
  include 'connect.php';
  $username = $_POST['username'];
  $password = $_POST['password'];

  $sql = "SELECT * FROM `registration` WHERE username = '$username' AND password = '$password'";

  $result = mysqli_query($con,$sql);

  if ($result) {
    $num=mysqli_num_rows($result); //count the row of the result 数数结果有几行
    if ($num>0) { //表示username已经存在
      $login = 1;
      //session use to manage our data use to store our data ,etc: if you login into instagram and close and goto instagram again you don't have to login again because their data is stored by instagram in SESSIONS 
      session_start();
      //store username inside session
      $_SESSION['username'] = $username;
      header('location:home.php');

    } else {
      $invalid = 1;
    }
  }
  
}?>


<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Login to our website</title>
  <link rel="stylesheet" href="bootstrap.min.css">
</head>
<body>
 <?php 
 if ($login) {
  echo '<div class="alert alert-success alert-dismissible fade show" role="alert">
  <strong>Congratulations!</strong> Login successfully!
  <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>';
   // code...
 }

 if ($invalid) {
  echo '<div class="alert alert-danger alert-dismissible fade show" role="alert">
  <strong>Error!</strong> You have error with your username or password!
  <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>';
   // code...
 }


  ?>
  <h1 class="text-center">登录页面</h1>
  <div class="container mt-5">
    <form action="./login.php" method="post">
      <div class="mb-3">
        <label for="username" class="form-label">Name</label>
        <input type="text" class="form-control" id="username" name="username" placeholder="Enter your name">
      </div>
      <div class="mb-3">
        <label for="userpassword" class="form-label">Password</label>
        <input type="password" class="form-control" id="userpassword" name="password" placeholder="Enter your password">
      </div>
      
      <button type="submit" class="btn btn-primary w-100">Login</button>
    </form>
  </div>
  
</body>
<script src="bootstrap.bundle.min.js"></script>
</html>

//connect php 
$HOSTNAME='localhost';
$USERNAME='root';
$PASSWORD='';
$DATABASE='';

$con = mysqli_connect($HOSTNAME,$USERNAME,$PASSWORD,$DATABASE);

if (!$con) {
  die(mysql_error($con));
  // code...
}

//SIGNUP FORM 
$success = 0;
$user = 0;


if ($_SERVER['REQUEST_METHOD'] == 'POST') {
  include 'connect.php';
  $username = $_POST['username'];
  $password = $_POST['password'];

  // $sql = "INSERT INTO `registration` (username,password) VALUES ('$username','$password')";
  // $result = mysqli_query($con,$sql);

  // if ($result) {
  //   echo "Data inserted successfully";
  // } else {
  //   die(mysqli_error($con));
  // }
  
  /**
   * 通过数据库验证是否存在信息然后返回
   */

  $sql = "SELECT * FROM `registration` WHERE username = '$username'";

  $result = mysqli_query($con,$sql);

  if ($result) {
    $num=mysqli_num_rows($result); //count the row of the result 数数结果有几行
    if ($num>0) { //表示username已经存在
      // echo 'User already exists!';
      $user = 1;
    } else {
      $sql = "INSERT INTO `registration` (username,password) VALUES ('$username','$password')";
      $result = mysqli_query($con,$sql);
      if ($result) {
        // echo "Signup successfully!";
        $success = 1;
        header('location:login.php');
      } else {
        die(mysqli_error($con));
      }

    }
  }
  
}?>


<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Sign up page</title>
  <link rel="stylesheet" href="bootstrap.min.css">
</head>
<body>
  <?php 
  if ($user) {
    echo '<div class="alert alert-danger alert-dismissible fade show" role="alert">
  <strong>Error:</strong> The user is already exists!
  <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>';
  }

  if ($success) {
    echo '<div class="alert alert-success alert-dismissible fade show" role="alert">
    <strong>Success!</strong> You are successfully signed up!
    <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
  </div>';

  }

   ?>
  <h1 class="text-center">注册页面</h1>
  <div class="container mt-5">
    <form action="./sign.php" method="post">
      <div class="mb-3">
        <label for="username" class="form-label">Name</label>
        <input type="text" class="form-control" id="username" name="username" placeholder="Enter your name">
      </div>
      <div class="mb-3">
        <label for="userpassword" class="form-label">Password</label>
        <input type="password" class="form-control" id="userpassword" name="password" placeholder="Enter your password">
      </div>
      
      <button type="submit" class="btn btn-primary w-100">Sign up</button>
    </form>
  </div>
  
</body>
<script src="bootstrap.bundle.min.js"></script>
</html>

//logout
session_start();
session_destroy();
header('location:login.php');

//home.php
<?php 
  session_start();
  if (!isset($_SESSION['username'])) {
    header('location:login.php');
    // code...
  }

 ?>

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <link rel="stylesheet" href="bootstrap.min.css">
  <title>Welcome Page</title>
</head>
<body>
  <h1 class="text-center text-success mt-5">Welcome <?php echo $_SESSION['username'] ?></h1>
  <div class="container">
    <a href="logout.php" class="logout btn btn-primary">Logout</a>
  </div>
  
</body>
<script src="bootstrap.bundle.min.js"></script>
</html>

  • 标签1
  • 标签1
  • 标签1
Top