Joe*_*oeW 14 php error-handling redirect http-status-code-301
我有一个显示各种元素的页面,即使它从数据库调用的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)) {
Run Code Online (Sandbox Code Playgroud)
由于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)) {
Run Code Online (Sandbox Code Playgroud)
上述修改导致的错误:
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
Run Code Online (Sandbox Code Playgroud)
第22和23行是两条标题行,如下所示:
header("HTTP/1.1 301 Moved Permanently");
header( 'Location: http://examplesite.domain/errorpage' ) ;
Run Code Online (Sandbox Code Playgroud)
TMS*_*TMS 32
我有更简单的解决方案 - 它很简单!只需在php源代码的最开头添加此命令:
ob_start();
Run Code Online (Sandbox Code Playgroud)
这将开始缓冲输出,因此在PHP脚本结束之前(或直到您手动刷新缓冲区)之前不会输出任何内容 - 并且在此之前也不会发送标头!因此,您不需要重新组织代码,只需在代码的最开头添加此行即可:-)
| 归档时间: |
|
| 查看次数: |
22206 次 |
| 最近记录: |