Reputation: 1330
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?
Upvotes: 0
Views: 261
Reputation: 126
I think for this you have to redirect user to the same page after vote has been set .And redirect url will be set by parse url function.
Upvotes: 0
Reputation: 17481
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.
Upvotes: 2