A simple example of PHP preventing repeated data submission by post

Source: Internet
Author: User

I had this question during an interview in an empire: how to handle the repeated post submission problem? Later, I talked with @ wenyang. He said that I didn't understand how to record the time, what I want is to use the session to record the form page and then submit the page for judgment. If it is equal, it is regarded as successful and the session is cleared. But the problem is that if the form page is html, what should I do? Do you want to call a php verification page? Similar to the Verification Code function. It is also said to set the expiration time using the header... but did not try. The following is written in php and is available after testing.

Copy codeThe Code is as follows:
<? Php
// Enable session
Session_start ();

// If a submission ID exists
If (isset ($ _ GET ['action']) & $ _ GET ['action'] = 'save '){

// If a session exists and it is the same as the passed value, it is submitted.
If (isset ($ _ SESSION ['_ open_auth']) & isset ($ _ POST ['auth']) & $ _ SESSION ['_ open_auth'] =$ _ POST ['auth ']) {
Print_r ($ _ POST );
$ _ SESSION ['_ open_auth'] = null; // clear
} Else {

// Start
Header ("location: post. php ");
}
Exit ();
}

// Authorization
$ Auth = $ _ SESSION ['_ open_auth'] = time ();

?>
<! Doctype html>
<Html>
<Head>
<Meta charset = "UTF-8">
<Title> post </title>
</Head>
<Body>
<Form action = "post. php? Action = save "method =" post ">
<Ul>
<Li>
<Input type = "hidden" name = "auth" value = "<? Php echo $ auth;?> ">
<Input type = "text" name = "userName">
</Li>
<Li>
<Input type = "password" name = "userpass">
</Li>
<Li>
<Input type = "submit" value = "start">
</Li>
<Li>
<? Php echo time () ;?>
</Li>
</Ul>
</Form>
</Body>
</Html>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.