Php anti-injection message board (simple)

Source: Internet
Author: User
When a beginner learns php, the test case is the message board. In the past, when the database was not connected, I directly wrote the message entered by the user into a txt file, and then read the display from the txt file (~.~ ). I recently learned some knowledge about accessing the MySQL database using php, and rewritten the message board. The function is relatively simple. 1. The first is the form for submitting the message gustmo

When a beginner learns php, the test case is the message board. In the past, when the database was not connected, I directly wrote the message entered by the user into a txt file, and then read the display from the txt file (~.~ ). I recently learned some knowledge about accessing the MySQL database using php, and rewritten the message board. The function is relatively simple. 1. The first is the form for submitting the message gustmo

When a beginner learns php, the test case is the message board. In the past, when the database was not connected, I directly wrote the message entered by the user into a txt file, and then read the display from the txt file (~.~ ).

I recently learned some knowledge about accessing the MySQL database using php, and rewritten the message board. The function is relatively simple.

1. First, submit the message form guessbook. php to post. php.

 

2. Database Connection config. php

 

3. Verify post. php

When we insert data into the database, data must be processed to keep the input data in the original format when it is displayed. For example, if you do not escape the code and directly display it, for example, JavaScript code will be directly run on your page.

Here we useHtmlspecialchars Function. However, htmlspecialchars does not escape the line break \ r \ n. That is to say, if the data you insert contains a line break, the function does not display a line break when you escape the line feed. Therefore, you must use this function when displaying data.Nl2brFunction.

 ", Htmlspecialchars ($ patch); // insert database statement $ SQL =" insert into content (name, email, content, time) values ('$ name ', '$ email', '$ content', now () "; mysql_query ($ SQL); // insert success, return information echo" script "alert (' submitted successfully! Back to view! '); Location. href ='./guessbook. php'; script ";}?>

4. display the user message list, which can be displayed in the guessbook. php message form.

Include ("config. php"); // connect to the database $ SQL = "select * from content"; // The database query statement. The content is the table that the user leaves a message. $ Result = mysql_query ($ SQL, $ con );

User Content Time

5. Basic results: the results after CSS modification are as follows:

Http://www.phptogether.com/archives/10907

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.