verify.func.php
<?php
/**
* @param string $fontFile
* @param int $type
* @param int $length
* @param string $codeName 存入session的名字
* @param int $pixel
* @param int $line
* @param int $arc
* @param int $width
* @param int $height
*/
function getVerify($fontFile = 'fonts/simsunb.ttf', $type = 1, $length = 4, $codeName = 'verifyCode', $pixel = 50, $line = 3, $arc = 2, $width = 200, $height = 50)
{
header("content-type:text/html;charset=utf-8");
$image = imagecreatetruecolor($width, $height);
$white = imagecolorallocate($image, 255, 255, 255);
imagefilledrectangle($image, 0, 0, $width, $height, $white);
function getRandColor($image)
{
return imagecolorallocate($image, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));
}
/**
* 1-数字
* 2-字母
* 3-数字+字母
* 4-汉子
*/
switch ($type) {
case 1:
//数字
$string = join('', array_rand(range(0, 9), $length));
break;
case 2:
//字母
$string = join('', array_rand(array_flip(array_merge(range('a', 'z'), range('A', 'Z'))), $length));
break;
case 3:
//数字+字母
$string = join('', array_rand(array_flip(array_merge(range(0, 9), range('a', 'z'), range('A', 'Z'))), $length));
break;
case 4:
//汉子
$str = "需,要,传,一,个,字,体,文,件,到,函,数,参,数,里";
$arr = explode(',', $str);
$string = join('', array_rand(array_flip($arr), $length));
break;
default:
exit('非法参数');
break;
}
//将验证码存入session
session_start();
$_SESSION[$codeName] = $string;
for ($i = 0; $i < $length; $i++) {
$size = mt_rand(20, 28);
$angle = mt_rand(-15, 15);
$x = 20 + ceil($width / $length) * $i;
$y = mt_rand(ceil($height / 3), $height - 20);
$color = getRandColor($image);
// $fontFile = 'fonts/simsunb.ttf';
//mb_substr 可识别中文
$text = mb_substr($string, $i, 1, 'utf-8');
imagettftext($image, $size, $angle, $x, $y, $color, $fontFile, $text);
}
if ($pixel > 0) {
for ($i = 1; $i <= $pixel; $i++) {
imagesetpixel($image, mt_rand(0, $width), mt_rand(0, $height), getRandColor($image));
}
}
if ($line > 0) {
for ($i = 1; $i <= $line; $i++) {
imageline($image, mt_rand(0, $width), mt_rand(0, $height), mt_rand(0, $width), mt_rand(0, $height), getRandColor($image));
}
}
if ($arc > 0) {
for ($i = 1; $i <= $arc; $i++) {
imagearc($image, mt_rand(0, $width / 2), mt_rand(0, $height / 2), mt_rand(0, $width), mt_rand(0, $height), mt_rand(0, 360), mt_rand(0, 360), getRandColor($image));
}
}
header('content-type:image/png');
imagepng($image);
imagedestroy($image);
}
getVerify('fonts/simsunb.ttf', 3);
?>
reg.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>注册页面</title>
</head>
<body>
<h1>IMOOC注册页面</h1>
<form action="doAction.php" method="post">
<table bgcolor="#abcdef" width="80%" border="1" cellspacing="0" cellpadding="0">
<tr>
<td>用户名</td>
<td><input type="text" name="username" id="" placeholder="请输入用户名..."></td>
</tr>
<tr>
<td>密码</td>
<td><input type="password" name="password" id="" placeholder="请输入密码..."></td>
</tr>
<tr>
<td>验证码</td>
<td><input type="text" name="vertify" id="" placeholder="请输入验证码">
![](verify.func.php)<a
onclick="document.getElementById('verifyImage').src='getVerify.php?r='+Math.random()"
href="javascript:void(0)">看不清,换一个</a>
</td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="注册"></td>
</tr>
</table>
</form>
</body>
</html>
doAction.php
<?php
header("content-type:text/html;charset=utf-8");
session_start();
//print_r($_POST);
var_dump($_POST);
echo "<hr/>";
var_dump($_SESSION);
?>