


Detailed explanation of PHP magic function application_PHP tutorial
PHP provides two magic reference functions magic_quotes_gpc and magic_quotes_runtime that are convenient for us to quote data. If these two functions are set to ON in php.ini, they will encounter single quotes ' and ' for the data we quote. Double quotation marks" and backslashes are automatically added to help us automatically translate symbols and ensure the correct operation of data operations. However, under different versions of PHP or different server configurations, some magic_quotes_gpc and magic_quotes_runtime are set to on. , and some are off, so the program we write must comply with both on and off conditions. So what is the difference between the two functions magic_quotes_gpc and magic_quotes_runtime? See the following description:
magic_quotes_gpc
The scope is: WEBclient server;
Action time: the request starts, for example, when the script is running.
magic_quotes_runtime
Scope of action: Data read from a file or the result of executing exec() or obtained from a SQL query;
Time of action: Every time the script accesses data generated in the running state.
So the setting value of magic_quotes_gpc will affect the data obtained through Get/Post/Cookies; the setting value of magic_quotes_runtime will affect the data read from the file or the data obtained from the database query
Example. Note:
<form action="" method="post" >STR:<input type="text" name="str"><input type="submit"></form><?php/* 我们在表单里填写: '" 这些符号,如果magic_quotes_gpc没有开启,那么他们不会被反斜杠转义 */echo '现在通过POST传递过来的值是:' ,$_POST['str'], '<br />';if (get_magic_quotes_gpc()) { // 检查magic_quotes_gpc是否打开,如果没有打开,用addslashes进行转义 $str = $_POST['str'];} else { $str = addslashes($_POST['str']);}echo '这里是转义过后的:' ,$str, '<hr />';$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 '<br />SQL语句为:<br />' ,$sql;//---入库都转义了,但是多余了反斜杠,我们要读出来是原来的数据时候使用stripslashes()去掉反斜杠//---stripslashes()和addslashes()作用相反?> Copy after login |
The most critical difference is the two points mentioned above: they target different processing objects
The setting value of magic_quotes_gpc will affect the use of Get/Post/Cookies Obtained data
The setting value of magic_quotes_runtime will affect the data read from the file or the data obtained from the database query
By the way, here are a few functions that I want to associate:
set_magic_quotes_runtime():
Set the magic_quotes_runtime value. 0=off. 1=on. The default state is off. You can view magic_quotes_runtime through echo phpinfo();
get_magic_quotes_gpc():
magic_quotes_gpc value.0=off.1=on.
get_magic_quotes_runtime():
View magic_quotes_runtime value. 0=off. 1=on.
Note that there is no set_magic_quotes_gpc() function, that is, the value of magic_quotes_gpc cannot be set in the program.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











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.

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 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.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

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 is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

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.

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.
