从PHP中删除URL的最快方法是什么?

时间:2016-04-11 23:30:52

标签: php get url-redirection

示例:

用户致电:

http://www.example.com/?mysecret=hello&second=world&third=bar

如果“mysecret”正确,则设置cookie并将用户重定向到:

http://www.example.com/?second=world&third=bar

代码示例:

if(is_page(MY_LOCKED_PAGE)) {

if($_COOKIE["unlocked"]=="y") {
    // proceed
} else if(isset($_GET["mysecret"])) {
    setcookie('unlocked','y',time()+3600*24*180,'/',"",false,true);

    // strip mysecret from the URL

    // redirect to the original URL without the get parameter "mysecret", but keeping other get parameters

} else {
    // redirect 404
}
}

填写遗漏的代码 - 剥离参数mysecret并重定向到同一网址的最快方法是什么?

2 个答案:

答案 0 :(得分:3)

只需unset()您的mysecret并利用http_build_query()$_GET超级全局生成新网址:

unset($_GET['mysecret']);
$url = http_build_query($_GET);
// redirect
header("Location: {$url}");

这里是Example

答案 1 :(得分:0)

对我有用的完整答案如下:

    unset($_GET['mysecret']);

    $build = http_build_query($_GET);

    $uri_parts = explode('?', $_SERVER['REQUEST_URI'], 2);

    $prot = "http://";

    if(isset($_SERVER['HTTPS'])) {
        if ($_SERVER['HTTPS'] == "on") {
            $prot = "https://";
        }
    }

    $url = $prot . $_SERVER['HTTP_HOST'] . $uri_parts[0] . "?" . $build;

    // redirect
    header("Location: {$url}"); die;