php是有GD生成验证码
2015-05-23 09:53
162 查看
html代码
<?xml version="1.0" encoding="utf-8"?>
<!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" lang="en_US" xml:lang="en_US">
<!--
* Created on 2015-5-22
*
* To change the template for this generated file go to
* Window - Preferences - PHPeclipse - PHP - Code Templates
-->
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
<title> </title>
</head>
<body>
<form action="check.php" name="form1" method="post">
<table>
<tr>
<td>验证码:<input type="text" name="chkcode" value="" size="15" maxlength="4"></td>
<td><iframe src="crecode.php" height="50px" width="80px" frameborder="0" id="chkimg"></iframe>
<input type="button" value="看不清,换一张" onclick="chkimg.location.reload();">
</td>
</tr>
</table>
<input type="hidden" value="4" name="checkcnt"/>
<input type="submit" name="submit1" value="提交"/>
</form>
</body>
</html>
########################################################################################
生成验证码图像的代码(crecode.php)
#######################################################################################
<?php
/*
* Created on 2015-5-22
*
* To change the template for this generated file go to
* Window - Preferences - PHPeclipse - PHP - Code Templates
*/
header("Content-type:image/png");
session_start();
$authnum="";
$str="abcdefghijkmnopqrstuvwxyz1234567890";
$strLength=strlen($str);
for($i=1;$i<=4;$i++){
$num=rand(0,$strLength-1);
$authnum .=$str[$num];
}
$_SESSION["authnum"]=$authnum;
srand((double)microtime()*1000000);
$im=imagecreate(50,20);
$gray=imagecolorallocate($im,200,200,100);
$white=imagecolorallocate($im,255,255,255);
imagefill($im,10,5,$gray);
$li=imagecolorallocate($im,150,150,150);
for($i=0;$i<3;$i++){
imageline($im,rand(0,20),rand(0,50),rand(20,40),rand(0,50),$li);
}
for($i=0;$i<strlen($_SESSION['authnum']);$i++){
$strColor=imagecolorallocate($im,mt_rand(0,100),mt_rand(50,150),mt_rand(100,200));
$fontSize=mt_rand(3,5);
$x=mt_rand(1,5)+50*$i/4;
$y=mt_rand(1,5);
imagestring($im,$fontSize,$x,$y,$_SESSION['authnum'][$i],$strColor);
}
for($i=0;$i<90;$i++){
imagesetpixel($im,rand()%70,rand()%30,$gray);
}
imagepng($im);
imagedestroy($im);
?>
##########################################################
验证验证码
#########################################################
<?php
/*
* Created on 2015-5-22
*
* To change the template for this generated file go to
* Window - Preferences - PHPeclipse - PHP - Code Templates
*/
header("Content-type:text/html;charset=utf-8");
session_start();
$chkCode=strtoupper($_POST['chkcode']);
$auth=strtoupper($_SESSION['authnum']);
//echo $chkCode;
//echo "<br>";
// echo $_SESSION['authnum'];
if($chkCode==$auth){
echo "验证成功!";
}else{
echo "验证失败!";
}
session_destroy();
?>
<?xml version="1.0" encoding="utf-8"?>
<!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" lang="en_US" xml:lang="en_US">
<!--
* Created on 2015-5-22
*
* To change the template for this generated file go to
* Window - Preferences - PHPeclipse - PHP - Code Templates
-->
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
<title> </title>
</head>
<body>
<form action="check.php" name="form1" method="post">
<table>
<tr>
<td>验证码:<input type="text" name="chkcode" value="" size="15" maxlength="4"></td>
<td><iframe src="crecode.php" height="50px" width="80px" frameborder="0" id="chkimg"></iframe>
<input type="button" value="看不清,换一张" onclick="chkimg.location.reload();">
</td>
</tr>
</table>
<input type="hidden" value="4" name="checkcnt"/>
<input type="submit" name="submit1" value="提交"/>
</form>
</body>
</html>
########################################################################################
生成验证码图像的代码(crecode.php)
#######################################################################################
<?php
/*
* Created on 2015-5-22
*
* To change the template for this generated file go to
* Window - Preferences - PHPeclipse - PHP - Code Templates
*/
header("Content-type:image/png");
session_start();
$authnum="";
$str="abcdefghijkmnopqrstuvwxyz1234567890";
$strLength=strlen($str);
for($i=1;$i<=4;$i++){
$num=rand(0,$strLength-1);
$authnum .=$str[$num];
}
$_SESSION["authnum"]=$authnum;
srand((double)microtime()*1000000);
$im=imagecreate(50,20);
$gray=imagecolorallocate($im,200,200,100);
$white=imagecolorallocate($im,255,255,255);
imagefill($im,10,5,$gray);
$li=imagecolorallocate($im,150,150,150);
for($i=0;$i<3;$i++){
imageline($im,rand(0,20),rand(0,50),rand(20,40),rand(0,50),$li);
}
for($i=0;$i<strlen($_SESSION['authnum']);$i++){
$strColor=imagecolorallocate($im,mt_rand(0,100),mt_rand(50,150),mt_rand(100,200));
$fontSize=mt_rand(3,5);
$x=mt_rand(1,5)+50*$i/4;
$y=mt_rand(1,5);
imagestring($im,$fontSize,$x,$y,$_SESSION['authnum'][$i],$strColor);
}
for($i=0;$i<90;$i++){
imagesetpixel($im,rand()%70,rand()%30,$gray);
}
imagepng($im);
imagedestroy($im);
?>
##########################################################
验证验证码
#########################################################
<?php
/*
* Created on 2015-5-22
*
* To change the template for this generated file go to
* Window - Preferences - PHPeclipse - PHP - Code Templates
*/
header("Content-type:text/html;charset=utf-8");
session_start();
$chkCode=strtoupper($_POST['chkcode']);
$auth=strtoupper($_SESSION['authnum']);
//echo $chkCode;
//echo "<br>";
// echo $_SESSION['authnum'];
if($chkCode==$auth){
echo "验证成功!";
}else{
echo "验证失败!";
}
session_destroy();
?>
相关文章推荐
- php基础之gd图像生成、缩放、logo水印和简单验证码实现
- PHP学习笔记09——GD生成验证码实例
- PHP调用GD函数生成验证码
- PHP GD 生成图片验证码+session获取储存验证码
- php运存过小,导致gd生成验证码时php解析出错
- PHP GD 生成图片验证码+session获取储存验证码
- php gd 无法生成缩略图
- PHP生成雪花背景的验证码
- php生成短信验证码,php生成登陆验证码指定字符串
- 基于PHP生成简单的验证码
- php验证码 GD库生成验证码
- 生成随机字符串和验证码的类的PHP实例
- PHP生成带有雪花背景的网站验证码
- PHP验证码的生成
- 一个简单的PHP生成验证码
- php生成验证码
- PHP生成图形验证码
- PHP生成验证码
- PHP生成各种验证码和Ajax验证
- PHP生成各种常见验证码和Ajax验证过程