Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

WP Insert Post is a very simple and easy to use function. Currently I have it set up on event triggered by whether a user is logged in during a booking to ratify their booking in its own page. My concern is that if the user were to refresh or go back and submit again the page they would end up with 2 bookings.

So I am looking to prevent that from happening. One idea that I have is to use a session that is created when the page with the new booking is created. That session then populates the field ID => in the insert post array. That way overiding the information in the existing post. For this to work it would rely on the default value of ID being a wordpress creation if the session had no value, ie. a booking had not been made yet.

Would that work and if not does anyone know of a way it might.

Marvellous

share|improve this question

3 Answers 3

In wordpress you can do that with so called nonces: WordPress Nonces (Codex)

Add a nonce to your form and when send, check if the nonce is correct. The next time the form get's submitted, the nonce isn't correct any longer, so if you check for that you can prevent replaying the data into your system again.

Here you find a step-by-step description and sample code: Improving security in WordPress plugins using Nonces.

share|improve this answer

I'm not sure I fully understand your question. One common way of preventing accidental re-submission is to have the code that handles the POST do a redirect to a 'normal' URL on successful completion. If the user hits reload then all they get is a reload of a normal page.

share|improve this answer
    
This is already the case. I was concerned if someone were to press the back button and the the page would redirect again but with a new booking having been created. The ID session idea above I have just been working on has so far not worked. –  Robin I Knight Feb 9 '11 at 17:46

If anyone is interested I worked it out.

Step 1:: On the receiving page of the booking (the last page)

<?php  
$thispostID = $post->ID; ?>
<?php  session_start();
$_SESSION['booking-id'] = $thispostID; ?>

This stores the ID of the page that has been created by the wp insert post. If the page was taking a long time to load or the client got bored / thought there was a problem and pressed the back button the fact that this bit of code was at the top of the page means that the new post ID has been taken.

On the intermediary page that created the new booking and had the wp insert post function add this to the array.

'ID' => $_SESSION['booking-id'],

And then to make sure that a Sessioned ID does not effect a future booking, on the initial booking page add the following.

$_SESSION['booking-id'] = '';

That way with no value for the intermediary page wordpress will default and create a new id.

If anyone has such an obscure problem in the future this is how I fixed it.

share|improve this answer
    
That's basically what a nonce does which would have saved you from creating a session which is not common in WP (because of scalability reasons). –  hakre Feb 10 '11 at 14:14

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.