php验证码

来源:互联网 发布:怎么找淘宝刷单的兼职 编辑:程序博客网 时间:2024/06/08 10:00

validcode.php:

<?phpheader("Content-Type:image/png");//开启sessionsession_start();//随机4个数字$code = "";$arr = "23456789abcdefghijkmnpqrstuvwxyzABCDEFGHIJKLMNPQRSTUVW";for ($i = 0; $i < 4; $i++) {    $code .= $arr[rand(0, strlen($arr)-1)];}//设置入session中,方便比对$_SESSION["validcode"] = $code;//开始绘图$width = 100;$height = 25;$img = imagecreatetruecolor($width,$height);//填充背景色$backcolor = imagecolorallocate($img,0,0,0);imagefill($img,0,0,$backcolor);//获取随机较深颜色for($i=0;$i<10;$i++){    $textcolor = imagecolorallocate($img,rand(50,180),rand(50,180),rand(50,180));    imagechar($img,5,7+$i*25,3,$code[$i],$textcolor);}for($i=0;$i<100;$i++) //加入干扰象素{    $randcolor = ImageColorallocate($img,rand(0,255),rand(0,255),rand(0,255));    imagesetpixel($img, rand(1,100) , rand(2,18) , $randcolor);}//显示图片imagepng($img);//销毁图片imagedestroy($img);?>
validcode.php:

<html><head>    <meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>    <title>测试页面</title>    <style type="text/css">        a{            font-size:12px;            text-decoration:none;            color:red;        }        a:hover{            color:orange;        }    </style></head><body><img src="validcode.php" style="width:100px;height:25px;" id="code"/><a href="javascript:changeCode()">看不清,换一张</a></body></html><script type="text/javascript">    function changeCode(){        document.getElementById("code").src = "validcode.php?id="+Math.random();    }</script>


0 0
原创粉丝点击