本问题已经有最佳答案,请猛点这里访问。
我有一个页面,显示各种元素,即使它从数据库调用的ID不存在或被删除(这会引发各种各样的丑陋错误,搜索引擎会继续列出不存在的页面)。
如果$id不存在,您是否可以修改下面显示的页面代码的第一部分以发送404(或者至少发送到projecterror.php,该文件有404个标题)?多谢!
include_once("includes/linkmysql.php");
$adda=$_GET['a'];
$cont=$_GET['c'];
$select="SELECT * FROM projects where id='$id'";
$qselect = mysql_query($select);
while ($row = mysql_fetch_array($qselect)) {
Matt Wilson建议的以下修改是由vivek goel会生成正确显示页面的有效条目,但不存在的页面会显示此修改代码下的错误:
include_once("includes/linkmysql.php");
$adda=$_GET['a'];
$cont=$_GET['c'];
$select="SELECT * FROM projects where id='$id'";
$qselect = mysql_query($select);
if( mysql_num_rows( $qselect ) === 0 )
{
header("HTTP/1.1 301 Moved Permanently");
header( 'Location: http://examplesite.domain/errorpage' ) ;
exit;
}
while ($row = mysql_fetch_array($qselect)) {
上述修改产生的错误:
Warning: Cannot modify header information - headers already sent by (output started at /home/website/public_html/header1.php:14) in /home/website/public_html/header1.php on line 22
Warning: Cannot modify header information - headers already sent by (output started at /home/website/public_html/header1.php:14) in /home/website/public_html/header1.php on line 23 Lines 22 and 23 are the two header lines in your example above
第22行和第23行是两个标题行,如下所示:
header("HTTP/1.1 301 Moved Permanently");
header( 'Location: http://examplesite.domain/errorpage' ) ;
我有更简单的解决方案给你-很简单!只需在php源文件的开头添加以下命令:
这将开始缓冲输出,以便在PHP脚本结束(或手动刷新缓冲区之前)之前不会真正输出任何内容,并且在那之前也不会发送任何头文件!所以您不需要重新组织代码,只需在代码的开头添加这一行,就可以了:—)
干得好-这一切如愿以偿,多谢!
工作得很好!谢谢!
伟大的解决方案!
在发送任何要查看的内容(浏览器中的文本)之前。检查您的模型是否有效。如果ID无效,请重定向到错误页
header("HTTP/1.1 301 Moved Permanently");
header( 'Location: http://examplesite.domain/errorpage' ) ;
在放置标题之前,您正在发送一些文本。在发送任何视图之前,这些事情都会发生。(任何东西到浏览器)
文本在哪里?我不能解决这个问题…
这是你的整页代码吗?或部分代码
project.php是位于顶部的主页(header1.php是上面写的代码):。
我猜include_once("header1.php")正在发送头和数据。
从header1.php到第一篇文章结尾的代码是:…,然后是第一篇文章中的代码。
如果会话是301重定向,则不应调用它。您确定没有从这些文件向浏览器发送任何O/P吗?
除了数据库用户/密码等所需的config.php文件外,我刚刚尝试删除所有包含的文件-我还删除了session_start(),但它也不起作用。错误继续落在2x标题行上。
有更简单的解决方案,看我的答案
在使用php header()重定向之前,不应将任何内容发送到浏览器。如果邮件头已经发送,您可以改为尝试javascript重定向。
if( mysql_num_rows( $qselect ) === 0 )
{
echo"
exit;
}
但我不确定javascript重定向是否对搜索引擎友好