2012-06-19 173 views
1

我正在重定向感謝PHP,但它根本不重定向(但使其他重定向)。php重定向,將不會重定向

下面是我實現的代碼:

<?php 
session_start(); // pour pouvoir récupérer /creer desvariables de session 

try 
{ 
    $bdd = new PDO('mysql:host=localhost;dbname=youf', 'root', 'root'); 
} 
catch(Exception $e) 
{ 
     die('Erreur : '.$e->getMessage()); 
} 

$redirection="command.php"; 
if (!isset($_SESSION['Login'])){ header('Location: connect.php'); } // Si pas de sessionactives, redirect 
    if (!isset($_SESSION['CommandId'])){ 
     $long = strlen($_SESSION['Login']); 
     $table = $_SESSION['Login'][$long-2].$_SESSION['Login'][$long-1]; 
     $zone = $_SESSION['Login'][$long-4].$_SESSION['Login'][$long-3]; 
     $createCommand="INSERT INTO `youfood`.`command` (`Number`, `Date`, `isPaid`, `Table`, `Zone`, `Server`, `State`) VALUES (NULL, current_date, '0', '".$table."', '".$zone."', '".$_SESSION['NumberEmployee']."', '5');"; 
     $bdd->query($createCommand); 

     $sqlCommandNumber=$bdd->query("SELECT * FROM `command` WHERE `Table` =".$table." AND `Zone` =".$zone." AND `State` =0 LIMIT 0 , 1"); 
     $commandNumber=""; 
     while ($data=$sqlCommandNumber->fetch()){ 
      $commandNumber = $data['Number']; 
     } 
     $_SESSION['CommandId'] = $commandNumber ; 
     header('Location: actionValidateCommand.php'); 
     $redirection="actionValidatedCommand.php"; 
    } else { 
     foreach ($_SESSION['cart'] as $key => $value) { 
      if($value != 0){ 
       $sql = "INSERT INTO `youfood`.`commanddishe` (`Number` ,`command` ,`Dishe` ,`Quantity`)VALUES (NULL, '".$_SESSION['CommandId']."', '".$key."', '".$value."')"; 
       $bdd->query($sql); 
       $messagee = $messagee."<br />".$sql."<br />"; 
       $_SESSION['cart'][$key] = 0 ; 
      } 
     } 
    header('Location: command.php'); 
    $redirection="command.php"; 
} 
?> 
<!DOCTYPE html> 
<html> 

當我檢查不重定向頁面的HTML結果,我看到了標籤之前的空間。我不知道哪裏有空間,我知道這是錯誤的!

感謝幫助我:-)

+1

嘗試使用:header('Location:path/here');出口; –

+0

$ redirect = ...的目的是什麼?只需確保在header()將被調用之前在aol中沒有輸出到瀏覽器。 – Brian

+0

標籤之前的空間?您可能在標題之前發送輸出。用'headers_sent'和'headers_list'檢查。 – hakre

回答

0

您所看到的空白可能是UTF 字節順序標記(BOM)。 使用Notepad ++或甚至十六進制編輯器等程序將其刪除。 這裏是參考:http://www.sunfinedata.com/tips/remove-bom-from-utf-8-files/

如果有一個BOM,PHP告訴Apache發送響應頭,然後沒有額外的頭可以設置。

如果啓用在php.ini的display_errors,你會看到這樣的事情:

警告:不能更改頭信息 - 頭已經發出(輸出開始/blah/blah.php:1)在上線

session_start()/blah/blah.php也將產生一個錯誤。

編號:http://en.wikipedia.org/wiki/Byte_order_mark

附:我不知道爲什麼有些人會低估可能的正確答案。

2

在標題啓動之前,您正在輸出內容。

即:$messagee = $messagee."<br />".$sql."<br />";

爲了重定向到正常工作,你應該避免標題行之前輸出任何東西。

另請在您的文件開始處嘗試此操作:error_reporting(E_ALL);。看看它是否返回錯誤或警告。

+0

正確的答案,錯誤的理由;-)不必要的空格來自'?>'和'<!DOCTYPE html>'之間的換行符,並且不能與重定向失敗有任何關係,因爲標題會被髮送之前,空白將得到迴應... – feeela

+1

是的,我想你是對的。這意味着要做的正確事情就是放一個exit();每個標題啓動後的聲明。 – edwardmp

+0

行'$ messagee = $ messagee。「
」。$ sql。「
」;'實際上並不輸出任何內容。其實上面的代碼根本沒有什麼可輸出的。 –

0

發送頭之前,一定要放棄你的輸出:

// at the beginning of your script 
ob_start(); 

// clean output before sending headers 
ob_clean(); 
header(/* … */); 

的理由不得到執行的標題也許駐留在第一if -construct。你有沒有檢查過,在任何時候這種情況都可能是真的?

0

有時我會在打開<?php標記之前找到一個空格,無論是在php文件還是包含文件中,這似乎並不是這裏的情況。或關閉後?>