Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The reason and solution of automatically adding backslash before quotation marks after PHP form submission

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces "the reason and solution of automatically adding backslash before quotation marks after PHP form submission". In daily operation, I believe that many people have doubts about the reasons and solutions of automatically adding backslash before quotation marks after PHP form submission. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubt of "the reason and solution of automatically adding backslash before quotation marks after PHP form submission"! Next, please follow the editor to study!

The default PHP instruction magic_quotes_gpc is on, that is, open. At this point, you can use the stripslashes () function to remove the automatically added backslash. The usage is: for example, if the variable containing a string is $str, then use the stripslashes () function to process the string: stripslashes ($str), and the output is to remove the backslash.

So I process the contents of the read string with the stripslashes () function, that is, $value=stripslashes ($str), and then save it.

But there is another problem, that is, because the local PHP instruction magic_quotes_gpc is off, if you use this function, the normal backslash will be removed. This is not what we want.

The solution is to use the function get_magic_quotes_gpc () for detection, removing the backslash if the state is open and not removing the backslash if the state is closed.

The program code is as follows:

$str=$_POST ["str"]; / / read the contents of str and assign to the $str variable if (get_magic_quotes_gpc ()) / / if get_magic_quotes_gpc () is open {$str=stripslashes ($str); / / process the string}

Here are three ways to solve this problem:

Method 1: modify the PHP configuration file php.ini

This method is only suitable for the case that you have the right to manage the server, and if you use the virtual space, you can only use the latter two methods.

Set magic_quotes_gpc, magic_quotes_runtime, and magic_quotes_sybase to off in the PHP configuration file php.ini. As follows:

Magic_quotes_gpc = Off

Magic_quotes_runtime = Off

Magic_quotes_sybase = Off

Method 2: use the .htaccess file

This method is only supported by the server if the server supports htaccess, and the current server generally supports it.

Add the following sentence to the .htaccess file under the program directory:

The copy code is as follows:

Php_flag magic_quotes_gpc Off

Method 3: mask in the code

This method is the most portable and can be used as long as it supports PHP, regardless of the configuration of the server.

Add the following code at the beginning of all PHP files

If (get_magic_quotes_gpc ()) {function stripslashes_deep ($value) {$value=is_array ($value)? array_map ('stripslashes_deep',$value): stripslashes ($value); return $value;} $_ POST=array_map (' stripslashes_deep',$_POST); $_ GET=array_map ('stripslashes_deep',$_GET); $_ COOKIE=array_map (' stripslashes_deep',$_COOKIE); $_ REQUEST=array_map ('stripslashes_deep',$_REQUEST) At this point, the study on "the reason and solution of automatically adding backslash before quotation marks after PHP form submission" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report