简体   繁体   English

PHP从url中删除Get请求

[英]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. 我正在研究一个投票系统,该系统通过GET请求来了解必须更新哪个帖子ID。 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. 但是当投票完成后,我的网址中仍然存在获取请求(/?vote_up = 123),因此如果用户刷新其页面,则将再次设置投票。 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. 首先,投票操作不应该依赖于GET请求,根据定义,GET请求应该检索信息而不是设置信息。

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. 也就是说,您应该使用PUT请求(或POST请求使事情变得更简单),并且您的投票不应直接访问后端URL,因为即使它是POST请求,如果用户重新加载,它也将重新触发。

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. 我建议您在前端使用ajax投票系统,它使用不影响当前用户位置URL的异步请求将数据POST或PUT到PHP后端。

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). 与此同时,一个紧急解决方案是使用POST请求投票,然后登陆一个URL,将用户重定向到他们来自的任何地方(假设您可以从多个页面投票)。 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. 在该过程的最后,用户的位置不是监听POST的位置,而是他被重定向到的位置,因此刷新或重新加载对他来说无害。

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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