代码如下 | 复制代码 |
<?php header("Cache-Control: no-cache, must-revalidate"); session_start(); //绘制杂点 //绘制文字 imagepng($image); |
下面代码保存成MD5Crypt.class.php文件
代码如下 | 复制代码 |
<?php class MD5Crypt { /** * Enter description here ... * @param unknown_type $str * @return string */ public final static function mdsha($str) { $code = substr ( md5 ( $str ), 10 ); $code .= substr ( sha1 ( $str ), 0, 28 ); $code .= substr ( md5 ( $str ), 0, 22 ); $code .= substr ( sha1 ( $str ), 16 ) . md5 ( $str ); return self::chkToken () ? $code : null; } /** * Enter description here ... * @param unknown_type $param */ private final static function chkToken() { return true; } /** * Enter description here ... * @param unknown_type $txt * @param unknown_type $encrypt_key * @return Ambigous <string, boolean> */ private final static function keyED($txt, $encrypt_key) { $encrypt_key = md5 ( $encrypt_key ); $ctr = 0; $tmp = ""; for($i = 0; $i < strlen ( $txt ); $i ++) { if ($ctr == strlen ( $encrypt_key )) $ctr = 0; $tmp .= substr ( $txt, $i, 1 ) ^ substr ( $encrypt_key, $ctr, 1 ); $ctr ++; } return $tmp; } /** * Enter description here ... * @param unknown_type $txt * @param unknown_type $key * @return string */ public final static function Encrypt($txt, $key) { srand ( ( double ) microtime () * 1000000 ); $encrypt_key = md5 ( rand ( 0, 32000 ) ); $ctr = 0; $tmp = ""; for($i = 0; $i < strlen ( $txt ); $i ++) { if ($ctr == strlen ( $encrypt_key )) $ctr = 0; $tmp .= substr ( $encrypt_key, $ctr, 1 ) . (substr ( $txt, $i, 1 ) ^ substr ( $encrypt_key, $ctr, 1 )); $ctr ++; } $_code = md5 ( $encrypt_key ) . base64_encode ( self::keyED ( $tmp, $key ) ) . md5 ( $encrypt_key . $key ); return self::chkToken () ? $_code : null; } /** * Enter description here ... * @param unknown_type $txt * @param unknown_type $key * @return Ambigous <string, boolean> */ public final static function Decrypt($txt, $key) { $txt = self::keyED ( base64_decode ( substr ( $txt, 32, - 32 ) ), $key ); $tmp = ""; for($i = 0; $i < strlen ( $txt ); $i ++) { $md5 = substr ( $txt, $i, 1 ); $i ++; $tmp .= (substr ( $txt, $i, 1 ) ^ $md5); } return self::chkToken () ? $tmp : null; } /** * Enter description here ... * @var unknown_type */ private static $_key = 'lau'; } ?> |
用法
代码如下 | 复制代码 |
<?php //Code Start define ( 'WORKSPACE', '.' . DIRECTORY_SEPARATOR ); header ( "Content-Type: text/html; charset=utf-8" ); include_once 'Core/Library/MD5Crypt.class.php'; $a = MD5Crypt::Encrypt ( "A", 100 ); echo "EnCode:" . $a, "<br />"; echo "DeCode:" . MD5Crypt::Decrypt ( $a, 100 ); ?> |
/*
* Auth_code()是验证码函数
* @access public
* @param int $width 表示验证码的长度,默认为80
* @param int $height 表示验证码的高度,默认为20
* @param int $num 表示验证码中数字位数,默认为4
* @param int $line 表示验证码中线的条数,默认为4
* @param int $line 表示验证码中点的个数,默认为150
*/
代码如下 | 复制代码 |
function Auth_code($width = 80,$height = 20,$num = 4,$line = 4,$dot = 150) { $length = floor($width/$num); //floor:取整数部分 length:每段平均长度 |
-主要函数:
imagecreatetruecolor //新建一个黑色底的画板
imagecolorallocate //取色
imagefill //填充画板
imageline //画线
imagesetpixel //画点
imagestring //画字符串(本验证码为数字)
注意:在将数字验证码画上画板时要注意字体越界,所以要相应减去一些范围,详见附件。
-局部刷新验证码:
代码如下 | 复制代码 |
<img" width=100% src="code.php" onclick="javascript:this.src='code.php?id='+Math.random()"> |
代码如下 | 复制代码 |
代码如下: //随机生成一个4位数的数字验证码 //随机绘制两条虚线,起干扰作用 //在画布上随机生成大量黑点,起干扰作用; |
使用方法:
本程序可以直接运行,运行之后即可看到一个图形验证码,每次刷新都随机生成新码。
在某页面中使用此程序时,可以用以下代码:
代码如下:
.....
请输入验证码:
代码如下 | 复制代码 |
<input type=text name=passcode> <img" width=100% src=showimg.php> ..... |
这样即可显示出验证码图片。到了验证页面,用以下代码:
代码如下:
...
代码如下 | 复制代码 |
$code=$_POST["passcode"]; if( $code == $_SESSION["Checknum"]){ 验证通过 }else{ 验证码错误 } ... |
你可以这样使用
代码如下 | 复制代码 |
http://www.xxx.com/index.php?page=../etc/passwd 获取更多数据: |
像上面代码,如果你是
?page=$_GET的话这样就完了,分析原因,因为我们分页只有数字,那么我们这样操作
代码如下 | 复制代码 |
?page=intval($_GET); |
这样就无法把字符提交了,我们利用了intval函数进行了过滤,那么对于提交字符怎么处理呢。
在处理字符时我们利用php自带函数的函数 addslashes和htmlspecialchars进行过滤,
如
代码如下 | 复制代码 |
$body = htmlspecialchars(isset($_GET[$str])?$_GET[$str]:''); |
这样就基本过滤了各种安全注入,当然如果你服务器有漏洞在php上是解决不了的。