php用户自定过滤非法sql注入字符串函数

 更新时间:2016年11月25日 15:25  点击:2017
 代码如下 复制代码

function uc_addslashes($string, $force = 0, $strip = false) {
 !defined('magic_quotes_gpc') && define('magic_quotes_gpc', get_magic_quotes_gpc());
 if(!magic_quotes_gpc || $force) {
  if(is_array($string)) {
   foreach($string as $key => $val) {
    $string[$key] = uc_addslashes($val, $force, $strip);
   }
  } else {
   $string = addslashes($strip ? strips教程lashes($string) : $string);
  }
 }
 return $string;
}

if(!function_exists('daddslashes')) {
 function daddslashes($string, $force = 0) {
  return uc_addslashes($string, $force);
 }
}

//php 过滤函数应用实例111cn.net

$get = $_get;
$g = uc_addslashes($get, $force = 0, $strip = false);

//过滤post提交数据

$post =  $_post;
$p = uc_addslashes($post, $force = 0, $strip = false);

这里二个函数是利用正则过滤sql关键词,并判断用户提交信息的来路进行处理,可以有效的防止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://www.111cn.net");
   exit();
  }
 }

 

方法一过滤html自定义函数

 代码如下 复制代码
function ihtmlspecialchars($string) {
if(is_array($string)) {
foreach($string as $key => $val) {
$string[$key] = ihtmlspecialchars($val);
}
} else {
$string = preg_replace('/&((#(d{3,5}|x[a-fa-f0-9]{4})|[a-za-z][a-z0-9]{2,5});)/', '&\1',
str_replace(array('&', '"', '<', '>'), array('&amp;', '&quot;', '&lt;', '&gt;'), $string));
}
return $string;

方法二

 代码如下 复制代码
// $rptype = 0 表示仅替换 html标记
// $rptype = 1 表示替换 html标记同时去除连续空白字符
// $rptype = 2 表示替换 html标记同时去除所有空白字符
// $rptype = -1 表示仅替换 html危险的标记
function htmlreplace($str,$rptype=0)
{
$str = strips教程lashes($str);
if($rptype==0)
{
$str = htmlspecialchars($str);
}
else if($rptype==1)
{
$str = htmlspecialchars($str);
$str = str_replace(" ",' ',$str);
$str = ereg_replace("[rnt ]{1,}",' ',$str);
}
else if($rptype==2)
{
$str = htmlspecialchars($str);
$str = str_replace(" ",'',$str);
$str = ereg_replace("[rnt ]",'',$str);
}
else
{
$str = ereg_replace("[rnt ]{1,}",' ',$str);
$str = eregi_replace('script','script',$str);
$str = eregi_replace("<[/]{0,1}(link|meta|ifr|fra)[^>]*>",'',$str);
}
return addslashes($str);
}

其它方法

php教程过滤不安全字符函数

 代码如下 复制代码

function uh($str)
{
    $farr = array(
        "/s+/",//过滤多余的空白
        "/<(/?)(script|i?frame|style|html|body|title|link|meta|?|%)([^>]*?)>/isu",//过滤 <script 等可能引入恶意内容或恶意改变显示布局的代码,如果不需要插入flash等,还可以加入<object的过滤
        "/(<[^>]*)on[a-za-z]+s*=([^>]*>)/isu",//过滤网页特效的on事件
    );
   $tarr = array(
        " ",
        "<\1\2\3>", //如果要直接清除不安全的标签,这里可以留空
        "\1\2",
   );

  $str = preg_replace($farr,$tarr,$str);
  return $str;

<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=gb2312" />
<title>几种防御php程序被木马攻击配置详解方法</title>
</head>

<body>
防止跳出web目录
  首先修改httpd.conf,如果你只允许你的php脚本程序在web目录里操作,还可以修改httpd.conf文件限制php的操作路径。比如你的web目录是/usr/local/apache/htdocs,那么在httpd.conf里加上这么几行:

  php_admin_value open_basedir /usr/local/apache

  /htdocs

  这样,如果脚本要读取/usr/local/apache/htdocs以外的文件将不会被允许,如果错误显示打开的话会提示这样的错误:

  warning: open_basedir restriction in effect. file is in wrong directory in

  /usr/local/apache/htdocs/open.php on line 4

  等等。

2、防止php木马执行webshell
  打开safe_mode,

  在,php.ini中设置

  disable_functions= passthru,exec,shell_exec,system

  二者选一即可,也可都选

3、防止php木马读写文件目录
  在php.ini中的

  disable_functions= passthru,exec,shell_exec,system

  后面加上php处理文件的函数

  主要有

  fopen,mkdir,rmdir,chmod,unlink,dir

  fopen,fread,fclose,fwrite,file_exists

  closedir,is_dir,readdir.opendir

  fileperms.copy,unlink,delfile

  即成为

  disable_functions= passthru,exec,shell_exec,system,fopen,mkdir,rmdir,chmod,unlink,dir

  ,fopen,fread,fclose,fwrite,file_exists

  ,closedir,is_dir,readdir.opendir

  ,fileperms.copy,unlink,delfile

  ok,大功告成,php木马拿我们没辙了,遗憾的是这样的话,利用文本数据库教程的那些东西就都不能用了。

  如果是在windos平台下搭建的apache我们还需要注意一点,apache默认运行是system权限,这很恐怖,这让人感觉很不爽.那我们就给apache降降权限吧。

  net user apache fuckmicrosoft /add

  net localgroup users apache /del

  ok.我们建立了一个不属于任何组的用户apche。

  我们打开计算机管理器,选服务,点apache服务的属性,我们选择log on,选择this account,我们填入上面所建立的账户和密码,重启apache服务,ok,apache运行在低权限下了。

  实际上我们还可以通过设置各个文件夹的权限,来让apache用户只能执行我们想让它能干的事情,给每一个目录建立一个单独能读写的用户。这也是当前很多虚拟主机提供商的流行配置方法哦,不过这种方法用于防止这里就显的有点大材小用了
</body>
</html>

 代码如下 复制代码

<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.111cn.net/tr/xhtml1/dtd/xhtml1-transitional.dtd">
<html lang="en-us" xml:lang="en-us" xmlns="http://www.w3.org/1999/xhtml">
<title>login</title>
<link rel="stylesheet" type="text/css教程" href="/tryit.css教程" />
<script type="text/网页特效">
function getpinimg()
{
 document.getelementbyid("pinimg").src="log_demo.php教程?a=pin&r=" + math.random();
}
</script>
</head>

<body>
<form action="log_demo.php" method="get">
 <input type="hidden" name="a" value="log" />
 user:<input type="text" name="user" size="20" value="test" /><br /><br />
 password:<input type="password" name="pass" size="12" value="123456" /><br /><br />
 pincode:<input type="text" name="pin" size="8" value="" />
 <input type="button" value="getpin" onclick="getpinimg();">
   <img id="pinimg" src="log_demo.php?a=pin" onclick="getpinimg();" />
 <input type="submit" value="submit"><br />
</form>
</body>
</html>

log_demo.php代码

 代码如下 复制代码

<?php
require_once("log_aux.php");

if ($_get['a'] == "pin") {
 getpinimg();
 exit;
}

if ($_get['a'] == "log") {
 $ret = checkpincookie($_get['pin'], 300);
 if ($ret < 0) {
  echo "pin error:$ret ";
  exit;
 }
 // check user name and password.
 echo "ok..$ret ";
 if ("123xx" == 123) {
  echo "dddd ";
 }
 echo $_get['u'];
}

log_aux.php代码

 代码如下 复制代码

<?php
$magic_a = "www.111cn.net34589";
$magic_b = "234566***+";

function getpincookie($pin_code)
{
 global $magic_a;
 global $magic_b;
 $now = time();
 $client_ip = $_server['remote_addr'];
 $pin = md5($now . $client_ip . $magic_a . $pin_code . $maigc_b) . $now;
 return $pin;
}

function checkpincookie($pin_code_in, $timeout=300) // 5 miniuteswww.111cn.net
{
 global $magic_a;
 global $magic_b;
 $pin = substr($_cookie['pin'], 0, 32);
 $time = substr($_cookie['pin'], 32);
 $now = time();
 if ($now - $time > $timeout) {
  return -100;  // time_out
 }
 $pin_code_in = strtolower($pin_code_in);
 $client_ip = $_server['remote_addr'];
 $pin_2 = md5($time . $client_ip . $magic_a . $pin_code_in . $maigc_b);
 if ($pin === $pin_2) {
  return 0;
 } else {
  return -200; // pincode is error.
 }
}

function getpinimg()
{
// $rnd = rand(0, 10);
 $rnd = 0;
 $path = "./www.111cn.net/" . substr("00000$rnd", -4);
 for ($i = 0; $i < 4; ++$i) {
  if (__getpinimg($path)) {
   break;
  }
 }
 exit;
}

function __getpinimg($path)
{
 require("$path/pinmap.php");
 $pinv = $pin[rand(0, $pin_max)];
 list ($file, $pin_code) = explode('#', $pinv);
 $filepath = "$path/$file$pin_ext";

 //  output pincode image.
 $fh = fopen($filepath, "rb");
 if (!$fh) {
  return false;
 }
 $data = fread($fh, 10240); // php just read max size: 8k.
 fclose($fh);

 if (strlen($data) < 200) {
  return false;
 }
 // set cookie;
 header("content-type: image/jpeg");
 $pin_code = strtolower($pin_code);
 $pin = getpincookie($pin_code);
 setcookie("pin", $pin);

 echo $data;
 return true;
}

[!--infotagslink--]

相关文章

  • phpems SQL注入(cookies)分析研究

    PHPEMS(PHP Exam Management System)在线模拟考试系统基于PHP+Mysql开发,主要用于搭建模拟考试平台,支持多种题型和展现方式,是国内首款支持题冒题和自动评分与教师评分相...2016-11-25
  • ASP/PHP sql注入语句整理大全

    SQL注入攻击指的是通过构建特殊的输入作为参数传入Web应用程序,而这些输入大都是SQL语法里的一些组合,通过执行SQL语句进而执行攻击者所要的操作 标准注入语句1.判...2016-11-25
  • php正确禁用eval函数与误区介绍

    eval函数在php中是一个函数并不是系统组件函数,我们在php.ini中的disable_functions是无法禁止它的,因这他不是一个php_function哦。 eval()针对php安全来说具有很...2016-11-25
  • php中eval()函数操作数组的方法

    在php中eval是一个函数并且不能直接禁用了,但eval函数又相当的危险了经常会出现一些问题了,今天我们就一起来看看eval函数对数组的操作 例子, <?php $data="array...2016-11-25
  • C#中截取字符串的的基本方法详解

    这篇文章主要介绍了C#中截取字符串的的基本方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-11-03
  • Python astype(np.float)函数使用方法解析

    这篇文章主要介绍了Python astype(np.float)函数使用方法解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下...2020-06-08
  • PHP防止SQL注入的例子

    防止SQL注入是我们程序开发人员必须要做的事情了,今天我们就来看一篇关于PHP防止SQL注入的例子了,具体的实现防过滤语句可以参考下面来看看吧。 使用prepared以及参...2016-11-25
  • c#中判断字符串是不是数字或字母的方法

    这篇文章介绍了C#判断字符串是否数字或字母的实例,有需要的朋友可以参考一下...2020-06-25
  • PostgreSQL判断字符串是否包含目标字符串的多种方法

    这篇文章主要介绍了PostgreSQL判断字符串是否包含目标字符串的多种方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下...2021-02-23
  • Python中的imread()函数用法说明

    这篇文章主要介绍了Python中的imread()函数用法说明,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-03-16
  • 详解C++ string常用截取字符串方法

    这篇文章主要介绍了C++ string常用截取字符串方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-04-25
  • C# 中如何取绝对值函数

    本文主要介绍了C# 中取绝对值的函数。具有很好的参考价值。下面跟着小编一起来看下吧...2020-06-25
  • C#学习笔记- 随机函数Random()的用法详解

    下面小编就为大家带来一篇C#学习笔记- 随机函数Random()的用法详解。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧...2020-06-25
  • php字符串按照单词逐个进行反转的方法

    本文实例讲述了php字符串按照单词进行反转的方法。分享给大家供大家参考。具体分析如下:下面的php代码可以将字符串按照单词进行反转输出,实际上是现将字符串按照空格分隔到数组,然后对数组进行反转输出。...2015-03-15
  • 金额阿拉伯数字转换为中文的自定义函数

    CREATE FUNCTION ChangeBigSmall (@ChangeMoney money) RETURNS VarChar(100) AS BEGIN Declare @String1 char(20) Declare @String2 char...2016-11-25
  • C++中 Sort函数详细解析

    这篇文章主要介绍了C++中Sort函数详细解析,sort函数是algorithm库下的一个函数,sort函数是不稳定的,即大小相同的元素在排序后相对顺序可能发生改变...2022-08-18
  • Android开发中findViewById()函数用法与简化

    findViewById方法在android开发中是获取页面控件的值了,有没有发现我们一个页面控件多了会反复研究写findViewById呢,下面我们一起来看它的简化方法。 Android中Fin...2016-09-20
  • MySQL 字符串拆分操作(含分隔符的字符串截取)

    这篇文章主要介绍了MySQL 字符串拆分操作(含分隔符的字符串截取),具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-02-22
  • 使用list stream: 任意对象List拼接字符串

    这篇文章主要介绍了使用list stream:任意对象List拼接字符串操作,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教...2021-09-09
  • PHP用strstr()函数阻止垃圾评论(通过判断a标记)

    strstr() 函数搜索一个字符串在另一个字符串中的第一次出现。该函数返回字符串的其余部分(从匹配点)。如果未找到所搜索的字符串,则返回 false。语法:strstr(string,search)参数string,必需。规定被搜索的字符串。 参数sea...2013-10-04