checkcode.php 核心页面
<?php
session_start();
$checkcode="";
for($i=1;$i<=4;$i++){
$checkcode.=dechex(rand(1,15));
}
$_SESSION['checkcode']=$checkcode;
//创建画布
$image=imagecreatetruecolor(80,30);
//设置一个颜色
$color=imagecolorallocate($image,255,255,255);
//写入画布
imagestring($image,rand(2,5),rand(0,50),rand(0,20),$checkcode,$color);
//干扰线
for($i=0;$i<10;$i++){
imageline($image,rand(0,80),rand(0,30),rand(0,80),rand(0,30),imagecolorallocate($image,rand(0,255),rand(0,255),rand(0,255)));
}
//输出
header( "Content-type: image/png" );
imagepng ( $image );
?>
login.php 视图页面
<form action="loginProcess.php" method="post">
验证码:<input type="text" name="checkcode"> <img src="checkcode.php" οnclick="this.src='checkcode.php?a=Math.random()'">
</form>
loginProcess.php 逻辑页面
<?php
session_start();
if($_POST['checkcode']==$_SESSION['checkcode']){
echo "验证码正确";
}else{
echo "验证码错误";
}
?>