How does a PHP Web site prevent SQL injection?

Source: Internet
Author: User
The operation of the site is sure that every webmaster must consider the issue, you know, most hackers attack the site is the use of SQL injection, this is what we often say why?

The most original static website is the safest. Today we talk about PHP injection security specifications to prevent your site from being injected into SQL.

Now that the mainstream web development language is PHP, let's start with how the PHP Web site prevents SQL injection:

PHP injection security Through the above process, we can understand the principles and methods of PHP injection, of course, we can also work out the corresponding prevention method:
The first is the security settings for the server, which is primarily Php+mysql security settings and Linux Host Security settings. For Php+mysql injection prevention, first set MAGIC_QUOTES_GPC to On,display_errs set to OFF, if ID type, we use Intval () to convert it to an integer type, such as code:



$idintval ($id);
Mysql_query "*fromexamplewherearticieid ' $id '"; or write this: mysql_query ("Select*fromarticlewherearticleid". Intval ($id). "")
If it is a character type, filter it with addslashes () 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-inject security code
Description
Determine if the passed variable contains illegal characters
such as $_post, $_get
Function:
Anti-injection
  1. **************************/
  2. Illegal characters to filter on
  3. $ArrFiltratearray ("'", ";", "union");
  4. The URL to jump after the error, without filling the default previous page
  5. $StrGoUrl "";
  6. Whether the values in the array exist
  7. Functionfunstringexist ($StrFiltrate, $ArrFiltrate) {
  8. Feach ($ArrFiltrateas $key> $value) {
  9. if (eregi ($value, $StrFiltrate)) {
  10. Returntrue;
  11. }
  12. }
  13. Returnfalse;
  14. }
  15. Merging $_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. Validation begins
  27. Feach ($ArrPostAndGetas $key> $value) {
  28. if (Funstringexist ($value, $ArrFiltrate)) {
  29. echo "Alert (/" NEEAO hint, 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 CodeSave As Checkpostget.php
Then add an include ("checkpostget.php") to each PHP file;
**************************/
In addition, the Administrator user name and password are taken MD5 encryption, so as to effectively prevent the injection of PHP.
There are also servers and MySQL to strengthen some security precautions.
Security settings for Linux servers:
Encrypt the password and use the/usr/sbin/authconfig tool to turn on the shadow function of the passcode and encrypt the passwd.
Disable access to important files, enter the Linux command interface, and enter at the prompt:
#chmod600/etc/inetd.conf//Change the file property to 600
#chattr +i/etc/inetd.conf//Guarantee file owner is root
#chattr –i/etc/inetd.conf//restrictions on changes to this file
Prevents any user from changing to the root user through the SU command
Add the following two lines at the beginning of the SU configuration file, which 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 and so on delete user
#groupdellp等等删除组
Prohibit Suid/sgid programs that are not used
#find/-typef\ (-perm-04000-o–perm-02000\) \-execls–lg{}\;



Http://hi.baidu.com/bigideaer/bl ... 7e76e11a4cffd0.html

Determine if the passed variable contains illegal characters we put the following code in a common file, For example, in the security.inc.php, each file contains the file, then you can give any one of the program to submit all the variables to filter, to achieve our effect once and for all.



Description:/*************************
Description
Determine if the passed variable contains illegal characters
such as $_post, $_get
Function: Anti-injection
**************************/

The code is as follows:

  1. Illegal characters to filter on
  2. $ArrFiltratearray ("", ";", "union");
  3. The URL to jump after the error, without filling the default previous page
  4. $StrGoUrl "";
  5. Whether the values in the array exist
  6. Functionfunstringexist ($StrFiltrate, $ArrFiltrate) {
  7. Feach ($ArrFiltrateas $key> $value) {
  8. if (eregi ($value, $StrFiltrate)) {
  9. Returntrue;
  10. }
  11. }
  12. Returnfalse;
  13. }

  14. Merging $_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. }
  21. Feach ($HTTP _get_varsas$key> $value) {
  22. $ArrPostAndGet [] $value;
  23. }
  24. }

  25. Validation begins
  26. Feach ($ArrPostAndGetas $key> $value) {
  27. if (Funstringexist ($value, $ArrFiltrate)) {
  28. echo "";
  29. if (Emptyempty ($STRGOURL)) {
  30. echo "";
  31. }else{
  32. echo "";
  33. }
  34. Exit
  35. }
  36. }
  37. ?>

Copy CodeSave As Checkpostget.php
Then add an include ("checkpostget.php") to each PHP file;

Method 2


The code is as follows:
  1. /* Filter all get over variables */
  2. Feach ($_getas$get_key> $get _var)
  3. {
  4. if (Is_numeric ($get _var)) {
  5. $get [Strtolower ($get _key)]get_int ($get _var);
  6. }else{
  7. $get [Strtolower ($get _key)]get_str ($get _var);
  8. }
  9. }

  10. /* Filter all post-over variables */
  11. Feach ($_postas$post_key> $post _var)
  12. {
  13. if (Is_numeric ($post _var)) {
  14. $post [Strtolower ($post _key)]get_int ($post _var);
  15. }else{
  16. $post [Strtolower ($post _key)]get_str ($post _var);
  17. }
  18. }

  19. /* Filter Function */
  20. Integer Filter function
  21. Functionget_int ($number)
  22. {
  23. Returnintval ($number);
  24. }
  25. string-Type Filter function
  26. Functionget_str ($string)
  27. {
  28. if (!GET_MAGIC_QUOTES_GPC ()) {
  29. Returnaddslashes ($string);
  30. }
  31. return$string;
  32. }
Copy CodeThe first is the way to escape the data

The second method, written in a separate file, is introduced into each PHP file.

Can be implemented to escape each data processing

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 released by Beckham on 2015-9-20 13:05
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.