gpt4 book ai didi

php - Cookie 未在 PHP 脚本中正确设置

转载 作者:可可西里 更新时间:2023-11-01 13:42:23 25 4
gpt4 key购买 nike

我是 php 的新手,尝试使用 cookie,但它在我的网站上没有唤醒,任何人都可以指导我,我的代码出了什么问题:

<?php
session_start();
?>
<script>
function Redirect(url)
{
location.href = url;
}

</script>
<?php

define('_VALID_ACCESS', true);
include_once "includes/connect.php";
include_once "includes/login.php";


if(empty($_POST['loginname']) || empty($_POST['password']))
{
$msg = "User or password is empty";
}
else
{
if(login($_POST['loginname'], $_POST['password']) == true)
{
$usern = $_POST['loginname'];
session_register('loginname');
$loginname = $usern;
sleep(1);
if(activestatus($_POST['loginname'], $_POST['password']) == true)
{
$usern = $_POST['loginname'];
session_register('loginname');
$loginname = $usern;
sleep(1);

$hour = time() + 3600;
setcookie("ID_my_site", $_POST['loginname'], $hour);
setcookie("Key_my_site", $_POST['password'], $hour);
$test = $_COOKIE["ID_my_site"];
$msg = "<script> Redirect ('home.html?testname=".$test."')</script>";
//header("Location: home.html");
}
else
{
$msg = "<script> Redirect ('valid.php?testname=".$usern."')</script>";
}

}
else
{
$msg = "<font color=red>User or Password is wrong</font>";
}
}
echo '<div id="divTarget">' . $msg . '</div>';
?>
<link rel="stylesheet" href="css/blueprint/screen.css" type="text/css" media="screen, projection">
<link rel="stylesheet" href="css/blueprint/print.css" type="text/css" media="print">
<link rel="stylesheet" href="css/blueprint/ie.css" type="text/css" media="screen, projection">
<body>
<div class="container" id="login_container">
<form id="login" action="action.php" method="post" name="loginform" >
<fieldset id="login_screen" style="width:350px">
<label id="login_label" for="login">User Login </label>
<br><br>
<label for="login">Email Address</label>
<input type="text" name="loginname" id="loginname" value="email@coolmates.com">
<p id="space"><label for="password">Password</label>
<input type="password" id="password" name="password" value="********" ></p>
<input type="checkbox">Keep me signed in until i signout
<p id="test"><input type="submit" value="Submit"></p>
<a href="forgetpassword.html">Forgot
your password</a>&nbsp;&nbsp;|<span id="free">Not a member?</span><a href="regForm.html">Sign up</a><blink><span id="free">Free</span></blink>
</p>
</fieldset>
</form> </div>
</body>

最佳答案

打开 display_errors 并将您的 error_reporting 设置为 E_ALL,您应该会看到一条关于“headers already sent”的错误消息——您必须在发送任何 HTML 之前调用 setcookie()。来自 php.net/setcookie:

setcookie() defines a cookie to be sent along with the rest of the HTTP headers. Like other headers, cookies must be sent before any output from your script (this is a protocol restriction). This requires that you place calls to this function prior to any output, including and tags as well as any whitespace.

在您发布的代码块中:

<script>
function Redirect(url)
{
location.href = url;
}

</script>

在您尝试设置 cookie 之前就直接输出到浏览器。

您的两种可能性是使用输出缓冲,以便您在最后输出所有内容,或者切换到一种方法,其中所有处理代码首先在一个脚本中执行,然后设置 $_SESSION 和 cookie 值,然后在第一个脚本的末尾包含第二个脚本,其中包含要输出到浏览器的代码。

关于php - Cookie 未在 PHP 脚本中正确设置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/857153/

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