2011-06-08 55 views
0

下面是代碼:PHP頭重定向不起作用

<?php 
     $curDate = date("r",strtotime("now")); 

     if($database->addNewSale($_SESSION['username'],$_SESSION['userid'], $_SESSION['cart'],$curDate) ==10){ 

      //Some of the offers in the cart have expired and cannot be purchased! 
      header("Location: ../order/index.php"); 

      exit; 


      } else if($database->addNewSale($_SESSION['username'],$_SESSION['userid'], $_SESSION['cart'],$curDate)){ 
       echo "insert process ok"; 

      } 

       else echo "sale has not been saved into the db"; 

     ?> 

但是,當函數addNewSale返回10這是在車的錯誤代碼,重定向頭功能實際上並不重定向到頁面我組。

+2

是否有錯誤信息? – 2011-06-08 07:29:12

+0

'<?php'標籤之前的空格是否是您在此處編寫的內容,或者實際上是否在您的頁面中?任何錯誤? (你設置了什麼error_reporting級別?) – 2011-06-08 07:31:31

+0

它根本不重定向嗎?它會去哪個頁面?有沒有可能../order/index.php不存在,你又被彈回到404頁面? – Paolo 2011-06-08 07:32:03

回答

0

您是否收到任何警告? header()失敗通常是由「headers already sent」警告引起的,這可能是由於空白(不能在php開頭標籤上面有任何內容)或其他任何會發送到html頁面的東西造成的。檢查以確保您的文本編輯器沒有使用字節順序標記(BOM)保存它 - 這會導致與錯誤空白一樣的問題。您可以嘗試開啓輸出緩衝(ob_start() - 只是一定要ob_end_flush()函數時,即可大功告成)

0

第一次嘗試與

if(result=='10')//use single quotes 
    otherwise 
     use full location path 
    $path="index.php"; 
    header("Location:$path"); 
+1

'=='不是類型匹配,所以我不明白爲什麼在撇號中包含數字會幫助解決這種情況。 – Treffynnon 2011-06-08 08:20:22

1

你有

header("Location: ../order/index.php"); 

但位置標題必須是完整的絕對URL。我不知道這是否是您的問題,但即使碰巧起作用也是不正確的。

請參閱http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html第14.30節

+1

另外,在產生任何輸出之前,必須調用'header'函數**。 – 2014-05-15 21:29:55