您的位置:首页 > 编程语言 > PHP开发

php:自己设计的一个php验证码

2012-06-21 21:09 246 查看
<?php
$length=5;             //即5个字符
$length_x=$length;
while($length-->0){
$temp=rand(1,3);
switch($temp){
case 1:
$chars[]=sprintf("%c",rand(65,90));
break;
case 2:
$chars[]=sprintf("%c",rand(97,122));
break;
case 3:
$chars[]=sprintf("%c",rand(48,57));
break;
}
}
$str='';
foreach($chars as $v){
$str.=$v;
}
setcookie('checkcode',$str);
$img = imagecreatetruecolor(count($chars)*9+18+($length_x-1)*10, 40);//9
$gray = imagecolorallocate($img, 0xC0,0xC0,0xC0);
$dark_gray = imagecolorallocate($img, 0xAA,0xAA,0xAA);
$bgcolor = imagecolorallocate($img, rand(0,255),rand(0,255),rand(0,255));
imagefill($img, 0, 0, $bgcolor);
$x=9;
for($i=0;$i<count($chars);$i++){
//
$fontColor = imagecolorallocate($img, rand(0,255),rand(0,255),rand(0,255));
imagechar($img, 5, $x, rand(0,25), $chars[$i], $fontColor);
$x+=9+10;
}
//imageString($img, 5, 2, 3, 'hello world', $gray);
header("Content-type: image/png");
imagepng($img);
//6-20.php
?>


<html>
<head>
<title>1</title>
<style type='text/css'>

</style>
</head>
<body>
<form action='' method='get'>
<input type='text' name='checkcode'/> 
<img src='6-20.php' onclick="this.src='6-20.php?tmp='+Math.random();" style='cursor:pointer;'/> 
<input type='submit' value='提交'/>
</form>
<?php
if($_GET){
if($_COOKIE['checkcode']==$_GET['checkcode'])
echo "  <font color='green'>验证码正确!{$_GET[checkcode]}</font>";
else
echo "  <font color='red'>验证码错误!</font>";
}
//6-21.php
?>
</body>
</html>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐