gpt4 book ai didi

当用户登录时 PHP 不会设置 cookie

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

我正在尝试为 PHP 编写一个简单的登录页面,该页面在用户登录时使用电子邮件地址值设置 cookie。

setcookie() 似乎不起作用......但我不知道为什么。

这是登录表单:

<?php include 'header.html' ?>
<div id="maincontent">

<h3>Please enter your login information below</h3>

<FORM method="post" action="login.php">

Salesperson Email: <INPUT TYPE="TEXT" name="email" size="20">
Password: <INPUT TYPE="PASSWORD" name="password" size="20">
<INPUT TYPE="submit" name="submit" value="Login">

</form>
<?php print_r($_COOKIE); ?>
</div>
<?php include 'footer.html' ?>

这是登录文件:

<?php
$username = "root";
$password = "root";
$hostname = "localhost";
//connection to the database
$conn = @mysql_connect($hostname, $username, $password)
or die("Unable to connect to MySQL");
echo "Connected to MySQL<br>";
if (mysql_select_db("w_c_a", $conn)) {
echo ("<p>Database selection successful</p>");
}
else {
die ("Could not locate w_c_a database" .mysql_error());
}
// Select salesperson that matches both username and password
$sql = "SELECT * FROM salesperson WHERE email= '$_POST[email]' AND password = '$_POST[password]'";
$result = mysql_query($sql,$conn) or die(mysql_error());
// if there's a value returned, assign the values to variables.
if (mysql_num_rows($result) == 1){
$name = mysql_result($result, 0, 'name');
$email = mysql_result($result, 0, 'email');
$id = mysql_result($result, 0, 'salesperson');
// set a cookie with the email as a value which expires in a day
setcookie('logged_in', $email, time() + (86400 * 30));
//spit out cookie value
echo $_COOKIE['logged_in'];
} else {
echo ("<p>Incorrect username or password</p>");
echo ("<p>Please try <a href=employee_login.php>logging in again</a></p>");
exit;
}
?>

<?php include 'header.html' ?>
<div id="maincontent">

<?php
print_r($_COOKIE);
echo ("name" . $name . "email" . $email . "id" . $id . "<br>")
?>
</div>
<?php include 'footer.html' ?>

根据我所收集的搜索信息,使用电子邮件值设置 cookie 应该没问题。即使当我尝试使用虚拟数据设置它时,也没有显示任何内容。

我尝试过点击进入下一页等。没有骰子。

我在底部有 print_r($_COOKIE) 值来检查所有 cookie。

我错过了什么?

最佳答案

看起来您在调用 setcookie 之前打印了成功的数据连接,setcookie 通过发送 Set-Cookie header 来工作。设置/发送/打印任何内容后无法发送 header 。尝试删除那 2 条 echo

关于当用户登录时 PHP 不会设置 cookie,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33802921/

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