我在PHP中遇到了'setcookie‘的问题,我不能解决它。
所以我收到这个错误"Warning: Cannot modify header information - headers已由C:\Program Files\VertrigoServ\www\vote.php:14中的第86行的C:\Program Files\VertrigoServ\www\vote.php中的输出开始发送“
这是文件..第86行是setcookie ($cookie_name,1,time()+86400,'/','',0);还有其他方法吗?
<html>
<head>
<title>Ranking</title>
<link href="style.css" rel="stylesheet" type="text/css">
</head>
<body bgcolor="#EEF0FF">
<div align="center">
<br/>
<div align="center"><div id="header"></div></div>
<br/>
<table width="800" border="0" align="center" cellpadding="5" cellspacing="0" class="mid-table">
<tr><td height="5">
<center>
</tr>
</table>
</center>
</td></tr>
<tr><td height="5"></td></tr>
</table>
<br/>
<?php
include "conf.php";
$id = $_GET['id'];
if (!isset($_POST['submitted']))
{
if (isset($_GET['id']) && is_numeric($_GET['id']))
{
</div></td></tr>
<tr><td align="center" valign="top"><img src="images/ads/top_banner.png"></td></tr>
</table>
</form>
<?php
}
else
{
echo '<font color="red">You must select a valid server to vote for it!</font>';
}
}
else
{
$kod=$_POST['kod'];
if($kod!=$_COOKIE[imgcodepage])
{
echo "The code does not match";
}
else
{
$id = mysql_real_escape_string($_POST['id']);
$query = "SELECT SQL_CACHE id, votes FROM s_servers WHERE id = $id";
$result = mysql_query($query) OR die(mysql_error());
$row = mysql_fetch_array($result, MYSQL_ASSOC);
$votes = $row['votes'];
$id = $row['id'];
$cookie_name = 'vote_'.$id;
$ip = $_SERVER['REMOTE_ADDR'];
$ltime = mysql_fetch_assoc(mysql_query("SELECT SQL_CACHE `time` FROM `s_votes` WHERE `sid`='$id' AND `ip`='$ip'"));
$ltime = $ltime['time'] + 86400;
$time = time();
if (isset($_COOKIE['vote_'.$id]) OR $ltime > $time)
{
echo 'You have already voted in last 24 hours! Your vote is not recorded.';
}
else
{
$votes++;
$query = "UPDATE s_servers SET votes = $votes WHERE id = $id";
$time = time();
$query2 = mysql_query("INSERT INTO `s_votes` (`ip`, `time`, `sid`) VALUES ('$ip', '$time', '$id')");
$result = mysql_query($query) OR die(mysql_error());
setcookie ($cookie_name, 1, time()+86400, '/', '', 0);
}
}
}
?>
<p><a href="index.php">[Click here if you don't want to vote]</a></p><br/>
<p><a href="index.php">Ranking.net</a> © 2010-2011<br> </p>
</div>
</body>
</html> 非常感谢!
发布于 2011-03-20 08:05:04
在调用header()和setcookie()之前,不能对任何输出执行操作。
https://stackoverflow.com/search?q=+headers+already+sent+by
https://stackoverflow.com/tags/php/info
任何输出都包括在打开的<?php标记之前的任何<html>,或者内容的任何print或echo。另一个罪魁祸首是UTF-8BOM http://en.wikipedia.org/wiki/Byte_Order_Mark -大多数文本编辑器看不到它,但在文件开始时会使PHP混淆。
发布于 2011-03-20 08:05:24
设置cookie需要向客户端发送标头,如果输出已经开始,则不能发送标头。
您必须将PHP代码setcookie放在 HTML标记之前,这样您就可以在发送任何输出之前调用setcookie,并且您还可以将PHP代码与表示分离,这是无论如何都应该做的。
发布于 2011-03-20 08:23:57
您应该将cookie代码放在页面的顶部。更好的布局应该是这样的:
<?php
//include config
//check posted data (included settings cookies)
//set needed variables
?>
<html>
.....你也可以把php代码和html分开。这通常是我做的事情。我的用法通常涉及视图类或(在过去) smarty。但一个简单的例子是在上面的php代码的底部添加以下代码,并去掉html:
<?php
if(empty($tpl)) {
$tpl = 'index';
}
if(file_exists("template/{$tpl}.tpl.php")) {
include("template/{$tpl}.tpl.php");
}
else {
header('Location: /');
}
?>YOu需要创建一个名为'templates‘的目录,并将html代码添加到以.tpl.php扩展名结尾的文件中。实际上,它们只是php页面,而是.tpl。部分帮助你记住它只是一个标记。让它们成为php页面(而不是html),这样你就可以输出变量。
然后,在上面代码的各种部分中,将$tpl设置为您想要加载的模板。
这只是一小部分代码,但它应该会让您对如何分隔这些数据有一个大致的了解。THe的主要思想是所有html和文本将在所有编程代码完成后输出。
https://stackoverflow.com/questions/5365822
复制相似问题