As more and more projects begin to use frameworks, many programmers are not concerned about security issues! Because the framework has helped us deal with it almost perfectly! However, I personally think that we still need to know the common security processing functions! Although various development frameworks provide us with good security solutions, we should pay attention to security issues.
The reason is simple: many small functions and projects cannot use the framework. we need to solve the security problem on our own!
① Common security functions:
The code is as follows:
Mysql_real_escape_string ()
Addslashes ()
② Functions:
The mysql_real_escape_string () and addslashes () functions escape single quotes and double quotes in data! That is to prevent SQL injection!
However, mysql_real_escape_string () takes into account the character set, which is more secure!
After reading the relevant information, we can draw a conclusion: if the current character set is single-byte, the two functions share the same role and can play the role of escape filtering. However, who will only use a single byte? Especially utf8 is widely used!
③ Function usage:
When you understand the usage of mysql_real_escape_string () and addslashes () functions, you must first understand the meanings of the other two functions!
Get_magic_quotes_gpc () and get_magic_quotes_runtime (). let's compare the similarities and differences between the two functions:
Same:
A. Both are used to obtain the configuration of the php. ini configuration file! 1 is returned when it is enabled, and 0 is returned when it is disabled!
B. When enabled, escape filtering is performed for data within the specified range!
Different:
A. The two have different scopes:
Magic_quotes_gpc mainly applies to web clients. In short, it mainly filters data in $ _ GET, $ _ POST, and $ _ COOKE!
Magic_quotes_runtime is mainly used to filter data read from files or data obtained from database queries!
B. The Setting time is different:
Magic_quotes_gpc cannot be set at runtime [this setting can only be read when the server is restarted]
Magic_quotes_runtime can be set at runtime!
Note: the so-called runtime setting means that we can use the ini_set () function to set the configuration in the php. ini file in the script!
After understanding the functions get_magic_quotes_gpc () and get_magic_quotes_runtime (), we should understand: when we detect php. if magic_quotes_runtime and magic_quotes_gpc are enabled in the ini configuration file, data within the specified range is automatically escaped! If it is disabled, we need to use mysql_real_escape_string () [or the addslashes function for filtering]
④ Example:
1. whether magic_quotes_gpc or magic_quotes_runctime is enabled or disabled, the getdata. php script can transfer data securely. the content is as follows:
The code is as follows:
<? Php
If (! Get_magic_quotes_gpc ()){
Foreach ($ _ GET as $ key =>&$ value ){
$ Value = mysql_real_escape_string ($ value );
}
}
If (! Get_magic_quotes_runtime ()){
$ SourceDataRecord = file ('./sourceData.txt ');
If ($ sourceDataRecord ){
Foreach ($ sourceDataRecord as $ sourceKey => & $ sourceValue ){
$ SourceValue = mysql_real_escape_string ($ sourceValue );
}
}
}
$ GpcData = implode ("\ t", $ _ GET );
$ RuntimeData = implode ("\ t", $ sourceDataRecord );
Echo $ gpcData, $ runtimeData;
?>
The content in sourcedata.txt is as follows:
The code is as follows:
"It's anoterh Sunday afternoon
I'm trunning turnning... turnning around"
The url when we request getData. php is as follows:
Php.test.com/safe/getdata.php? Name = maw'eibin & age = 25
The execution result is as follows:
The code is as follows:
Maw \ 'ibin 25 \ "It \'s anoterh Sunday afternoon \ r \ n I \'m trunning
Turnning... turnning around \"
⑤ References:
Http://www.jb51.net/article/35868.htm