处理完成后删除 URL 中的 GET 参数(不使用 POST),PHP

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13789231/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 06:03:57  来源:igfitidea点击:

remove GET parameter in URL after processing is finished(not using POST), PHP

phphtmlurlget

提问by dtnder

I have url like this http://localhost/join/prog/ex.php

我有这样的网址 http://localhost/join/prog/ex.php

When i use GET method the url address like this http://localhost/join/prog/ex.php?name=MEMORY+2+GB&price=20&quantity=2&code=1&search=add

当我使用 GET 方法时,url 地址是这样的 http://localhost/join/prog/ex.php?name=MEMORY+2+GB&price=20&quantity=2&code=1&search=add

My question is : so, I still use the GET method but I want to after processing in GET method is finished, I want to the url back(remove parameter) into http://localhost/join/prog/ex.php, as previously (not using POST method). How can i do it?

我的问题是:所以,我仍然使用 GET 方法,但我想在 GET 方法中处理完成后,我想像以前一样将 url 返回(删除参数)http://localhost/join/prog/ex.php(不使用 POST 方法)。我该怎么做?

回答by Sanne

Put this in your HTML file (HTML5).

把它放在你的 HTML 文件 (HTML5) 中。

<script>    
    if(typeof window.history.pushState == 'function') {
        window.history.pushState({}, "Hide", "http://localhost/join/prog/ex.php");
    }
</script>

Or using a backend solution using a session for instance;

或者使用例如使用会话的后端解决方案;

<?php
    session_start();

    if (!empty($_GET)) {
        $_SESSION['got'] = $_GET;
        header('Location: http://localhost/join/prog/ex.php');
        die;
    } else{
        if (!empty($_SESSION['got'])) {
            $_GET = $_SESSION['got'];
            unset($_SESSION['got']);
        }

        //use the $_GET vars here..
    }

回答by Mikeys4u

SIMPLE ANSWER

简单的答案

Just place this in the top of the file you need to make the GET querys disappear from the browser's URL bar after loading.

只需将它放在文件的顶部,您就可以在加载后使 GET 查询从浏览器的 URL 栏中消失。

<script>    
    if(typeof window.history.pushState == 'function') {
        window.history.pushState({}, "Hide", '<?php echo $_SERVER['PHP_SELF'];?>');
    }
</script>

回答by Ganesh Rathinavel

i guess after calling the url you want to redirect to the file ex.php , but this time without any parameters. for that try using the following code in ex.php

我想在调用 url 之后你想重定向到文件 ex.php ,但这次没有任何参数。为此尝试在 ex.php 中使用以下代码

<?
if($_GET['name']!='' || $_GET['price']!='' ||$_GET['quantity']!='' ||$_GET['code']!='' || $_GET['search']!=''){ 

/* here the code checks whether the url contains any parameters or not, if yes it will execute parameters stuffs and it will get redirected to the page http://localhost/join/prog/ex.php without any parameters*/

/* do what ever you wish to do, when the parameters are present. */

echo $name;
print $price;
//etc....

$location="http://localhost/join/prog/ex.php";
echo '<META HTTP-EQUIV="refresh" CONTENT="0;URL='.$location.'">';
exit;
}
else{
 /* here rest of the body i.e the codes to be executed after redirecting or without parameters.*/
echo "Hi no parameters present!";
}
?>

here what u did id just redirect redirect to the same page without checking if any parameter is there in the query string. the code intelligently checks for the presence of parameters, id any parameters are there it will redirect to ex.php else it will print "Hi no parameters present!" string!

在这里你所做的只是重定向到同一页面而不检查查询字符串中是否有任何参数。代码智能地检查参数的存在,id 任何参数在那里它会重定向到 ex.php 否则它会打印“嗨,没有参数存在!” 细绳!

回答by lelloman

If you're using apache, consider using a .htaccess file with mod_rewirte. Herea quickstart. I think this result can be obtained on iis as well with web.config file

如果您使用 apache,请考虑使用带有 mod_rewirte 的 .htaccess 文件。 是一个快速入门。我认为这个结果也可以在 iis 上通过 web.config 文件获得