Home > Backend Development > PHP Tutorial > PHP SQL anti-injection code collection_PHP tutorial

PHP SQL anti-injection code collection_PHP tutorial

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-07-21 15:52:03
Original
1171 people have browsed it

SQL anti-injection code one

Copy code The code is as follows:

/**
* Prevent sql injection
* @author: zhuyubing@gmail.com
**/
/**
* reject sql inject
*/
if (!function_exists (quote))
{
function quote($var)
{
if (strlen($var))
{
$var=!get_magic_quotes_gpc() ? $var : stripslashes($var);
$var = str_replace("'","'" ,$var);
}
return "'$var'";
}
}
if (!function_exists (hash_num)){
function hash_num($input)
{
$hash = 5381;
for ($i = 0; $i < strlen($str); $i++)
{
$c = ord($str{$ i});
$hash = (($hash << 5) + $hash) + $c;
}
return $hash;
}
}
/**************** end *************************/
?>


Copy code The code is as follows:

/**
* 防sql测试代码
CREATE TABLE IF NOT EXISTS `tb` (
`id` int(10) unsigned NOT NULL auto_increment,
`age` tinyint(3) unsigned NOT NULL,
`name` char(100) NOT NULL,
`note` text NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 ;
**/
include_once('common.php');
var_dump(hash_num('dddd'));
if( empty($_GET))
{
$_GET = array('age'=>'99','name'=>'a'b\'c";','note'=> ;"a'b'nc#");
}
$age = (int)$_GET['age'];
$name = quote($_GET['name']);
$note = quote($_GET['note']);
$sql = "INSERT INTO `tb` ( `age`, `name`, `note`) VALUES
( $age, $ name, $note)";
var_dump($sql);
?>

PHP prevent sql injection function code 2:
Copy code The code is as follows:

$magic_quotes_gpc = get_magic_quotes_gpc();
@extract(daddslashes($ _COOKIE));
@extract(daddslashes($_POST));
@extract(daddslashes($_GET));
if(!$magic_quotes_gpc) {
$_FILES = daddslashes($_FILES );
}

function daddslashes($string, $force = 0) {
if(!$GLOBALS['magic_quotes_gpc'] || $force) {
if(is_array( $string)) {
foreach($string as $key => $val) {
$string[$key] = daddslashes($val, $force);
}
} else {
$string = addslashes($string);
}
}
return $string;
}
?>

PHP prevents sql injection code three
Copy code The code is as follows:

function inject_check($sql_str) { // Prevent injection
$check = eregi('select|insert|update|delete|'|/*|*|../|./|union|into|load_file|outfile', $sql_str);
if ($check) {
echo "Enter illegal injection content! ";
exit ();
} else {
return $sql_str;
}
}
function checkurl() { //Check the source
if (preg_replace( "/httpstutorial?://([^:/]+).*/i", "1", $_server['http_referer']) !== preg_replace("/([^:]+).* /", "1", $_server['http_host'])) {
header("location: http://s.jb51.net");
exit();
}
}
//Call
checkurl();
$str = $_get['url'];
inject_check($sql_str);//This can be performed when getting parameters

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/319028.htmlTechArticleSQL anti-injection code 1 copy code is as follows: ?php /*** Prevent sql injection * @author: zhuyubing@gmail.com **/ /*** reject sql inject*/ if (!function_exists (quote)) { function quo...
Related labels:
source:php.cn
Statement of this Website
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
Latest Issues
php data acquisition?
From 1970-01-01 08:00:00
0
0
0
PHP extension intl
From 1970-01-01 08:00:00
0
0
0
How to learn php well
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template