2012-05-19 178 views
0

您能帮我吗?我的网站没有设置以下简单的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,$电子邮件,$小时);”
+0

你必须单独的PHP代码标签?标签上的新线条意味着'echo';''在'setcookie'之前,使错误 –

+1

可能重复[已经由PHP发送的标头](http://stackoverflow.com/questions/8028957/headers-already-sent -by-php) – mario

+0

正如警告所说,你的问题是在第1行,而不是在第16行。 – mario

回答

2

只是改变上面的代码如下方式和尝试,后把ob_start()要求()

<?php 
require "connect.php"; 
require('header.html'); 
?> 
<?php ob_start(); ?> 
+0

没有区别。:( –

+0

编辑了答案,尝试 – mack

0

需要之前你发送内容到输出缓冲区:

<?php ob_start(); ?> 
    <--- right here 
<?php 
// call the header of the page 
require('header.html'); 

// connect to database 
require "connect.php"; 
?> 
    <--- and right here 
<?php 
    $email = $_POST['email']; 

你应该在输出摆脱不必要的空白,所以你可以进行你的服务器端处理(包括头修改),然后再建立输出。

理想情况下,你不想混合两者。服务器端处理应该在输出生成之前进行,然后输出将使用处理结果构建并发送到客户端。

+0

仍然,同样的错误:( –