PHP随机验证码、供初学者参考

来源:互联网 发布:网络电视机直播软件 编辑:程序博客网 时间:2024/05/20 00:15

<?php
session_start();
//通过循环打印出五个随机数
for ($i=0;$i<=4;$i++){
$rand.=dechex(rand(0,15));
}
$_SESSION['check_pic']=$rand;

//设置一个图片大小

$image=imagecreatetruecolor(70,25);

//设置背景颜色
$bg=imagecolorallocate($image,0,0,0);
//设置字体颜色
$text=imagecolorallocate($image,rand(0,255),rand(0,255),rand(0,255));

imagestring($image,6,15,4,$rand,$text);
//这里开始通过循环打印出六条线
for ($i=0;$i<=3;$i++){
 
imageline($image,rand(0,65),0,0,20,$text);
}
for ($i=0;$i<=200;$i++){
 imagesetpixel($image,rand()%70,rand()%30,$text);
}
//输出这个图片
header("Content-type:image/jpeg");
imagejpeg($image);
?>

以下为调用页面

<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<?php
session_start();
if ($_POST['submit']) {
if ($_POST['rand']==$_SESSION['check_pic']) {
echo "验证码正确为".$_SESSION['check_pic'];  
}else {
 echo "验证码错误为".$_SESSION['check_pic'];

}
?>
<form action="" method="POST">
<input name="rand" type="text">&nbsp;<img src="rand_file1.php">
<input type="submit" name="submit" value="提交">
</form>

原创粉丝点击