Php 赢得';t重定向

Php 赢得';t重定向,php,redirect,http-headers,http-redirect,Php,Redirect,Http Headers,Http Redirect,多亏了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) {

多亏了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结果时,我看到标记前面有一个空格。我不知道空间在哪里,我知道这是个错误


感谢帮助:-)

您看到的空白可能是UTF字节顺序标记(BOM)。 使用一些程序,如记事本+,甚至十六进制编辑器来删除它。 以下是参考资料:

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

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

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

会话\u start()
也将生成错误

参考:


另外,我不知道为什么有些人会对可能正确的答案投反对票。

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

也就是说:
$messagee=$messagee.
“$sql.

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


还可以尝试以下操作:
error\u reporting(E\u ALL)在文件的开头。查看它是否返回错误或警告。

发送标题前始终删除输出:

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

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

头无法执行的原因可能存在于第一个
if
-构造中。您是否检查过,该条件在任何时候都是/可以是真的?

有时我会在开头前找到一个空格
尝试使用:header('Location:path/here');出口$redirect=…的用途是什么。。。?只需确保在调用header()之前,aol上的浏览器没有任何输出。在哪个标记之前有空格?您可能在发送头之前发送输出。检查发送的
标题和
标题列表。回答正确,理由错误;-)不必要的空白来自
?>
之间的换行符,与重定向失败没有任何关系,因为标头将在空白得到回显之前发送…是的,我想你是对的。这意味着正确的做法就是放一个出口();语句。行
$messagee=$messagee.
“$sql.
实际上没有输出任何内容。实际上,上面的代码根本没有输出。