Php basic security processing tips _ PHP Tutorial

Source: Internet
Author: User
Php basic security processing skills. For escaping common output targets (including clients, database tutorials, and URLs), PHP has built-in functions available. If you want to write an algorithm of your own, you can escape some common output targets (including the client, database tutorial, and URL). PHP has built-in functions available.
If you want to write your own algorithms, it is very important to be foolproof. You need to find a reliable and complete list of special characters in the external system and their representation, so that the data is retained rather than translated.
The most common output target is the client. using htmlentities () to escape data before it is sent is the best method. Like other string functions, the input is a string that is processed and output. However, the best way to use the htmlentities () function is to specify two optional parameters: The Escape mode of quotation marks (second parameter) and character set (third parameter ). The escape mode of quotation marks should be specified as ENT_QUOTES. The purpose is to escape single quotation marks and double quotation marks at the same time. This is the most thorough operation. character set parameters must be matched with the character set used on the page.
We recommend that you define a naming mechanism to determine whether the data has been converted. For the escape data output to the client, I use the $ html array for storage. The data is first initialized into an empty array to save all filtered and converted data.

$ Html = array ();
$ Html ['username'] = htmlentities ($ clean ['username'], ENT_QUOTES, 'utf-8 ');
Echo"

Welcome back, {$ html ['username']}.

";
?>

Tips
The htmlspecialchars () and htmlentities () functions are basically the same. Their parameter definitions are exactly the same, but the escaping of htmlentities () is more thorough. Output the username to the client through $ html ['username'], and you can ensure that the special characters are not browsed.
Error description. If username only contains letters and numbers, it is not necessary to escape them, but this reflects the principle of deep defense. Escaping any output is a good habit. it can dramatically improve the security of your software.
Another common output target is the database. If possible, you need to use PHP built-in functions to escape data in SQL statements. For MySQL users, the best escape function is mysql tutorial _ real_escape_string (). If the database you are using does not have the PHP built-in escape function available, addslashes () is the final choice.
The following example illustrates the correct escape technique for the MySQL database:

$ Mysql = array ();
$ Mysql ['username'] = mysql_real_escape_string ($ clean ['username']);
$ SQL = "SELECT *
FROM profile
WHERE username = '{$ mysql ['username']}' ";
$ Result = mysql_query ($ SQL );
?>

Escape (including client, database tutorial, and URL). built-in functions are available in PHP. If you want to write an algorithm of your own, it's safe to do it...

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.