Home > Backend Development > PHP Tutorial > How to prevent SQL injection in PHP+Mysql

How to prevent SQL injection in PHP+Mysql

不言
Release: 2023-03-24 12:42:01
Original
24661 people have browsed it

The content of this article is about the method of preventing SQL injection in PHP Mysql. It has a certain reference value. Now I share it with you. Friends in need can refer to

Related mysql video tutorial recommendations: " mysql tutorial

Method 1:

mysql_real_escape_string -- Escape special characters in the string used in the SQL statement, taking into account the current connection character set!
$sql = "select count(*) as ctr from users where username
='".mysql_real_escape_string($username)."' and 
password='". mysql_real_escape_string($pw)."' limit 1";
Copy after login

Method 2:

Open magic_quotes_gpc to prevent SQL injection. There is a setting in php.ini: magic_quotes_gpc = Off. This is turned off by default. If it is turned on, it will automatically convert the SQL query submitted by the user, such as converting ' to \', etc., which plays a significant role in preventing SQL injection.

If magic_quotes_gpc=Off, use the addslashes() function.

Method 3:

Custom function

/**
* 防止sql注入自定义方法一
* author: xiaochuan
* @param: mixed $value 参数值
*/  
function check_param($value=null) { 
        #  select|insert|update|delete|\'|\/\*|\*|\.\.\/|\.\/|union|into|load_file|outfile
    $str = 'select|insert|and|or|update|delete|\'|\/\*|\*|\.\.\/|\.\/|union|into|load_file|outfile';
 
    if(!$value) {
 
        exit('没有参数!'); 
 
    }elseif(eregi($str, $value)) { 
 
        exit('参数非法!');
 
    }
 
    return true; 
} 
  
 
 
 
 
 
 
/**
* 防止sql注入自定义方法二
* author: xiaochuan
* @param: mixed $value 参数值
*/ 
function str_check( $value ) { 
 
    if(!get_magic_quotes_gpc()) { 
 
        // 进行过滤 
        $value = addslashes($value); 
 
    } 
 
    $value = str_replace("_", "\_", $value); 
 
    $value = str_replace("%", "\%", $value); 
      
   return $value; 
} 
  
 
 
 
 
 
/**
* 防止sql注入自定义方法三
* author: xiaochuan
* @param: mixed $value 参数值
*/ 
function post_check($value) { 
 
    if(!get_magic_quotes_gpc()) {
 
        // 进行过滤  
        $value = addslashes($value);
 
    } 
 
    $value = str_replace("_", "\_", $value); 
 
    $value = str_replace("%", "\%", $value); 
 
    $value = nl2br($value); 
 
    $value = htmlspecialchars($value); 
 
    return $value; 
}
Copy after login

The above is the detailed content of How to prevent SQL injection in PHP+Mysql. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template