-1

I've got some PHP which handles a GET request via a query string. Once processing on that query string is done, it generates a page with the results.

So far so good. But the url in the browser keeps the query string, so e.g. if you hit reload, it again tries to process the GET.

So I'd like to generate the page, but return without the query string. I've tried setting the header() to the URL-minus-query, but that redirects (i.e. reloads) the page, rather than returning directly.

I'd think this is a common and easy task, but I can't find a solution...!

dhc
  • 647
  • 8
  • 17
  • so why was this downgraded? Is there something I'm missing? – dhc Oct 04 '18 at 01:25
  • Modify browser URL without reloading can only be achieved on client side. See [this post](https://stackoverflow.com/a/3340186/10317684) – Ricky Mo Oct 04 '18 at 01:45

2 Answers2

1

In case anyone runs across this post...

For the general case, given the difficulty of changing the url server-side, it's easiest to go through the effort to make it a POST, or use AJAX.

update: But in my case, it's a page where the user can change account information, which then needed to be reflected on that page. The answer in this case is simple: do the database updates first, then just redirect to the same page: parse_url($_SERVER['REQUEST_URI'],PHP_URL_PATH)

Regular PHP processing handles the rest.

dhc
  • 647
  • 8
  • 17
0

The core problem is that you absolutely MUST NOT use GET for processing anything. GET should be safe and refreshing should have no side-effect. Generally the way people solve this is by:

  • Doing some processing with a POST request
  • Redirect to a 'results' page.

The most correct redirect status code for this is 303 See Other, but must frameworks will use 302 Found.

Evert
  • 93,428
  • 18
  • 118
  • 189
  • Thanks. Usually I would redirect, but this is an "in situ" situation. I've gone with a POST... – dhc Oct 05 '18 at 01:47