开发者

In PHP how can i remove query string to avoid people refreshing launching a process twice?

开发者 https://www.devze.com 2023-02-06 13:08 出处:网络
in php i\'m using GET to create query string which launches an action : for example when i go to : www.exam开发者_如何学编程ple.com/index.php?mode=action&name=launch_this

in php i'm using GET to create query string which launches an action : for example when i go to :

www.exam开发者_如何学编程ple.com/index.php?mode=action&name=launch_this

it will get mode=action and name=launch_this and the php will launch a function called "launch_this"

But how can i avoid people from refreshing this page and then re-launch the process ?

i'd like to be able to get these parameters once and convert the url for the client to index.php without parameters..

Is it possible ? Thanks


actions which modify state on the server or launch a process should not use GET, but POST. If you use POST and the user refreshes, the browser will at least prompt for confirmation.

To avoid refreshes, you should send a redirect once the process is launched :

  1. user posts to launch action
  2. server launches action and sends a redirect
  3. user browser receives the redirect and GETs the page atthe given URL
  4. user refreshes, and reloads the page, but doesn't relaunch the action.

This pattern is known as Post/redirect/get, or as "redirect after post".


You can use the Post/Redirect/Get pattern for this: http://en.wikipedia.org/wiki/Post/Redirect/Get


In PHP how can i remove query string to avoid people refreshing launching a process twice?

I'd consider looking at the Post/Redirect/Get pattern.


Before you send any output to the client, set the redirect header.

header("Location: index.php");

This will cause the browser to load the index.php page, and the URL shown in the location bar will change too.

You could also use the history.replaceState() javascript function to change the URL in the browser, but this function is only supported in Safari and Firefox 4 (see for example https://developer.mozilla.org/en/DOM/Manipulating_the_browser_history)


You could store some value inside $_SESSION and check it before execution like

if(!isset($_SESSION['launched'])) {
   $_SESSION['launched'] = true;
   ...
   ...   
}


POST/Redirect/GET is a great answer.

Another possibility would be to use AJAX to actually send the data to the server, and then simply refresh the current page (or navigate to a different one, for that matter) once the AJAX completes. This way, the request that actually sends the data to the server is only ever called upon user action, and would not be called on reload.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号