1

由于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 结果时,我看到标签前有一个空格。我不知道空间在哪里,我知道这是错误的!

感谢帮助我:-)

4

4 回答 4

2

在标题启动之前,您正在输出内容。

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

为了使重定向正常工作,您应该避免在标题行之前输出任何内容。

也试试这个:error_reporting(E_ALL);在文件的开头。查看它是否返回错误或警告。

于 2012-06-19T15:24:39.590 回答
0

您看到的空格可能是 UTF字节顺序标记(BOM)。使用 Notepad++ 之类的程序甚至十六进制编辑器来删除它。这是一个参考:http ://www.sunfinedata.com/tips/remove-bom-from-utf-8-files/

如果有 BOM,PHP 会告诉 Apache 发送响应标头,之后就不能设置额外的标头。

如果您在 php.ini 中启用 display_errors,您将看到如下内容:

警告:无法修改标头信息 - 标头已由第1行/blah/blah.php 中的(输出开始于/blah/blah.php:1 )发送

session_start()也会产生错误。

参考:http ://en.wikipedia.org/wiki/Byte_order_mark

PS我不知道为什么有些人会否决可能正确的答案。

于 2012-06-19T15:23:50.473 回答
0

在发送标头之前始终删除输出:

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

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

标头未执行的原因可能在于第一个if-construct。您是否检查过该条件在任何时候都/可以为真?

于 2012-06-19T15:37:10.280 回答
0

有时我会在开始标记之前找到一个空格<?php,无论是在相关的 php 文件中还是在包含的文件中,但这里似乎并非如此。或关闭后?>

于 2012-06-19T15:37:23.630 回答