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.

today's mainstream Website Development language or 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 the 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

**************************/

illegal characters to filter on

$ArrFiltratearray ("'", ";", "union");

the URL to jump after the error , without filling the default previous page

$StrGoUrl "";

whether the values in the array exist

Functionfunstringexist ($StrFiltrate, $ArrFiltrate) {

Feach ($ArrFiltrateas $key> $value) {

if (eregi ($value, $StrFiltrate)) {

Returntrue;

}

}

Returnfalse;

}

merging $_post and $_get

if (function_exists (Array_merge)) {

$ArrPostAndGetarray _merge ($HTTP _post_vars, $HTTP _get_vars);

}else{

Feach ($HTTP _post_varsas$key> $value) {

$ArrPostAndGet [] $value;

}

Feach ($HTTP _get_varsas$key> $value) {

$ArrPostAndGet [] $value;

}

}

validation begins

Feach ($ArrPostAndGetas $key> $value) {

if (Funstringexist ($value, $ArrFiltrate)) {

echo"alert (/"NEEAO hint, illegal character /"); ";

if (empty ($STRGOURL)) {

echo "Histy.go (-1);";

}else{

echo "window.location/" ". $StrGoUrl." /”;”;

}

Exit

}

}

?>

/*************************

Save As checkpostget.php

Then add an include ("checkpostget.php") to each php file ; can be

**************************/

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 ofthe 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 the 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 and so on delete group

Prohibit suid/sgid programs that are not used

#find/-typef\ (-perm-04000-o–perm-02000\) \-execls–lg{}\;

determine if the passed variable contains illegal characters we put the following code in a common file, such as security.inc.php inside, each file includes this file, Then it is possible to filter all the variables that are submitted by any one program, so that we can achieve this effect once and for all.

Description:/*************************

Description

Determine if the passed variable contains illegal characters

such as $_post,$_get

Function: Anti-injection

**************************/

illegal characters to filter on

$ArrFiltratearray ("", ";", "union");

the URL to jump after the error , without filling the default previous page

$StrGoUrl "";

whether the values in the array exist

Functionfunstringexist ($StrFiltrate, $ArrFiltrate) {

Feach ($ArrFiltrateas $key> $value) {

if (eregi ($value, $StrFiltrate)) {

Returntrue;

}

}

Returnfalse;

}

merging $_post and $_get

if (function_exists (Array_merge)) {

$ArrPostAndGetarray _merge ($HTTP _post_vars, $HTTP _get_vars);

}else{

Feach ($HTTP _post_varsas$key> $value) {

$ArrPostAndGet [] $value;

}

Feach ($HTTP _get_varsas$key> $value) {

$ArrPostAndGet [] $value;

}

}

validation begins

Feach ($ArrPostAndGetas $key> $value) {

if (Funstringexist ($value, $ArrFiltrate)) {

echo "alert (\" illegal character \ ");";

if (Emptyempty ($STRGOURL)) {

echo "Histy.go (-1);";

}else{

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

}

Exit

}

}

?>

Save As checkpostget.php

Then add an include ("checkpostget.php") to each php file ; can be

Method 2

The code is as follows :

/* Filter all GET over variables */

Feach ($_getas$get_key> $get _var)

{

if (Is_numeric ($get _var)) {

$get [Strtolower ($get _key)]get_int ($get _var);

}else{

$get [Strtolower ($get _key)]get_str ($get _var);

}

}

/* Filter all POST -Over variables */

Feach ($_postas$post_key> $post _var)

{

if (Is_numeric ($post _var)) {

$post [Strtolower ($post _key)]get_int ($post _var);

}else{

$post [Strtolower ($post _key)]get_str ($post _var);

}

}

/* Filter function */

Integer filter function

Functionget_int ($number)

{

Returnintval ($number);

}

string-type filter function

Functionget_str ($string)

{

if (!GET_MAGIC_QUOTES_GPC ()) {

Returnaddslashes ($string);

}

return$string;

}

The first is the way to escape the data

The second method, written in a separate file, introduces each of the in 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);

}

Free pick up Brother even PHP Original video tutorial CD-ROM, more information on the website customer service:

Http://www.lampbrother.net


How does a PHP Web site prevent SQL injection?

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.