gpt4 book ai didi

PHP setcookie() 问题

转载 作者:太空宇宙 更新时间:2023-11-03 12:14:45 25 4
gpt4 key购买 nike

我有一个 php 文件:

<?php 
$username = $_POST["username"];

if((!$username) || (!$_POST["password"])){
header("Location:http://localhost/login.html");
exit;
}

$db_name = "muzak";
$tblname = "auth_users";
$connection = mysqli_connect("localhost", "***username***", "***password***", $db_name) or die("Couldn't connect to database");
$sql = "SELECT * FROM $tblname WHERE username = '$username' AND password= password('$_POST[password]')";

$result = mysqli_query($connection, $sql) or die(mysqli_error($connection));

$numHits = mysqli_num_rows($result);
var_dump($numHits);//equal to one

if($numHits > 0){
$cookie_name = "auth";
$cookie_value = "ok";
$cookie_expire = "0";
$cookie_domain = "127.0.0.1";
setcookie($cookie_name, $cookie_value, $cookie_expire, "/" , $cookie_domain, 0);

$display = "<li><a href=\"secret_areaA.php\">Secret Area A</a></li>
<li><a href=\"secret_areaB.php\">Secret Area B</a> </li>";
}else{
header("Location:http://localhost/login.html");
}
?>
<html>
<head>
<title>Secret Menu</title>
</head>
<body>
<?php echo $display;?>
</body>
</html>

它从 html 文件中获取输入并请求数据库进行确认。 HTML 部分工作正常,但是当我尝试检查 secret_areaA.php 文件中是否存在 cookie 时。 . .

<?php 
if($_COOKIE["auth"] == "ok"){
$msg = "<p><Welcome to the first secret area!<p>";
}else{
die("Cookie wasn't valid");
}
?>
<html>
<head>
<title>
Secret Area A
</title>
</head>
<body>
<?php echo $msg;?>
</body>
</html>

我收到此错误:注意:未定义索引:第 2 行 C:\xampp\htdocs\secret_areaA.php 中的 auth。

我做错了什么?

最佳答案

Cookie expire 设置为 0 让 cookie 在设置后立即过期。所以它永远不会出现。

你要设置的时间必须是 time() + $seconds,而不仅仅是 $seconds

关于PHP setcookie() 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22692817/

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