text.php
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>登录</title>
</head>
<body>
<form method="post" action="">
<table border="1px" width="300px" align="center">
<tr><td colspan="2"><img src="captha.php"></td></tr>
<tr><td>验证:</td><td><input type="text" name="verification" value=""></td></tr>
<tr><td colspan="2" align="center">
<input type="submit" name="enroll" value="登录">
<input type="reset" name="ret" value="重置">
</td>
</tr>
</table>
</form>
<?php
session_start();
include 'loc.php';
if(isset($_POST['enroll'])){
if($_SESSION['captcha']==$_POST['verification']){
echo "<script>
alert('登录成功');
window.location.href ='test.php';
</script>";
}else{
echo "<script>
alert('验证码错误,登录失败');
</script>";
}
}
?>
</body>
</html>
captha.php
<?php
//包含生成给定长度字符串得自定义函数functions.php
include 'functions.php';
//开启或继续会话,保存图形验证码在会话中,供其它页面调用
if(!isset($_SESSION)){
session_start();
}
//创建65*20px大小的图像
$width=65;
$height = 20;
$image = imagecreate($width,$height);
//为一幅图像分配颜色;imagecolorallocate
//imagefilledrectangle:围一矩形并填充
// 为空白图像填充颜色
$bg_color = imagecolorallocate($image,0x33,0x66,0xff);
imagefilledrectangle($image,0,0,$width,$height,$bg_color);
//取得随机字符串
$text = random_texts(5);
//定义字体,位置
$font = 5;
$x = imagesx($image)/2-strlen($text)*imagefontwidth($font)/2;
$y = imagesy($image)/2-imagefontheight($font)/2;
//输出字符到图形上
$fg_color = imagecolorallocate($image,0xff,0xff,0xff);
imagestring($image,$font,$x,$y,$text,$fg_color);
//保存验证码到会话,用于比较验证
$_SESSION['captcha'] = $text;
//输出图像
header('Content-type:image/png');//定义header,声明图片文件
imagepng($image);
imagedestroy($image);
$a = "php china php mrkj";
echo strrchr($a,'1');
?>
funtions.php
<?php
//返回给定长度得随机字符串
function random_texts($count){
//创建字符数组
$chars = array_flip(array_merge(range(1,9),range('A','z')));
//删除有些容易引起混淆得相似得单号
//生成随机字符文本
for($i=0,$text='';$i<$count;$i++){
$text.=array_rand($chars);
}
return $text;
}
?>