php PHP页面重定向问题-无法修改头信息

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6974691/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 01:43:41  来源:igfitidea点击:

PHP Page redirect problem - Cannot modify header information

phpredirecterror-handlinghttp-status-code-301

提问by JoeW

I have a page that displays various elements even if the id it's calling from the database does not exist or was deleted (which throws up all sorts of ugly errors along with search engines continuing to list non-existent pages).

我有一个页面显示各种元素,即使它从数据库调用的 id 不存在或被删除(这会引发各种丑陋的错误以及搜索引擎继续列出不存在的页面)。

Can you modify the first part of the page code shown below to send a 404 (or at least to projecterror.php which has 404 headers) if $id does not exist? Many thanks!

如果 $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)) { 

The following modification as kindly suggested by Matt Wilsonas a result of an original comment by Vivek Goelresults in valid entries showing the page correctly but non-existent pages are showing the errors below this modified code:

由于Vivek Goel的原始评论 ,Matt Wilson善意建议的以下修改导致有效条目正确显示页面,但不存在的页面在此修改后的代码下方显示错误:

<?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)) { 

Errors resulting from the above modifications:

上述修改导致的错误:

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

Lines 22 and 23 are the two header lines as below:

第 22 和 23 行是两个标题行,如下所示:

header("HTTP/1.1 301 Moved Permanently");
header( 'Location: http://examplesite.domain/errorpage' ) ;

回答by TMS

I have much easier solution for you - it is simple! Just add this command at the very startof the php source:

我为您提供了更简单的解决方案 - 很简单!只需在 php 源代码的最开始添加此命令:

ob_start();

This will start buffering the output, so that nothing is really output until the PHP script ends (or until you flush the buffer manually) - and also no headers are sent until that time! So you don't need to reorganize your code, just add this line at the very beginning of your code and that's it :-)

这将开始缓冲输出,因此在 PHP 脚本结束(或直到您手动刷新缓冲区)之前,不会真正输出任何内容 - 并且在那之前也不会发送任何标头!所以你不需要重新组织你的代码,只需在代码的最开始添加这一行,就是这样:-)

回答by Vivek Goel

before sending any thing to view (text in the browser). check if your model that if id is valid or not . if id is not valid redirect to your error page

在发送任何要查看的内容(浏览器中的文本)之前。检查您的模型是否有效。如果 id 无效,则重定向到您的错误页面

header("HTTP/1.1 301 Moved Permanently");
header( 'Location: http://examplesite.domain/errorpage' ) ;

回答by Withfriendship Hiox

No content should be sent to the browser before using php header() redirection. You can try javascript redirection instead if headers already sent.

在使用 php header() 重定向之前,不应将任何内容发送到浏览器。如果已发送标头,您可以尝试使用 javascript 重定向。

if( mysql_num_rows( $qselect ) === 0 )
{
   echo "<script type='text/javascript'>window.location.href='{http://examplesite.domain/errorpage}'</script>";
   exit;
}

But I am not sure javascript redirects are search engine friendly

但我不确定 javascript 重定向是否对搜索引擎友好