【发布时间】:2011-10-21 22:15:32
【问题描述】:
我有一个页面显示各种元素,即使它从数据库调用的 id 不存在或被删除(这会引发各种丑陋的错误以及搜索引擎继续列出不存在的页面)。
如果 $id 不存在,您能否修改下面显示的页面代码的第一部分以发送 404(或至少发送到具有 404 标头的 projecterror.php)?非常感谢!
<?php
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 导致有效条目正确显示页面,但不存在的页面在此修改后的代码下方显示错误:
<?php
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 redirect error-handling http-status-code-301