PHP标头在url中传递参数

时间:2012-06-15 06:06:08

标签: php header http-headers

我有一个index.php用于注册,一个login.php作为注册处理程序。当注册失败时,它会向客户端发送错误消息。我使用此代码发送消息

header('Location: http://localhost/musicshare/index.php?reg=false');

并在index.php中接收

if(isset($_POST['reg'])){
        echo 'set';//for checking whether reg is set
        if($_POST['reg']=='false'){
            echo '<script type="text/javascript">';
            echo 'alert("Reg faile")';
            echo '</script>';
        }
    }

然而,它似乎根本不起作用;请帮助,谢谢。

2 个答案:

答案 0 :(得分:1)

使用$_GET代替$_POST

if (isset($_GET['reg']) && $_GET['reg'] == 'false'){
   // do something
}

答案 1 :(得分:0)

当您使用标头()时,它会触发GET请求。

所以你应该使用$ _GET [&#39; reg&#39;]

if(isset($_GET['reg'])){
    echo 'set';//for checking whether reg is set
    if($_GET['reg']=='false'){
        echo '<script type="text/javascript">';
        echo 'alert("Reg faile")';
        echo '</script>';
    }
}