PHP 页面重定向问题 - 无法修改标头信息
2022-08-30 22:50:25
我有一个页面,显示各种元素,即使它从数据库中调用的id不存在或被删除(这抛出了各种丑陋的错误以及搜索引擎继续列出不存在的页面)。
如果不存在$id,是否可以修改下面显示的页面代码的第一部分,以发送404(或至少发送到具有404个标头的投影.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' ) ;