How does a php website prevent SQL injection? -Php Tutorial

Source: Internet
Author: User
How does a php website prevent SQL injection?
Website operation security must be considered by every Webmaster. as you know, most websites attacked by hackers use SQL injection, which is what we often say?

The most primitive static website is the safest. Today we will talk about PHP injection security specifications to prevent your website from being injected with SQL statements.

Today's mainstream website development language is php. let's start with how php websites prevent SQL injection:

Through the above process, we can understand the principles and techniques of Php injection. of course, we can also work out the appropriate defense methods:
The first step is to set the server security. here, we mainly set the security of php + mysql and the security of linux hosts. To prevent injection of php + mysql, set magic_quotes_gpc to On and display_errs to Off. if the id type is set, we use intval () to convert it to an integer type, such as code:



$ Idintval ($ id );
Mysql_query "* fromexamplewherearticieid '$ ID'"; or write: mysql_query ("SELECT * FROMarticleWHEREarticleid". intval ($ id )."")
If it is a replica type, use addslashes () to filter it, and then filter "%" and "_", such:
$ Searchaddslashes ($ search );
$ Searchstr_replace ("_", "\ _", $ search );
$ Searchstr_replace ("%", "\ %", $ search );
Of course, you can also add php universal anti-injection code:
/*************************
PHP general anti-injection security code
Note:
Determines whether the passed variable contains invalid characters.
Such as $ _ POST and $ _ GET
Function:
Anti-injection

  1. **************************/
  2. // Invalid characters to be filtered
  3. $ ArrFiltratearray ("'", ";", "union ");
  4. // The url to be redirected after an error occurs. If this parameter is left blank, the previous page is displayed by default.
  5. $ StrGoUrl "";
  6. // Whether the value in the array exists
  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. /*************************

Save as checkpostget. php
Add include ("checkpostget. php") before each php file.
**************************/
In addition, the administrator username and password are encrypted using md5, which effectively prevents php injection.
There are also servers and mysql to enhance security.
For linux server security settings:
Use the "/usr/sbin/authconfig" tool to enable the password shadow function and encrypt passwd.
Prohibit access to important files. go to the linux command interface and enter:
# Chmod600/etc/inetd. conf // change the file attribute to 600
# Chattr + I/etc/inetd. conf // ensure that the file owner is root
# Chattr-I/etc/inetd. conf // restrict changes to this file
Prohibit any user from changing to a root user through the su command
Add the following two lines at the beginning of the su configuration file/etc/pam. d:
Auth sufficient/lib/security/pam_rootok.sodebug
Auth required/lib/security/pam_whell.sogroupwheel
Delete all special accounts
# Deleting users such as userdel lp
# Groupdellp and so on
Disable 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 variable contains illegal characters, we put the following code into a public file, such as security. inc. in php, every file contains this file, so that all variables submitted by any program can be filtered out, which achieves our effect once and for all.



Brief description :/*************************
Note:
Determines whether the passed variable contains invalid characters.
Such as $ _ POST and $ _ GET
Function: Anti-injection
**************************/

The code is as follows:


  1. // Invalid characters to be filtered
  2. $ ArrFiltratearray ("", ";", "union ");
  3. // The url to be redirected after an error occurs. If this parameter is left blank, the previous page is displayed by default.
  4. $ StrGoUrl "";
  5. // Whether the value in the array exists
  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. }
  21. Feach ($ HTTP_GET_VARSas $ key> $ value ){
  22. $ ArrPostAndGet [] $ value;
  23. }
  24. }

  25. // Verification starts
  26. Feach ($ ArrPostAndGetas $ key> $ value ){
  27. If (FunStringExist ($ value, $ ArrFiltrate )){
  28. Echo" Alert (\ "invalid character \"); script ";
  29. If (emptyempty ($ StrGoUrl )){
  30. Echo" Histy. go (-1); script ";
  31. } Else {
  32. Echo" Window. location \ "". $ StrGoUrl. "\"; script ";
  33. }
  34. Exit;
  35. }
  36. }
  37. ?>

Save as checkpostget. php
Add include ("checkpostget. php") before each php file.

Method 2


The code is as follows:

  1. /* Filter all GET 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 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 filter function
  26. Functionget_str ($ string)
  27. {
  28. If (! Get_magic_quotes_gpc ()){
  29. Returnaddslashes ($ string );
  30. }
  31. Return $ string;
  32. }

The first method is to escape data.

The second method is written in a separate file, and each php file is introduced.

You can escape each 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 removed from the top of the topic by Beckham at, September 20 ,.

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.