php login example [PHP 登入檢查機制]
php login example [PHP 登入檢查機制]
資料來源: Bard
01.登入表单:
<form action="login.php" method="post"> <label for="username">用户名:</label> <input type="text" id="username" name="username" required> <br> <label for="password">密码:</label> <input type="password" id="password" name="password" required> <br> <button type="submit">登入</button> </form>
02.驗證程序:
<?php session_start(); if ($_SERVER["REQUEST_METHOD"] === "POST") { $username = $_POST["username"]; $password = $_POST["password"]; // 验证用户名和密码(例如查询数据库) if (// 验证通过) { $_SESSION["logged_in"] = true; $_SESSION["username"] = $username; header("Location: protected_page.php"); } else { echo "登入失败,请重试"; } } ?>
03.管控葉面:
<?php session_start(); if (!isset($_SESSION["logged_in"])) { header("Location: login.php"); exit; } // 访问受保护的内容 echo "欢迎," . $_SESSION["username"]; ?>