cookie设置的PHP头修改解决方案[重复]
Posted
技术标签:
【中文标题】cookie设置的PHP头修改解决方案[重复]【英文标题】:PHP header modification solution for cookie setting [duplicate] 【发布时间】:2012-06-17 20:18:07 【问题描述】:可能重复:Headers already sent by php
我正在为我的本地数据库使用 phpmyadmin,并使用 3 个 php 页面来操作管理员面板,以便能够查看、编辑和删除工作线索。这是 adminpanel.php 页面:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"><!-- InstanceBegin template="/Templates/About.dwt" codeOutsideHTMLIsLocked="false" -->
<head>
<!-- InstanceBeginEditable name="doctitle" -->
<title>.: Nokee Kwe - Admin Panel :.</title>
<!-- InstanceEndEditable -->
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link rel="stylesheet" type="text/css" media="screen" href="layout.css" />
<link rel="stylesheet" type="text/css" media="screen" href="style.css" />
<script type="text/javascript">
//<![CDATA[
if (navigator.userAgent.toLowerCase().indexOf('chrome') > -1)
document.write('<link rel=\"stylesheet\" type=\"text/css\" href=\"stylechr.css\" />');
else if (navigator.userAgent.toLowerCase().indexOf('opera') > -1)
document.write('<link rel=\"stylesheet\" type=\"text/css\" href=\"styleopr.css\" />');
else
document.write('<link rel=\"stylesheet\" type=\"text/css\" href=\"style.css\" />');
//]]>
</script>
<!--[if lte IE 8]>
<link rel="stylesheet" type="text/css" href="ie.css" />
<![endif]-->
<!--[if IE 9]>
<link rel="stylesheet" type="text/css" href="ie9.css" />
<![endif]-->
<script type="text/javascript" src="jquery-1.7.2.js"></script>
<style type="text/css">
form
margin: 0 auto;
</style>
</head>
<body>
<div class="wrapper">
<div class="header" id="ieHead">
<div class="splashTop">
<!--[if lte IE 8]>
<img src="images/eagle.png" />
<![endif]-->
<!--[if IE 9]>
<img src="images/eagle.png" />
<![endif]-->
<div class="splashFAX" id="ieFax">Telephone: (519) 667-7088 • Facsimile: (519) 667-4872</div>
<div class="splashADDRESS" id="ieAddress">104-1069 Wellington Road South • London, Ontario • N6E 2H6</div>
<br/>
<span class="splashShare">
<span class="facebook"><script type="text/javascript">
//<![CDATA[
(function()
document.write('<fb:like href="www.facebook.com/NokeeKwe?sk=info" layout="button_count" show_faces="" action="like" font="tahoma" colorscheme="light"></fb:like>');
var s = document.createElement('SCRIPT'), s1 = document.getElementsByTagName('SCRIPT')[0];
s.type = 'text/javascript';
s.async = true;
s.src = 'http://connect.facebook.net/en_US/all.js#xfbml=1';
s1.parentNode.insertBefore(s, s1);
)();
//]]>
</script>
</span><span class="twitter"><script type="text/javascript">
//<![CDATA[
(function()
document.write('<a href="http://twitter.com/share" class="twitter-share-button" data-count="horizontal" data-via="NokeeKwe">Tweet</a>');
var s = document.createElement('SCRIPT'), s1 = document.getElementsByTagName('SCRIPT')[0];
s.type = 'text/javascript';
s.async = true;
s.src = 'http://platform.twitter.com/widgets.js';
s1.parentNode.insertBefore(s, s1);
)();
//]]>
</script></span>
</span>
</div>
<div id="splashMain">
<object type="application/x-shockwave-flash"
data="wheelNew.swf"
style="float: right; position: absolute; margin: -10px 0 0 425px;" id="flashWheel">
<param name="movie" value="wheelNew.swf" />
<param name="quality" value="high"/>
<param name="wmode" value="transparent"/>
</object>
<p class="title" id="em">About<br /> Nokee Kwe</p>
<p class="subtitle" id="emSub">Serving both Aboriginal<br/> and non-Aboriginal populations since 1978.</p>
</div>
<div id="navOuter">
<div id="nav">
<a href="about.html" target="_self"><span class="buttonNav" id="aboutBtn">About Us</span></a>
<a href="career.html" target="_self"><span class="buttonNav" id="cdBtn">Career Directions</span></a>
<a href="nlcentre.html" target="_self"><span class="buttonNav" id="nlcBtn">Native Education Centre</span></a>
<a href="index.html" target="_self"><span class="buttonNav" id="homeBtn">Home</span></a>
</div>
</div>
</div>
<div class="wideBox">
<form action="AdminPanel.php" method="get">
<p><button name="jobLead" value="addJob">+</button><button name="jobLead" value="viewJobs">&</button></p>
</form>
<?php
if (isset($_GET['jobLead']))
switch($_GET['jobLead'])
case 'addJob':
echo "<META HTTP-EQUIV='Refresh' Content='0; URL=addJob.php'>";
break;
case 'viewJobs':
echo "<META HTTP-EQUIV='Refresh' Content='0; URL=viewJobs.php'>";
break;
default:
break;
?></div>
<div class="push"></div>
</div>
<div class="footer">
<table border="0" cellspacing="0" cellpadding="0" class="footer-Links">
<tr>
<td colspan="4"> </td>
</tr>
<tr>
<td align="left" valign="middle" style="padding-left:40px;">
<a href="board.html">Board</a><br />
<a href="sponsors.html">Sponsors</a><br/>
<a href="funders.html">Funders</a><br/>
<a href="partners.html">Partners</a>
</td>
<td align="left" valign="middle">
<a href="history.html">History</a><br />
<a href="vision.html">Vision</a><br />
<a href="mission.html">Mission</a>
</td>
<td align="left" valign="middle">
<a href="contact.html">Contact Nokee Kwe</a><br/>
<a href="career.html">Career Directions</a><br />
<a href="nlcentre.html">Native Education Centre</a>
</td>
<td >
<a href="index.html"><img src="images/bottom/nokeekwe_logo.png" border="0"/></a>
</td>
</tr>
</table>
</div>
<!-- Website Tracking Scripts -->
<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script src="A%20CSS%20Sticky%20Footer_files/ga.js" type="text/javascript"></script>
<script type="text/javascript">
var pageTracker = _gat._getTracker("UA-271290-1");
pageTracker._initData();
pageTracker._trackPageview();
</script>
</body>
</html>
当你去viewJobs.php时:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"><!-- InstanceBegin template="/Templates/About.dwt" codeOutsideHTMLIsLocked="false" -->
<head>
<!-- InstanceBeginEditable name="doctitle" -->
<title>.: Nokee Kwe - Admin Panel :.</title>
<!-- InstanceEndEditable -->
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link rel="stylesheet" type="text/css" media="screen" href="layout.css" />
<link rel="stylesheet" type="text/css" media="screen" href="style.css" />
<script type="text/javascript">
//<![CDATA[
if (navigator.userAgent.toLowerCase().indexOf('chrome') > -1)
document.write('<link rel=\"stylesheet\" type=\"text/css\" href=\"stylechr.css\" />');
else if (navigator.userAgent.toLowerCase().indexOf('opera') > -1)
document.write('<link rel=\"stylesheet\" type=\"text/css\" href=\"styleopr.css\" />');
else
document.write('<link rel=\"stylesheet\" type=\"text/css\" href=\"style.css\" />');
//]]>
</script>
<!--[if lte IE 8]>
<link rel="stylesheet" type="text/css" href="ie.css" />
<![endif]-->
<!--[if IE 9]>
<link rel="stylesheet" type="text/css" href="ie9.css" />
<![endif]-->
<script type="text/javascript" src="jquery-1.7.2.js"></script>
<style type="text/css">
form
margin: 0 auto;
</style>
</head>
<body>
<div class="wrapper">
<div class="header" id="ieHead">
<div class="splashTop">
<!--[if lte IE 8]>
<img src="images/eagle.png" />
<![endif]-->
<!--[if IE 9]>
<img src="images/eagle.png" />
<![endif]-->
<div class="splashFAX" id="ieFax">Telephone: (519) 667-7088 • Facsimile: (519) 667-4872</div>
<div class="splashADDRESS" id="ieAddress">104-1069 Wellington Road South • London, Ontario • N6E 2H6</div>
<br/>
<span class="splashShare">
<span class="facebook"><script type="text/javascript">
//<![CDATA[
(function()
document.write('<fb:like href="www.facebook.com/NokeeKwe?sk=info" layout="button_count" show_faces="" action="like" font="tahoma" colorscheme="light"></fb:like>');
var s = document.createElement('SCRIPT'), s1 = document.getElementsByTagName('SCRIPT')[0];
s.type = 'text/javascript';
s.async = true;
s.src = 'http://connect.facebook.net/en_US/all.js#xfbml=1';
s1.parentNode.insertBefore(s, s1);
)();
//]]>
</script>
</span><span class="twitter"><script type="text/javascript">
//<![CDATA[
(function()
document.write('<a href="http://twitter.com/share" class="twitter-share-button" data-count="horizontal" data-via="NokeeKwe">Tweet</a>');
var s = document.createElement('SCRIPT'), s1 = document.getElementsByTagName('SCRIPT')[0];
s.type = 'text/javascript';
s.async = true;
s.src = 'http://platform.twitter.com/widgets.js';
s1.parentNode.insertBefore(s, s1);
)();
//]]>
</script></span>
</span>
</div>
<div id="splashMain">
<object type="application/x-shockwave-flash"
data="wheelNew.swf"
style="float: right; position: absolute; margin: -10px 0 0 425px;" id="flashWheel">
<param name="movie" value="wheelNew.swf" />
<param name="quality" value="high"/>
<param name="wmode" value="transparent"/>
</object>
<p class="title" id="em">About<br /> Nokee Kwe</p>
<p class="subtitle" id="emSub">Serving both Aboriginal<br/> and non-Aboriginal populations since 1978.</p>
</div>
<div id="navOuter">
<div id="nav">
<a href="about.html" target="_self"><span class="buttonNav" id="aboutBtn">About Us</span></a>
<a href="career.html" target="_self"><span class="buttonNav" id="cdBtn">Career Directions</span></a>
<a href="nlcentre.html" target="_self"><span class="buttonNav" id="nlcBtn">Native Education Centre</span></a>
<a href="index.html" target="_self"><span class="buttonNav" id="homeBtn">Home</span></a>
</div>
</div>
</div>
<div class="wideBox">
<?php
$jobID = 0;
$rows = 0;
$errors = 0;
$DBConnect = @mysql_connect("localhost", "root");
if($DBConnect === FALSE)
echo "<p>Unable to connect to the database server. " .
"Error code " . mysql_errno() . ": " .
mysql_error() . "</p>\n";
$errors++;
else
$DBName = "nokeekwe";
$result = @mysql_select_db($DBName, $DBConnect);
if($result === FALSE)
echo "<p>Unable to select the database. " .
"Error code " . mysql_errno($DBConnect) . ": " .
mysql_error($DBConnect) . "</p>\n";
$errors++;
if($DBConnect !== FALSE)
$jobID = 0;
$errors = 0;
$title = "";
$desc = "";
$req = "";
$employer = "";
$phone = "";
$fax = "";
$email = "";
$address = "";
$TableName = "tbljobs";
$SQLstring = "SELECT * FROM tbljobs";
$QueryResult = @mysql_query($SQLstring, $DBConnect);
while(($Row = mysql_fetch_row($QueryResult)) !== FALSE)
$jobID = $Row[0];
setcookie("jobID", $jobID);
echo "<table>\n";
echo "<tr><td>Job ID #: $Row[0]</td></tr>\n";
echo "<tr><td>Job Title: $Row[1]</td></tr>\n";
echo "<tr><td>Job Description: $Row[2]</td></tr>\n";
echo "<tr><td>Job Requirements: $Row[3]</td></tr>\n";
echo "<tr><td>Employer: $Row[4]</td></tr>\n";
echo "<tr><td>Phone: <br /> $Row[5]</td></tr>\n";
echo "<tr><td>Fax: <br /> $Row[6]</td></tr>\n";
echo "<tr><td>Email: <br /> $Row[7]</td></tr>\n";
echo "<tr><td>Address: <br /> $Row[8]</td></tr>\n";
echo "<form action='editJobs.php' method='request'><button name='func' value='edit'>Edit</button><button name='func' value='delete'>Delete</button></form></tr>\n";
echo "</table>\n";;
$rows = mysql_num_rows($QueryResult);
if($rows == 0)
echo "no jobs to show\n";
mysql_close($DBConnect);
?>
</div>
<div class="push"></div>
</div>
<div class="footer">
<table border="0" cellspacing="0" cellpadding="0" class="footer-Links">
<tr>
<td colspan="4"> </td>
</tr>
<tr>
<td align="left" valign="middle" style="padding-left:40px;">
<a href="board.html">Board</a><br />
<a href="sponsors.html">Sponsors</a><br/>
<a href="funders.html">Funders</a><br/>
<a href="partners.html">Partners</a>
</td>
<td align="left" valign="middle">
<a href="history.html">History</a><br />
<a href="vision.html">Vision</a><br />
<a href="mission.html">Mission</a>
</td>
<td align="left" valign="middle">
<a href="contact.html">Contact Nokee Kwe</a><br/>
<a href="career.html">Career Directions</a><br />
<a href="nlcentre.html">Native Education Centre</a>
</td>
<td >
<a href="index.html"><img src="images/bottom/nokeekwe_logo.png" border="0"/></a>
</td>
</tr>
</table>
</div>
<!-- Website Tracking Scripts -->
<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script src="A%20CSS%20Sticky%20Footer_files/ga.js" type="text/javascript"></script>
<script type="text/javascript">
var pageTracker = _gat._getTracker("UA-271290-1");
pageTracker._initData();
pageTracker._trackPageview();
</script>
</body>
</html>
您会在 viewJobs.php 页面的顶部看到此错误:
警告:无法修改标头信息 - 第 143 行的 C:\xampp\htdocs\NokeeKweSite\viewJobs.php 中的标头已由(输出开始于 C:\xampp\htdocs\NokeeKweSite\viewJobs.php:98)发送
【问题讨论】:
另外,当您提出问题时,请仅发布相关信息。当错误出现在某一特定行时,没有人会检查数百行代码。绝对包括那部分代码(以及我们需要理解的任何其他内容),但其余部分是不必要的。 user1366254,没有人有义务以任何方式帮助您。没有人在这里得到报酬。如果您不愿意尽可能简单地获得准确的响应,那么为什么有人愿意花时间检查您的数百行代码? @user1366254 不,您需要帮助我们帮助您。此外,我发布的链接应该解释为什么你会收到这个错误。考虑阅读它们。 【参考方案1】:我没有阅读您的所有代码,但您可以使用以下命令缓冲您的输出:
ob_start();
【讨论】:
【参考方案2】:你做不到
setcookie("jobID", $jobID);
数据已经推送到浏览器之后。
你正试图在你的 mysql_fetch 循环中设置一个 cookie。
不要回显表格,但要..
<?php
$jobID = 0;
$rows = 0;
$errors = 0;
$DBConnect = @mysql_connect("localhost", "root");
if($DBConnect === FALSE)
$str = "<p>Unable to connect to the database server. " .
"Error code " . mysql_errno() . ": " .
mysql_error() . "</p>\n";
$errors++;
else
$DBName = "nokeekwe";
$result = @mysql_select_db($DBName, $DBConnect);
if($result === FALSE)
$str = "<p>Unable to select the database. " .
"Error code " . mysql_errno($DBConnect) . ": " .
mysql_error($DBConnect) . "</p>\n";
$errors++;
if($DBConnect !== FALSE)
$jobID = 0;
$errors = 0;
$title = "";
$desc = "";
$req = "";
$employer = "";
$phone = "";
$fax = "";
$email = "";
$address = "";
$TableName = "tbljobs";
$SQLstring = "SELECT * FROM tbljobs";
$QueryResult = @mysql_query($SQLstring, $DBConnect);
while(($Row = mysql_fetch_row($QueryResult)) !== FALSE)
$jobID = $Row[0];
setcookie("jobID", $jobID);
$str .= "<table>\n";
$str .= "<tr><td>Job ID #: $Row[0]</td></tr>\n";
$str .= "<tr><td>Job Title: $Row[1]</td></tr>\n";
$str .= "<tr><td>Job Description: $Row[2]</td></tr>\n";
$str .= "<tr><td>Job Requirements: $Row[3]</td></tr>\n";
$str .="<tr><td>Employer: $Row[4]</td></tr>\n";
$str .="<tr><td>Phone: <br /> $Row[5]</td></tr>\n";
$str .="<tr><td>Fax: <br /> $Row[6]</td></tr>\n";
$str .="<tr><td>Email: <br /> $Row[7]</td></tr>\n";
$str .="<tr><td>Address: <br /> $Row[8]</td></tr>\n";
$str .="<form action='editJobs.php' method='request'><button name='func' value='edit'>Edit</button><button name='func' value='delete'>Delete</button></form></tr>\n";
$str .="</table>\n";;
$rows = mysql_num_rows($QueryResult);
if($rows == 0)
$str .="no jobs to show\n";
mysql_close($DBConnect);
?>
将该位移到页面的最顶部。
之后,
<div class="wideBox">
添加:
<?=$str;?>
这将防止在您设置 cookie 之前将信息发送到浏览器。
另外,在 MySQL 循环中设置 cookie 并不是一个好主意。如果您返回多行,您所做的只是将其设置为返回的最后一行的值。
【讨论】:
但该信息需要在...之后保存 你想把桌子吐到哪里。 我建议在 之后以上是关于cookie设置的PHP头修改解决方案[重复]的主要内容,如果未能解决你的问题,请参考以下文章