Jump to content
xisto Community
sonesay

Manage Double Posting In Php

Recommended Posts

Is there a way to create a session variable that expires after a set time e.g 15 seconds or so users hitting reload or trying to submit twice wont be able to. I've disabled my comments function on my site at the moment until I find a solution. I would rather not use javascript as PHP code would be more transparent I think. Any ideas?edit: I forgot to add the whole process is in one page so I have the form action unset so it submits to its containing file. I would like to keep this structure. I know having it submit to a new file and redirecting back would remove the double post and reload problem but I'm looking for an alternative solution.

Edited by sonesay (see edit history)

Share this post


Link to post
Share on other sites

The easiest way would be to store a timestamp in the session variable. Then, check that timestamp against the current time. If it has existed for more than 15 seconds, unset the session variable. Use the time() function to get the UNIX timestamp, then you can do something like:

if( (time() - $_SESSION['creation_time']) >= 15 ){	session_unset();}

Is that the sort of thing you need?

Share this post


Link to post
Share on other sites

Yeah something like that. I'll give it a go and update.update:

$enabled_comment = true;if(isset($_SESSION['msg_creation_time'])){	if(time() - $_SESSION['msg_creation_time'] <= 15)	{		$enabled_comment = false;	}}

Thanks for the example, I am using the above code and it works fine. Is there a way for PHP to destroy posted variables? Reloading page after the 15 seconds stills makes double posting possible but at least there is some flooding protection.

Edited by sonesay (see edit history)

Share this post


Link to post
Share on other sites

Why not just have the PHP script search the database for the same input and deny it if a match is found? I've seen, i think it was, the WordPress comment script do this.

Share this post


Link to post
Share on other sites

I sort of didn't want to go that route since I didn't want to do the queries required to try and match existing post to new ones being submitted. I found out using header() direct to redirect to the same page is possible you just need to track the URL and it will clear post vars without the user knowing.

Share this post


Link to post
Share on other sites
Vitiligo Treatment, VTManage Double Posting In Php

Defeat Vitiligo with our natural treatment. Try and buy Cure Vitiligo Oil, a Natural Vitiligo Treatment for Quick Repigmentation. Our medically proven Vitiligo Remedy will make you live your life all over again.

-reply by Maria

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.