Home >Backend Development >PHP Tutorial >How to prevent sql injection in php website?

How to prevent sql injection in php website?

WBOY
WBOYOriginal
2016-07-25 09:13:401224browse
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
  1. *******************************/
  2. //Illegal characters to be filtered
  3. $ArrFiltratearray("'",";","union");
  4. //The url to be redirected after an error occurs, if not filled in Default previous page
  5. $StrGoUrl"";
  6. //Whether there is a value in the array
  7. functionFunStringExist($StrFiltrate,$ArrFiltrate){
  8. feach($ArrFiltrateas$key>$value){
  9. if(eregi ($value,$StrFiltrate)){
  10. returntrue;
  11. }
  12. }
  13. returnfalse;
  14. }
  15. //Merge $_POST and $_GET
  16. if(function_exists(array_merge)){
  17. $ArrPostAndGetarray_ merge ($HTTP_POST_VARS,$HTTP_GET_VARS);
  18. }else{
  19. feach($HTTP_POST_VARSas$key>$value){
  20. $ArrPostAndGet[]$value;
  21. }
  22. feach($HTTP_GET_VARSas$key>$value) {
  23. $ArrPostAndGet[]$value;
  24. }
  25. }
  26. //Verification starts
  27. feach($ArrPostAndGetas$key>$value){
  28. if(FunStringExist($value,$ArrFiltrate)){
  29. echo "alert(/"Neeao prompt, illegal character/");";
  30. if(empty($StrGoUrl)){
  31. echo "histy.go(-1);";
  32. }else{
  33. echo "window.location/"".$StrGoUrl."/";";
  34. }
  35. exit;
  36. }
  37. }
  38. ?>
  39. /*********** ******************
Copy code
Save as checkpostget.php
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:

  1. //Illegal characters to be filtered
  2. $ArrFiltratearray("",";","union");
  3. //URL to be redirected after an error , if not filled in, it will default to the previous page
  4. $StrGoUrl"";
  5. //Whether there is a value in the array
  6. functionFunStringExist($StrFiltrate,$ArrFiltrate){
  7. feach($ArrFiltrateas$key>$value){
  8. if(eregi($value,$StrFiltrate)){
  9. returntrue;
  10. }
  11. }
  12. returnfalse;
  13. }

  14. //Merge $_POST and $_GET
  15. if(function_exists( array_merge) ){
  16. $ArrPostAndGetarray_merge($HTTP_POST_VARS,$HTTP_GET_VARS);
  17. } else{
  18. feach($HTTP_POST_VARSas$key>$value){
  19. $ArrPostAndGet[]$value;
  20. }
    feach($HTTP_GET_VARSas $key>$value){

  21. $ArrPostAndGet[]$value;

  22. }

  23. }


  24. //Verification starts

  25. feach($ArrPostAndGetas$key>$value){

  26. if(FunStringExist($ value,$ArrFiltrate)){

  27. echo"alert("Illegal character");";

  28. if(emptyempty($StrGoUrl)){

  29. echo"< ;scriptlanguage"javascript">histy.go(-1);";

  30. }else{

  31. echo"window.location"".$StrGoUrl."" ;";

  32. }

  33. exit;

  34. }

  35. }

  36. ?>


Copy the codeSave as checkpostget.php
Then add include("checkpostget.php"); in front of each php file


Method 2

The code is as follows:
    /*Filter all GET variables*/

  1. feach($_GETas$get_key>$get_var)

  2. {

  3. if(is_numeric($get_var)){

  4. $get[strtolower($ get_key)]get_int($get_var);

  5. }else{

  6. $get[strtolower($get_key)]get_str($get_var);

  7. }

  8. }


  9. /*Filter all POST variables* /

  10. feach($_POSTas$post_key>$post_var)

  11. {

  12. if(is_numeric($post_var)){

  13. $post[strtolower($post_key)]get_int($post_var);

  14. }else{

  15. $post[strtolower($post_key)]get_str($post_var);

  16. }

  17. }


  18. /*Filter function*/

  19. //Integer filter function

  20. functionget_int($number)

  21. {

  22. returnintval($number);

  23. }

  24. //String filter function

  25. functionget_str($string)

  26. {

  27. if(!get_magic_quotes_gpc()){

  28. returnaddslashes($string);

  29. }

  30. return$string;

  31. }
Copy codeThe first one is the method of escaping data

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);

}

This topic was unpinned by Xiaobei on 2015-9-20 13:05


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