php SQL防注入代码集合

时间:2023-11-18 03:58:34 

SQL防注入代码一


<?php
/**
* 防sql注入
* @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 *************************/
?>



<?php
/**
* 防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 防止sql注入函数代码二:


<?php
$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 防止sql注入代码三


function inject_check($sql_str) { //防止注入
$check = eregi('select|insert|update|delete|'|/*|*|../|./|union|into|load_file|outfile', $sql_str);
if ($check) {
echo "输入非法注入内容!";
exit ();
} else {
return $sql_str;
}
}
function checkurl() { //检查来路
if (preg_replace("/https教程?://([^:/]+).*/i", "1", $_server['http_referer']) !== preg_replace("/([^:]+).*/", "1", $_server['http_host'])) {
header("location: http://s.aspxhome.com");
exit();
}
}
//调用
checkurl();
$str = $_get['url'];
inject_check($sql_str);//这条可以在获取参数时执行操作


标签:php,SQL,防注入
0
投稿

猜你喜欢

  • 简单介绍Python中的readline()方法的使用

    2023-11-02 13:34:30
  • OpenCV立体图像深度图Depth Map基础

    2021-09-23 22:12:59
  • Python快速生成定制化的Word(docx)文档

    2022-04-27 12:33:06
  • PHP PDOStatement::setAttribute讲解

    2023-06-04 02:48:47
  • 微信小程序创建自定义全局函数以及其调用方法详解

    2023-08-24 20:43:22
  • python实现随机梯度下降法

    2023-11-02 16:55:37
  • IE的有条件注释详解(附实例代码)

    2009-03-31 13:01:00
  • 简单介绍Python虚拟环境及使用方法

    2021-03-19 20:59:53
  • 解决Alexnet训练模型在每个epoch中准确率和loss都会一升一降问题

    2022-12-06 16:17:37
  • python+opencv实现霍夫变换检测直线

    2021-07-30 04:08:57
  • python 每天如何定时启动爬虫任务(实现方法分享)

    2022-11-28 21:14:35
  • Go语言中数组的基本用法演示

    2024-02-09 21:26:34
  • 使用Nginx+uWsgi实现Python的Django框架站点动静分离

    2023-11-13 11:30:37
  • 微前端qiankun沙箱实现源码解读

    2024-05-02 16:10:25
  • Python datetime时间格式化去掉前导0

    2022-03-15 11:43:15
  • vue函数防抖与节流的正确使用方法

    2024-05-29 22:43:01
  • SQL SERVER触发器详解

    2024-01-22 01:50:00
  • Python Django 命名空间模式的实现

    2023-10-06 05:34:28
  • Python编程之event对象的用法实例分析

    2023-02-19 09:29:50
  • Python运算符重载用法实例分析

    2023-03-18 07:28:11
  • asp之家 网络编程 m.aspxhome.com