search
HomeBackend DevelopmentPHP TutorialPHP中magic_quotes_gpc跟magic_quotes_runtime的区别、作用和用法

PHP中magic_quotes_gpc和magic_quotes_runtime的区别、作用和用法
魔术引用发生作用是在传递$_GET,$_POST,$_COOKIE时
1.
条件: magic_quotes_gpc=off
写入数据库的字符串未经过任何过滤处理。从数据库读出的字符串也未作任何处理。
数据: ?$data=”snow”’’sun” ; (snow和sun之间是四个连续的单引号).
操作: 将字符串:”snow”’’sun” 写入数据库,
结果: 出现sql语句错误,mysql不能顺利完成sql语句,写入数据库失败。
数据库保存格式:无数据。
输出数据格式:无数据。
说明: 对于未经处理的单引号在写入数据库时会使sql语句发生错误。
2.
条件: magic_quotes_gpc=off
写入数据库的字符串经过函数addlashes()处理。从数据库读出的字符串未作任何处理。
数据: ?$data=”snow”’’sun” ; (snow和sun之间是四个连续的单引号).
操作: 将字符串:”snow”’’sun” 写入数据库,
结果: sql语句顺利执行,数据成功写入数据库
数据库保存格式:snow”’’sun (和输入一样)
输出数据格式:snow”’’sun (和输入一样)
说明: addslashes()函数将单引号转换为’的转义字符使sql语句成功执行,
但’并未作为数据存入数据库,数据库保存的是snow”’’sun 而并不是我们想象的snow’\’’\’sun
3.
条件: magic_quotes_gpc=on
写入数据库的字符串未经过任何处理。从数据库读出的字符串未作任何处理。
数据: ?$data=”snow”’’sun” ; (snow和sun之间是四个连续的单引号).
操作: 将字符串:”snow”’’sun” 写入数据库,
结果: sql语句顺利执行,数据成功写入数据库
数据库保存格式:snow”’’sun (和输入一样)
输出数据格式:snow”’’sun (和输入一样)
说明: magic_quotes_gpc=on 将单引号转换为’的转义字符使sql语句成功执行,
但’并未作为数据入数据库,数据库保存的是snow”’’sun而并不是我们想象的snow’\’’\’sun。
4.
条件: magic_quotes_gpc=on
写入数据库的字符串经过函数addlashes()处理。从数据库读出的字符串未作任何处理。
数据: ?$data=”snow”’’sun” ; (snow和sun之间是四个连续的单引号).
操作: 将字符串:”snow”’’sun” 写入数据库,
结果: sql语句顺利执行,数据成功写入数据库
数据库保存格式:snow’\’’\’sun (添加了转义字符)
输出数据格式:snow’\’’\’sun (添加了转义字符)
说明: magic_quotes_gpc=on 将单引号转换为’的转义字符使sql语句成功执行,
addslashes又将即将写入数据库的单引号转换为’,后者的转换被作为数据写入
数据库,数据库保存的是snow’\’’\’sun
总结如下:
1. 对于magic_quotes_gpc=on的情况,
我们可以不对输入和输出数据库的字符串数据作
addslashes()和stripslashes()的操作,数据也会正常显示。
如果此时你对输入的数据作了addslashes()处理,
那么在输出的时候就必须使用stripslashes()去掉多余的反斜杠。
2. 对于magic_quotes_gpc=off 的情况
必须使用addslashes()对输入数据进行处理,但并不需要使用stripslashes()格式化输出
因为addslashes()并未将反斜杠一起写入数据库,只是帮助mysql完成了sql语句的执行。
补充:
magic_quotes_gpc 作用范围是:WEB客户服务端;作用时间:请求开始时,例如当脚本运行时.
magic_quotes_runtime 作用范围:从文件中读取的数据或执行exec()的结果或是从SQL查询中得到的;作用时间:每次当脚本访问运行状态中产生的数据
============ magic_quotes_gpc和magic_quotes_runtime的区别和用法 =============
PHP提供两个方便我们引用数据的魔法引用函数magic_quotes_gpc和magic_quotes_runtime,这两个函数如果在php.ini设置为ON的时候,就会为我们引用的数据碰到单引号’和双引号”以及反斜线 是自动加上反斜线,帮我们自动转译符号,确保数据操作的正确运行,可是我们在php不同的版本或者不同的服务器配置下,有的magic_quotes_gpc和magic_quotes_runtime设置为on,有的又是off,所以我们写的程序必须符合on和off两种情况。那么magic_quotes_gpc和magic_quotes_runtime两个函数有什么区别呢?看下面的说明:
magic_quotes_gpc
作用范围是:WEB客户服务端;
作用时间:请求开始是,例如当脚本运行时.
magic_quotes_runtime
作用范围:从文件中读取的数据或执行exec()的结果或是从SQL查询中得到的;
作用时间:每次当脚本访问运行状态中产生的数据.
所以
magic_quotes_gpc的设定值将会影响通过Get/Post/Cookies获得的数据
magic_quotes_runtime的设定值将会影响从文件中读取的数据或从数据库查询得到的数据
例子说明:
复制内容到剪贴板
代码:

 
STR: 
 

/* 我们在表单里填写:    "    这些符号,如果magic_quotes_gpc没有开启,那么他们不会被反斜杠转义 */
echo 现在通过POST传递过来的值是: ,$_POST[str],


if(get_magic_quotes_gpc()) {      // 检查magic_quotes_gpc是否打开,如果没有打开,用addslashes进行转义 
      $str = $_POST[str]; 
} else { 
      $str = addslashes($_POST[str]); 


echo 这里是转义过后的: ,$str,
;
$sql = "INSERT INTO lastnames (lastname) VALUES ($str)"; 

//===================================================================================== 
//-----magic_quotes_gpc只会转义:     通过Get/Post/Cookies获得的数据 
//-----magic_quotes_runtime会转义:从文件中读取的数据或执行exec()的结果或是从SQL查询中得到的 
//=====================================================================================
$data = implode(file(try.php));      // 我们在里面依然写"这几个字符,用来测试 
echo 这里是try.php的数据,; 
if (get_magic_quotes_runtime()) {  
      $data = $data;  
      echo 被系统自带转义的 .$data;  
} else {  
      echo 被addslashes转义了的 .$data = addslashes($data);  
}  
 
$sql = "INSERT INTO lastnames (lastname) VALUES ($data)"; 
echo
SQL语句为:
,$sql; 
//---入库都转义了,但是多余了反斜杠,我们要读出来是原来的数据时候使用stripslashes()去掉反斜杠 
//---stripslashes()和addslashes()作用相反 
?>

最关键的区别是就是上面提到的2点:他们针对的处理对象不同
magic_quotes_gpc的设定值将会影响通过Get/Post/Cookies获得的数据
magic_quotes_runtime的设定值将会影响从文件中读取的数据或从数据库查询得到的数据
在这里顺便在提几个想关联的函数:
set_magic_quotes_runtime():
设置magic_quotes_runtime值. 0=关闭.1=打开.默认状态是关闭的.可以通过 echo phpinfo(); 查看magic_quotes_runtime
get_magic_quotes_gpc():
查看magic_quotes_gpc值.0=关闭.1=打开.
get_magic_quotes_runtime():
查看magic_quotes_runtime值。0=关闭.1=打开.
注意的是没有 set_magic_quotes_gpc()这个函数,就是不能在程序里面设置magic_quotes_gpc的值。
Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
PHP's Purpose: Building Dynamic WebsitesPHP's Purpose: Building Dynamic WebsitesApr 15, 2025 am 12:18 AM

PHP is used to build dynamic websites, and its core functions include: 1. Generate dynamic content and generate web pages in real time by connecting with the database; 2. Process user interaction and form submissions, verify inputs and respond to operations; 3. Manage sessions and user authentication to provide a personalized experience; 4. Optimize performance and follow best practices to improve website efficiency and security.

PHP: Handling Databases and Server-Side LogicPHP: Handling Databases and Server-Side LogicApr 15, 2025 am 12:15 AM

PHP uses MySQLi and PDO extensions to interact in database operations and server-side logic processing, and processes server-side logic through functions such as session management. 1) Use MySQLi or PDO to connect to the database and execute SQL queries. 2) Handle HTTP requests and user status through session management and other functions. 3) Use transactions to ensure the atomicity of database operations. 4) Prevent SQL injection, use exception handling and closing connections for debugging. 5) Optimize performance through indexing and cache, write highly readable code and perform error handling.

How do you prevent SQL Injection in PHP? (Prepared statements, PDO)How do you prevent SQL Injection in PHP? (Prepared statements, PDO)Apr 15, 2025 am 12:15 AM

Using preprocessing statements and PDO in PHP can effectively prevent SQL injection attacks. 1) Use PDO to connect to the database and set the error mode. 2) Create preprocessing statements through the prepare method and pass data using placeholders and execute methods. 3) Process query results and ensure the security and performance of the code.

PHP and Python: Code Examples and ComparisonPHP and Python: Code Examples and ComparisonApr 15, 2025 am 12:07 AM

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

PHP in Action: Real-World Examples and ApplicationsPHP in Action: Real-World Examples and ApplicationsApr 14, 2025 am 12:19 AM

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP: Creating Interactive Web Content with EasePHP: Creating Interactive Web Content with EaseApr 14, 2025 am 12:15 AM

PHP makes it easy to create interactive web content. 1) Dynamically generate content by embedding HTML and display it in real time based on user input or database data. 2) Process form submission and generate dynamic output to ensure that htmlspecialchars is used to prevent XSS. 3) Use MySQL to create a user registration system, and use password_hash and preprocessing statements to enhance security. Mastering these techniques will improve the efficiency of web development.

PHP and Python: Comparing Two Popular Programming LanguagesPHP and Python: Comparing Two Popular Programming LanguagesApr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

The Enduring Relevance of PHP: Is It Still Alive?The Enduring Relevance of PHP: Is It Still Alive?Apr 14, 2025 am 12:12 AM

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.