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

梦想与她 提交于 2020-01-09 19:48:41

问题


I have url like this 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

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?


回答1:


Put this in your HTML file (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..
    }



回答2:


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.

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



回答3:


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

<?
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!




回答4:


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



来源:https://stackoverflow.com/questions/13789231/remove-get-parameter-in-url-after-processing-is-finishednot-using-post-php

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!