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

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();

?>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: