2012-06-05 29 views
1

我正在使用頭(位置:something.php)在一個PHP項目中,我試圖從一個頁面重定向到另一個頁面。它的正常工作在本地機器上,但是當上傳到服務器,會出現以下錯誤:關於PHP中的頭錯誤

Warning: Cannot modify header information - headers already sent by (output started at /homepages/4/d404449574/htdocs/yellowandred_in/newTest/editHome.php:15) in /homepages/4/d404449574/htdocs/yellowandred_in/newTest/editHome.php on line 43 

我已經嘗試過用includeinclude_oncerequirerequire_once,但它給其他錯誤,如:

Cannot redeclare logged_in() (previously declared in C:\wamp\www\ynrNewVersion-1\editHome.php:3) in C:\wamp\www\ynrNewVersion-1\adminIndex.php on line 5 

我的代碼

<?php 
session_start(); 
function logged_in() { 
    return isset($_SESSION['username']); 
} 

function confirm_logged_in() { 
    if (!logged_in()) { 
    include "error404.php"; 
    exit; 
    } 
} 

confirm_logged_in(); 
require_once("connection.php"); 

$query="select * from home"; 
$homeInfo = mysql_query($query, $connection); 
$result = mysql_fetch_array($homeInfo); 

$content = $result['content']; 
$rssFeeds = $result['rssFeeds']; 
$message = ""; 
if(isset($_POST['submit'])){ 
    $content = $_POST['content']; 
    $rssFeeds = $_POST['rssFeeds']; 
    if($content == "" || $rssFeeds == ""){ 
    $message = "Please enter into all the fields"; 
    } else { 
    $query = "UPDATE home SET content='$content',rssFeeds='$rssFeeds' WHERE id=1 LIMIT 1"; 
    $result = mysql_query($query,$connection); 
    if(!$result){ 
     echo "error".mysql_error(); 
    } 
    if ($result == 1) { 
     header("Location:adminIndex.php"); 
    } else { 
     $message = "Error Occurred"; 
    } 
    } 
} 
if(isset($_POST['cancel'])){ 
    header("Location:adminIndex.php"); 
} 
?> 
+0

。可能是因爲上面的回聲,當它首先觸發。頭不會工作,你有一個錯誤。 – sephoy08

+0

問題將出現在設置標題()之前,您已經開始回顯內容了,您可以像提到的@shatru一樣ob_start,或者重新調整代碼以在任何標頭()調用之前不回顯文本 – Scuzzy

回答

0

使用ob_start()在PHP頁面的頂部;

0

您需要在header函數後面放置exit()或die(),否則腳本的其餘部分將繼續執行。

重定向可以採用相對或絕對URL。問題在於結腸之前的空間。嘗試像這樣:

header("Location: adminIndex.php"); 
die(); 
+0

謝謝您的工作 –

0

問題是,你有你的標題函數之前的回聲。

if(!$result){ 
    echo "error".mysql_error(); 
} 

你的兩個頭之前( 「地點:adminIndex.php」),你可以不回任何東西。