Correct understanding of the true meaning of PHP escaping _ PHP Tutorial

Source: Internet
Author: User
Correctly understand the true meaning of PHP escaping. Correct understanding: In PHP, there is a magic quotation mark switch by default. if this switch is turned on, $ _ GET, $ _ GET, and $ COOKIE transferred from outside will be escaped by PHP. for example, how can localhosttest be correctly understood?

By default, there is a "magic quotes" switch in PHP. if this switch is enabled, $ _ GET, $ _ GET, and $ COOKIE transferred from outside will be escaped by PHP.
For example:

Http: // localhost/test. PHP? Test = 1'

Then it is automatically escaped when the test. PHP output is as follows:

Var_dump ($ _ GET ['test'];

========= Output ========
String (3) "1 '"

The 'number is added with escape. but there is a problem here. when this value is output to the webpage, the screen is full. another function can be used here, and stripslashes can be removed.

The PHP escape in the manual means that it is recommended that you do not enable "magic quotes" because of efficiency issues. this is also a benefit, that is, it can be very safe for new users like me.
There are three methods to disable "magic quotes", because it cannot be executed and closed by PHP, that is, ini_set () cannot be used ().

1. set PHP. ini.

Magic_quotes_gpc = Off
Magic_quotes_runtime = Off
Magic_quotes_sybase = Off

2. if the system cannot be modified, you can use. htaccess

PHP_flag magic_quotes_gpc Off

3. PHP escape method with the lowest efficiency

 
 
  1. if (get_magic_quotes_gpc()) {
  2. function stripslashes_deep($value)
  3. {
  4. $value = is_array($value) ?
  5. array_map('stripslashes_deep', $value) :
  6. stripslashes($value);
  7. return $value;
  8. }
  9. $_POST = array_map('stripslashes_deep', $_POST);
  10. $_GET = array_map('stripslashes_deep', $_GET);
  11. $_COOKIE = array_map('stripslashes_deep', $_COOKIE);
  12. }
  13. ?>

Here we also mention the processing of % when LIKE is in an SQL statement, because addslashes does not escape % _, and the two characters do not need to be escaped in other SQL statements, so I compiled a function like_esc ($ value), which is used only when there is a LIKE statement.

Use stripslashes to export to a webpage and escape it with htmlspecialchars.

I now have a relatively lazy PHP escape method, which also escapes all transferred objects.

 
 
  1. if (!get_magic_quotes_gpc()) {
  2. function addslashes_deep($value)
  3. {
  4. $value = is_array($value) ? array_map('addslashes_deep', $value) : addslashes($value);
  5. return $value;
  6. }
  7. $_POST = array_map('addslashes_deep', $_POST);
  8. $_GET = array_map('addslashes_deep', $_GET);
  9. $_COOKIE = array_map('addslashes_deep', $_COOKIE);
  10. }


Secret has a magic quote switch by default in PHP. if this switch is enabled, $ _ GET, $ _ GET, and $ COOKIE transferred from the outside will be escaped by PHP. for example: http: // localhost/test...

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.