标头位置在活动服务器上不起作用


Header location is not working on live server

我有一个注册和付款页面。程序流程类似于注册>确认>付款。在注册页面进行验证和计算后,需要进入确认页面
我的代码是

<?php
ob_start();
session_start();
include("header.php");
include("ini.php");
date_default_timezone_set('Asia/Brunei');
header('Cache-Control: max-age=900');
if (isset($_POST['submit'])) {
    $error = 0;
//validations
if ($error <= 0) { 
//do some calculation
header('location:confirm.php');
}
}
<?php
ob_flush();
?>

控件输入到if($error<=0)子句中,但保留在注册页上。

我尝试了很多方法,比如

 header('location:confirm.php', true, 302);

而不是

header('location:confirm.php');

删除

ob_start() and ob_flush()

add exit() after  header()

然后转到空白页。

同时尝试使用

echo '<script type="text/javascript">';
        echo 'window.location.href="confirm.php";';
        echo '</script>';

然后控件转到确认页面,但url似乎是registration.php

但是标题('location:confirm.php');在我的本地服务器上运行良好。任何人都请帮我解决这个问题。。header()有其他方法吗。期待你们。。感谢

请记住,在发送任何实际输出之前,必须调用header(),无论是通过普通HTML标记、文件中的空行还是从PHP发送。使用include或require函数或其他文件访问函数读取代码,并且在调用header()之前输出空格或空行,这是一种非常常见的错误。使用单个PHP/HTML文件时也存在同样的问题。

同时尝试更改标题("Location:confirm.php");(注意L是大写的,因为它在本地服务器中的工作可能会遇到严格php类型的问题,请尝试一次)

$host  = $_SERVER['HTTP_HOST'];
$uri   = rtrim(dirname($_SERVER['PHP_SELF']), '/''');
$extra = 'confirm.php';
header("Location: http://$host$uri/$extra");

在末尾使用ob_end_flush()。这应该很有魅力。

我多年来一直有同样的问题,但今天我发现你不必在标题词之前有任何html或空格。试试