php标头重定向不起作用

时间:2011-06-08 07:25:43

标签: php redirect header

以下是代码:

    <?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这是购物车中的错误代码时,重定向标题函数实际上并没有重定向到我设置的页面。

3 个答案:

答案 0 :(得分:1)

你有

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

但是位置标题必须是完整的绝对URL。我不知道这是不是你的问题,但即使它碰巧工作也不正确。

请参阅http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html第14.30节

答案 1 :(得分:0)

你收到任何警告吗? header()失败最常见的原因是“已发送的头文件”警告,这可能是由空格(不能在php开头标记之上有任何内容)或其他任何可能发送到html页面的内容引起的。检查以确保您的文本编辑器没有使用字节顺序标记(BOM)保存它 - 这会导致与具有错误空格的完全相同的问题。您可以尝试打开输出缓冲(ob_start() - 完成后请确保ob_end_flush()。)

答案 2 :(得分:0)

首先尝试

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