Why PHP adds a backslash before quotation marks and how PHP removes the backslash

Source: Internet
Author: User
Tags php online

Yesterday with PHP made a read and write HTML document applet, local testing is normal but to the site found that when the submission is saved automatically in double quotation marks in front of a backslash "\", and every time you save a backslash, it is depressed.

Of course, do this just to participate in the electronic shopping district website to update, because too lazy to install CMS and blog program, directly with PHP online modification HTML Document to update the text bar.

From the Internet. Originally PHP program to prevent injection or overflow, through the PHP directive MAGIC_QUOTES_GPC automatically in double quotes, single quotes, backslashes, null before the backslash "\".

But in order to read and write HTML documents, we naturally have a lot of quotes and double quotes, this time we should remove the added backslash.

The default PHP directive MAGIC_QUOTES_GPC is on, which is open. You can then use the stripslashes () function to remove the auto-added backslash. The usage is: for example, the variable containing the string is $STR, then use the stripslashes () function to process the string: stripslashes ($STR), the result of the output is to remove the backslash.

So I took the read string content with the Stripslashes () function, that is, $str=stripslashes ($STR), and then save. After uploading the website is normal.

However, another problem arises because the local PHP directive MAGIC_QUOTES_GPC is off, and if this function is used, it will remove the normal backslash. This is not what we want.

The solution is to use the function GET_MAGIC_QUOTES_GPC () to detect, if it is open, then remove the backslash, if the state is closed, do not remove the backslash.

The program code is as follows:

$str=$_post["str"];  Read the contents of STR to the $STR variable if(get_magic_quotes_gpc())///If GET_MAGIC_QUOTES_GPC () is open {$str=stripslashes($str);  The string is processed }                 

The modified program tests both locally and on the Web site as normal.

Why PHP adds backslashes before quotation marks and how PHP removes backslashes

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.