PHP - 登入示例



一個典型的 PHP web 應用程式在登入前會對使用者進行身份驗證,方法是詢問使用者的憑據,例如使用者名稱密碼。然後將這些憑據與伺服器上可用的使用者資料進行核對。在本示例中,使用者資料以關聯陣列的形式提供。以下 PHP 登入指令碼進行了說明 -

HTML 表單

程式碼的 HTML 部分顯示了一個簡單的 HTML 表單,該表單接受使用者名稱和密碼,並將資料釋出到自身。

<form action = "<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" method="post">
   <div>
      <label for="username">Username:</label>
      <input type="text" name="username" id="name">
   </div>
   <div>
      <label for="password">Password:</label>
      <input type="password" name="password" id="password">
   </div>
   <section style="margin-left:2rem;">
      <button type="submit" name="login">Login</button>
   </section>
</form>

PHP 身份驗證

PHP 指令碼解析 POST 資料,並檢查 users 陣列中是否存在使用者名稱。如果找到,它將進一步檢查密碼是否與陣列中註冊的使用者相對應。

<?php
   if (array_key_exists($user, $users)) {
      if ($users[$_POST['username']]==$_POST['password']) {
         $_SESSION['valid'] = true;
         $_SESSION['timeout'] = time();
         $_SESSION['username'] = $_POST['username'];
         $msg = "You have entered correct username and password";
      } else { 
         $msg = "You have entered wrong Password"; 
      }
   } else {
      $msg = "You have entered wrong user name";
   }
?>

使用者名稱和相應的訊息將新增到 $_SESSION 陣列中。系統會提示使用者輸入相應的郵件,告知他輸入的憑據是否正確。

完整程式碼

以下是完整程式碼 -

Login.php

<?php
   ob_start();
   session_start();
?>
<html lang = "en">
<head>
   <meta charset="UTF-8">
   <meta name="viewport" content="width=device-width, initial-scale=1.0">
   <link rel="stylesheet" href="loginstyle.css">
   <title>Login</title>
</head>
<body>
   <h2 style="margin-left:10rem; margin-top:5rem;">Enter Username and Password</h2> 
   <?php
      $msg = '';
      $users = ['user'=>"test", "manager"=>"secret", "guest"=>"abc123"];

      if (isset($_POST['login']) && !empty($_POST['username']) 
      && !empty($_POST['password'])) {
         $user=$_POST['username'];                  
         if (array_key_exists($user, $users)){
            if ($users[$_POST['username']]==$_POST['password']){
               $_SESSION['valid'] = true;
               $_SESSION['timeout'] = time();
               $_SESSION['username'] = $_POST['username'];
               $msg = "You have entered correct username and password";
            }
            else {
               $msg = "You have entered wrong Password";
            }
         }
         else {
            $msg = "You have entered wrong user name";
         }
      }
   ?>

   <h4 style="margin-left:10rem; color:red;"><?php echo $msg; ?></h4>
   <br/><br/>
   <form action = "<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" method="post">
      <div>
         <label for="username">Username:</label>
         <input type="text" name="username" id="name">
      </div>
      <div>
         <label for="password">Password:</label>
         <input type="password" name="password" id="password">
      </div>
      <section style="margin-left:2rem;">
         <button type="submit" name="login">Login</button>
      </section>
   </form>

   <p style="margin-left: 2rem;"> 
      <a href = "logout.php" tite = "Logout">Click here to clean Session.</a>
   </p>
   </div> 
</body>
</html>

Logout.php

要登出,使用者可以點選連結到logout.php

<?php
   session_start();
   unset($_SESSION["username"]);
   unset($_SESSION["password"]);
   
   echo '<h4>You have cleaned session</h4>';
   header('Refresh: 2; URL = login.php');
?>

輸入“https:///login.php”啟動應用程式。以下是不同的場景 -

正確的使用者名稱和密碼

PHP Login Example 1

密碼錯誤

PHP Login Example 2

使用者名稱錯誤

PHP Login Example 3

當用戶點選底部的連結時,會話變數將被刪除,並重新顯示登入螢幕。

廣告