gpt4 book ai didi

php - 如何使 PHP 页面只能在登录后才能访问

转载 作者:行者123 更新时间:2023-11-29 20:58:49 25 4
gpt4 key购买 nike

我是 PHP 新手,正在尝试为作业组合一个原型(prototype)。我使用以下登录脚本以及填写用户名和密码的简单表单。一旦它根据数据库检查用户名和密码,如果匹配,则将用户带到welcome.php页面。如何使welcome.php 仅在用户成功登录后才可见?

<?php
include("web_config.php");

session_start();

if($_SERVER["REQUEST_METHOD"] == "POST") {
// username and password sent from form

$myusername = mysqli_real_escape_string($mysqli,$_POST['username']);
$mypassword = mysqli_real_escape_string($mysqli,$_POST['password']);

$sql = "SELECT UserAccountID FROM UserAccounts WHERE Username = '$myusername' and Password = '$mypassword'";
$result = mysqli_query($mysqli,$sql);
$row = mysqli_fetch_array($result,MYSQLI_ASSOC);
$active = $row['active'];

$count = mysqli_num_rows($result);

// If result matched $myusername and $mypassword, table row must be 1 row

if($count == 1) {
echo "User logged in."; header("location: welcome.php");
} else {


$error = "Your Login Name or Password is invalid";
}
}
?>
<div style = "font-size:11px; color:#cc0000; margin-top:10px"><?php echo $error; ?></div>

最佳答案

您可能想看看Using sessions & session variables in a PHP Login Script其中已经包含了有关该主题的一些有用信息。

关于php - 如何使 PHP 页面只能在登录后才能访问,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37399387/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com