我有一個顯示各種元素的頁面,即使它從數據庫調用的ID不存在或被刪除(這會引發各種醜陋的錯誤,隨着搜索引擎的繼續列出不存在的頁面)。PHP頁面重定向問題 - 無法修改標題信息
如果$ 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
線22a nd 23是兩個標題行如下:
header("HTTP/1.1 301 Moved Permanently");
header('Location: http://examplesite.domain/errorpage') ;
偉大的工作 - 完全按照希望工作,非常感謝! – JoeW
工作完美!謝謝! –
偉大的解決方案! – Newbyman