<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>验证码使用</title>
</head>
<body>
<h1>用户登录</h1>
<form action="index.php" method="post">
<input type="text" name="mycheck">
<img src="session5.php" οnclick="this.src='session5.php?aa='+Math.random()"/>
<button type="submit">提交</button>
</form>
<?php
if(isset($_POST['mycheck']))
{
$mycheck=$_POST['mycheck'];
session_start();
echo $mycheck;
echo $_SESSION['mycheck'];
if($mycheck==$_SESSION['mycheck'])
{
echo "验证成功";
}
else{
echo "验证失败";
}
}
?>
</body>
</html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>验证码使用</title>
</head>
<body>
<h1>用户登录</h1>
<form action="index.php" method="post">
<input type="text" name="mycheck">
<img src="session5.php" οnclick="this.src='session5.php?aa='+Math.random()"/>
<button type="submit">提交</button>
</form>
<?php
if(isset($_POST['mycheck']))
{
$mycheck=$_POST['mycheck'];
session_start();
echo $mycheck;
echo $_SESSION['mycheck'];
if($mycheck==$_SESSION['mycheck'])
{
echo "验证成功";
}
else{
echo "验证失败";
}
}
?>
</body>
</html>
//session.php文件
<?php
session_start();
$check="";
for ($i=0; $i < 4; $i++) {
$check.=dechex(rand(1,15));
}
//将随机验证码保存在session中
$_SESSION['mycheck']=$check;
//创建图片
$img=imagecreatetruecolor(110, 30);
//背景默认为黑色
$bgcolor=imagecolorallocate($img, 0, 0, 0);
imagefill($img, 0, 0, $bgcolor);
//创建新的颜色
$white=imagecolorallocate($img, 255, 255, 255);
$blue=imagecolorallocate($img, 0, 0, 255);
$red=imagecolorallocate($img, 255, 0, 0);
$green=imagecolorallocate($img, 255, 0, 0);
//画出干扰线段
for ($i=0; $i <10 ; $i++) {
imageline($img,rand(0,110),rand(0,30),rand(0,110), rand(0,30),
imagecolorallocate($img, rand(0,255), rand(0,255), rand(0,255)));
}
//画噪点
imagestring($img, rand(1,5), rand(2,80), rand(2,10), $check, $white);
header("Content-Type:image/png");
imagepng($img);
session_start();
$check="";
for ($i=0; $i < 4; $i++) {
$check.=dechex(rand(1,15));
}
//将随机验证码保存在session中
$_SESSION['mycheck']=$check;
//创建图片
$img=imagecreatetruecolor(110, 30);
//背景默认为黑色
$bgcolor=imagecolorallocate($img, 0, 0, 0);
imagefill($img, 0, 0, $bgcolor);
//创建新的颜色
$white=imagecolorallocate($img, 255, 255, 255);
$blue=imagecolorallocate($img, 0, 0, 255);
$red=imagecolorallocate($img, 255, 0, 0);
$green=imagecolorallocate($img, 255, 0, 0);
//画出干扰线段
for ($i=0; $i <10 ; $i++) {
imageline($img,rand(0,110),rand(0,30),rand(0,110), rand(0,30),
imagecolorallocate($img, rand(0,255), rand(0,255), rand(0,255)));
}
//画噪点
imagestring($img, rand(1,5), rand(2,80), rand(2,10), $check, $white);
header("Content-Type:image/png");
imagepng($img);
?>