Secure use of php form submission programs

Source: Internet
Author: User
This is to perform security analysis on a file that receives the freely submitted form data. I hope it will be helpful to you. First of all, the error () and succeed () in the code are self-defined functions used to display the error information and success Information. In fact, they can also directly echo the error information, here I just want to see the highlights of my error page and define a function for page output.
The code is as follows:
// Savecomment. php // do not read comments first. after reading this article, let's look back.
Require ("config. php ");
Mysql_connect ($ servername, $ dbusername, $ dbpassword) or die ("database connection failed ");
$ Name = $ _ POST ['name'];
$ Content = $ _ POST ['content'];
$ Blogid = $ _ POST ['blogid'];
$ Datearray = getdate (time ());
$ Date = date ("Y-m-d h: I: s", $ datearray [0]);
If (! Empty ($ name )&&! Empty ($ content )){
// Use the empty function to judge whether the form is not empty.
If (strlen ($ name)> 20 ){
// Judge the length of $ name by null.
Error ("The name exceeds 20 bytes (20 English or 10 Chinese characters)
");
}
F (! Is_numeric ($ _ POST ['blogid']) {
Error ("the hidden data has been illegally modified. please return
");
}
// Because $ blogid is to be put into select, this variable is used to indicate which article the comment belongs to. it is of the int type, although it is a hidden variable, however, attackers can modify the remote submission locally, so we need to check the type before putting it into the select statement.
$ Blogsql = "Select * FROM $ comment_table Where blogid = $ blogid"
$ Blogresult = mysql_db_query ($ dbname, $ blogsql );
$ Blog = mysql_fetch_array ($ blogresult );
If (strlen ($ name) = strlen ($ blog [name]) & strlen ($ content) = strlen ($ blog [content]) {
// Query the length of the two fields in the database. because the name length may be the same, but the two fields are the same, the probability of occurrence is quite small under normal circumstances, so use & to judge at the same time.
Error ("The comments you want to submit already exist. please return
");
}
// Start to judge the interval. For more details, see the content below.
Session_start ();
If (session_is_registered ("time") & time ()-$ _ SESSION ['Time'] <60*2) {error ("sorry, the time interval between your two submissions is less than 2 minutes.
");
} Else {
$ SQL = "Insert INTO $ comment_table (date, name, content, blogid) VALUES ('$ date',' $ name', '$ content',' $ blogid ')"
Mysql_db_query ($ dbname, $ SQL );
Mysql_close ();
$ Time = time ();
Session_register ("time ");
Succeed ("The comment is submitted successfully
");
}}
// End non-empty judgment
Error ("You have not completed all the forms
");
?>

The above is a document that records comment data. The form is as follows:
The code is as follows:

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.