php防注入代码
关于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;
- }
- ?>
防注入二,代码如下:
- 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://www.phpfensi.com");
- exit();
- }
- }