简体   繁体   中英

PHP Remove Get requests from url

I am working on a voting system which works by GET requests to know which post ID must be updated. But when a vote is done there is still the get request in my url (/?vote_up=123), so if the user refreshes his page the vote will be set again. And that should not happen.

I have now this after the vote is done, but this doesn't work.

$_SERVER['REQUEST_URI'] = strtok( $_SERVER['REQUEST_URI'], '?' );

Can anyone help me with this?

In the first place, a vote casting action shouldn't rely on a GET request, which by definition should retrieve information instead of setting information.

That said, you should use a PUT request (or a POST request to make things simpler), and your vote casting shouldn't access directly to the backend URL, because even if it's a POST request it will retrigger itself if the user reloads.

I'd suggest you use an ajax voting system in the frontend, which POSTs or PUTs data to your PHP backend with an asynch request that doesn't affect the current user's location url.

In the meantime, one emergency solution would be to cast the vote with a POST request, then land on an URL that redirects the user to wherever they came from (assuming you can vote from multiple pages). In the end of the process, the user's location is not the one that listens to the POST, but the one he is redirected to, and therefore it's harmless for him to refresh or reload.

$new_url = preg_replace('/&?return=[^&]*/', '', $old_url);

我认为为此你必须在设置投票后将用户重定向到同一页面。重定向url将由parse url函数设置。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM