How PHP judges client IP to prevent duplicate submission of forms

Source: Internet
Author: User
Tags table name

The example of this paper is to analyze the way that PHP prevents form recurrence by recording IP. Share to everyone for your reference. The specific analysis is as follows:

This principle is relatively simple is the first time the user submitted when we record the user's IP address, that way, if the user submits the form again within a fixed time, it prompts for a repeat submission, which is often used to support the application at the top, which is a very bad option to prevent duplication of data submissions.

example, the code is as follows:

The code is as follows Copy Code
<?php
Session_Start ();
if (Empty ($_session[' IP '))//first write to determine whether the IP address is logged, to know whether to write to the database
{
$_session[' IP ']=$_server[' remote_addr '];//first written to pave the back for a refreshing or receding judgment
mysql_query ("INSERT into admin (ID, name, age) VALUES (123, ' Yao Ming ', 25)"); Write to database operations
}
Else//has already had the first write operation and is no longer writing to the database
{
Echo ' Please do not repeat the form or refresh the page '; write some hints or other things that have been written
}
?>

There is another way:

1: In the page generated random code, that is, each submit random code is not the same, in the submission of the time to verify the random code!

2: At the time of submission, verify that if the data exists, it will not be submitted.

If you want to prevent repeated submissions to the incoming IP is not the best way, we can query in the database is not the same record and IP is not want to work with again.

example, the code is as follows:

The code is as follows Copy Code
$sql = "SELECT * from table name where buy_tel= ' phone ' and ip= ' $ip";/and $time-buy_date<60
$query = $db->query ($sql);
if ($db->rows ($query))
{
Echo (' <script>alert you have already submitted, do not repeat! ");</script> ');
}
Else
{
To make a storage operation
}

I hope this article will help you with your PHP program design.

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.