- 论坛徽章:
- 0
|
我做的登录验证码,大家觉得好的就顶一下!
如果还有更好或优化的请指点!
<?
//img.php
session_start();
//生成四位随机整数验证码
mt_srand((double)microtime()*1000000);
$AuthNum = mt_rand();
if($AuthNum<1000){$AuthNum *= 1000;}
$AuthNum = substr(strrev($AuthNum),-4);
session_register('AuthNum');
$_SESSION['AuthNum'] = md5($AuthNum);
Header("Content-type:image/png" ;
$im = ImageCreate(40,1 ;
$black = ImageColorAllocate($im,0,0,0);
$white = ImageColorAllocate($im,0,255,0);
$white2 = ImageColorAllocate($im,255,255,255);
//将四位整数验证码绘入图片
//位置交错
for ($i = 0; $i < 4; $i++) {
if ($i%2 == 0){$top = 1;}else{$top = 3;}
imagestring($im, 5, 10*$i+1, $top, substr($AuthNum,$i,1), $white);
}
//加入干扰象素线
//ImageLine($im,0,11,40,6,$white2);
//加入干扰象素点
for($i=0;$i<150;$i++){ imagesetpixel($im, rand()%70 , rand()%30 , $white2); }
ImagePng($im);
ImageDestroy($im);
/*
登陆表单用
<FORM METHOD=POST ACTION="ii.php">
<input type=text name=Number maxlength=4>
<a href="login.htm" title="点击更新验证数字"><img src="img.php" border="0" align="absmiddle" ></a>
<INPUT TYPE="submit" name="sub">
</FORM>
验证时用
if(empty($_POST["Number"]) || md5($_POST['Number']) <> $_SESSION['AuthNum']){ echo '<script> alert("验证码有误!" ; location.replace ("index.php" </script>'; exit(); }
session_unregister('AuthNum');
unset($_SESSION['AuthNum']);
*/
?> |
|