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

58,683

Solution 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..
    }

Solution 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>

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

Share:
58,683

Related videos on Youtube

dtnder
Author by

dtnder

Updated on July 09, 2022

Comments

  • dtnder
    dtnder almost 2 years

    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?

    • Pekka
      Pekka over 11 years
      You can do a header redirect, but to what end?
    • Popnoodles
      Popnoodles over 11 years
      without losing the variables? can't see why you would, but anyway html5 pushState
    • dtnder
      dtnder over 11 years
      @pekka: No redirect, but i want to remove parameter when finished using GET method(just remove parameter)
    • Brad Koch
      Brad Koch over 11 years
      It seems like you're confusing the purpose of GET and POST. GET requests fetch static resources, POST requests create a change of the server side data. I can't think of a reason why you would want to strip these parameters.
    • dtnder
      dtnder over 11 years
      @Brad: I use the GET method to save data to the database. every time I refresh (F5) then the data will continue to enter into the database, because the parameters are still there. so, I want every time after entering parameter data must be erased so that every time I refresh (F5), the data will not be entered into the database because there is no parameter in the url
    • Brad Koch
      Brad Koch over 11 years
      Exactly. This is the reason you should be using POST or PUT for this request; GET should not be used for the creation of new data.
    • Mathieu Turcotte
      Mathieu Turcotte over 5 years
      Brad, sometimes, people have needs that you don't understand (and that are none of your business). My website uses only one page wich is dynamically manipulated using ajax. My webpage is essentially never refreshed. I now need to send an email that will contain a link that, when clicked, will navigate directly to a subsection of the site and load an object. From an email, I can't use javascript. So I cannot send a POST to my page. This means I have to send a GET. But since the page is never refreshed, the GET params would stay in the address forever and cause issues when F5 is pressed.
    • Mathieu Turcotte
      Mathieu Turcotte over 5 years
      Bottom line is: When we ask questions, we want answers. Not opinions. Just because what he tries to do makes no sense in YOUR projects doesn't mean it doesn't make sense in THEIR projects. You could also argue that my project is bad, that I should have distinct pages and not a single ajax-refreshed page... But again, you would be talking about things you don't know. I didn't start this project, and I didn't choose to use such a framework. I have taken this project over from other people, and now I'm stuck with it. So please. Give answers, not opinions. Thank you.
  • dtnder
    dtnder over 11 years
    <script> if(typeof window.history.pushState == 'function') { window.history.pushState({}, "Hide", "http://localhost/join/prog/ex.php"); } </script>. It works, thx.
  • Sanne
    Sanne over 11 years
    The JS solution works only in HTML5 compatible browsers. The backend version works in every situation.
  • Appz Venture
    Appz Venture almost 11 years
    <script> if(typeof window.history.pushState == 'function') { window.history.pushState({}, "Hide", "examplePage.php"); } </script> Thats work great thanks
  • user3782779
    user3782779 over 8 years
    Console: SecurityError: The operation is insecure.
  • Marcello B.
    Marcello B. about 4 years
    Or to stay on the current page add to the url strtok($_SERVER["REQUEST_URI"],'?') otherwise you would just get the url of the webpage (example.com) for example window.history.pushState({}, "Hide", '<?php echo $_SERVER['PHP_SELF'].strtok($_SERVER["REQUEST_URI"],'?');?>'‌​);