您能帮我吗?我的网站没有设置以下简单的cookie。虽然其他一些登录脚本可以,但这个不起作用。由于Cookie未设置
test.php的
...
<form action="test2.php" method="post">
Email: <br />
<input type="email" name="email" value=""/> <br />
<input type="submit" name="submit" value="Next"/>
</form>
...
test2.php
<?php ob_start(); ?>
<?php
// call the header of the page
require('header.html');
// connect to database
require "connect.php";
?>
<?php
$email = $_POST['email'];
// set cookie
$one_hour = time() + 3600;
$set = setcookie(user_email, $email, $one_hour);
if($set == TRUE) {
print '<p> Cookie set</p>';
} else {
print '<p> Cookie not set</p>';
}
// call footer of the page
require('footer.html');
?>
<?php ob_flush(); ?>
运行上面的脚本后,我得到这个错误:
警告:不能更改头信息 - 头已在第16行的/websites/public_html/test2.php中发送(输出开始于/websites/public_html/test2.php:1)
的Cookie没有设置
- PS:我的剧本16行是 “$组=的setcookie(email_noaccount,$电子邮件,$小时);”
你必须单独的PHP代码标签?标签上的新线条意味着'echo';''在'setcookie'之前,使错误 –
可能重复[已经由PHP发送的标头](http://stackoverflow.com/questions/8028957/headers-already-sent -by-php) – mario
正如警告所说,你的问题是在第1行,而不是在第16行。 – mario