Home >Backend Development >PHP Tutorial >How to prevent sql injection in php website?
The operational security of the website is definitely an issue that every webmaster must consider. As we all know, most hackers attack websites by using SQL injection. This is what we often say why? The most original static website is the safest. Today we will talk about the security specifications of PHP injection to prevent your website from being injected by SQL. Nowadays, the mainstream website development language is still PHP, so let’s start with how to prevent SQL injection on PHP websites: Security Prevention of Php Injection Through the above process, we can understand the principles and techniques of php injection. Of course, we can also develop corresponding prevention methods: The first is the security settings of the server. Here are mainly the security settings of php+mysql and the security settings of the Linux host. To prevent php+mysql injection, first set magic_quotes_gpc to On and display_errs to Off. If it is an id type, we use intval() to convert it into an integer type, as shown in the code: $idintval($id); mysql_query”*fromexamplewherearticieid’$id’”; Or write like this: mysql_query(”SELECT*FROMarticleWHEREarticleid”.intval($id).””) If it is a character type, use addslashes() to filter it, and then filter "%" and "_", such as: $searchaddslashes($search); $searchstr_replace(“_”,”_”,$search); $searchstr_replace(“%”,”%”,$search); Of course, you can also add PHP universal anti-injection code: /***************************** PHP universal anti-injection security code Description: Determine whether the passed variable contains illegal characters Such as $_POST, $_GET Function: Anti-injection
Then add include("checkpostget.php"); before each php file ****************************/ In addition, the administrator username and password are md5 encrypted, which can effectively prevent PHP injection. There are also some security precautions that need to be strengthened on the server and mysql. For security settings of linux server: To encrypt the password, use the "/usr/sbin/authconfig" tool to turn on the password shadow function and encrypt passwd. To prohibit access to important files, enter the Linux command interface and enter at the prompt: #chmod600/etc/inetd.conf//Change the file attributes to 600 #chattr+I /etc/inetd.conf // Ensure that the file owner is root #chattr–I /etc/inetd.conf //Restrict changes to this file It is forbidden for any user to change to the root user through the su command Add the following two lines at the beginning of the su configuration file, that is, the /etc/pam.d/ directory: Auth sufficient /lib/security/pam_rootok.sodebug Auth required /lib/security/pam_whell.sogroupwheel Delete all special accounts #userdel lp etc. Delete user #groupdellpetc delete group Ban unused suid/sgid programs #find/-typef(-perm-04000 -o–perm-02000)-execls–lg{}; http://hi.baidu.com/bigideaer/bl... 7e76e11a4cffd0.html To determine whether the passed variables contain illegal characters, we put the following code into a public file, such as security.inc.php. Include this file in each file, then all the files submitted to any program can be After the variables are filtered, we achieve the effect once and for all. Brief description: /*************************** Description: Determine whether the passed variable contains illegal characters Such as $_POST, $_GET Function: Anti-injection **************************/ The code is as follows:
Method 2 The code is as follows:
The second method is written in a separate file and imported into every PHP file Then you can escape every data functionsaddslashes($string){ if(is_array($string)){ feach($stringas$key>$val){ $string[$key]saddslashes($val); } }else{ $stringaddslashes($string); } return$string; } ################################################ ############### $magic_quoteget_magic_quotes_gpc(); if(empty($magic_quote)){ $_GETsaddslashes($_GET); $_POSTsaddslashes($_POST); } |