继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

十二章 php简单验证码

手记佰篇
关注TA
已关注
手记 60
粉丝 58
获赞 272

<?php
session_start();
//在页首先要开启session,
//error_reporting(2047);
session_destroy();
//将session去掉,以每次都能取新的session值;
//用seesion 效果不错,也很方便
?>
<html>
<head>
<title>session 图片验证实例</title>
<style type="text/css">

login p{

margin-top: 15px;
line-height: 20px;
font-size: 14px;
font-weight: bold;
}

login img{

cursor:pointer;
}
form{
margin-left:20px;
}
</style>
</head>
<body>

<form id="login" action="" method="post">
<p>此例为session验证实例</p>
<p>
<span>验证码:</span>
<input type="text" name="validate" value="" size=10>
<img title="点击刷新" src=" /captcha.php" align="absbottom" onclick="this.src=' /captcha.php?'+Math.random();"></img>
</p>
<p>
<input type="submit">
</p>
</form>
<?php
//打印上一个session;
//echo "上一个session:<b>".$_SESSION["authnum_session"]."</b><br>";
$validate="";
if(isset($_POST["validate"])){
$validate=$_POST["validate"];
echo "您刚才输入的是:".$_POST["validate"]."<br>状态:";
echo "您刚才:".$_SESSION["verification"]."<br>状态:";
if($validate!=$_SESSION["verification"]){
//判断session值与用户输入的验证码是否一致;
echo "<font color=red>输入有误</font>";
}else{
echo "<font color=green>通过验证</font>";
}
}
?>

captcha.php
<?php
session_start();
function random($len) {
$srcstr = "1a2s3d4f5g6hj8k9qwertyupzxcvbnm";
mt_srand();
$strs = "";
for ($i = 0; $i < $len; $i++) {
$strs .= $srcstr[mt_rand(0, 30)];
}
return $strs;
}
//随机生成的字符串
$str = random(4);
//验证码图片的宽度
$width = 50;
//验证码图片的高度
$height = 25;
//声明需要创建的图层的图片格式
@ header("Content-Type:image/png");
//创建一个图层
$im = imagecreate($width, $height);
//背景色
$back = imagecolorallocate($im, 0xFF, 0xFF, 0xFF);
//模糊点颜色
$pix = imagecolorallocate($im, 187, 230, 247);
//字体色
$font = imagecolorallocate($im, 0, 0, 0);
//绘模糊作用的点
mt_srand();
for ($i = 0; $i < 1000; $i++) {
imagesetpixel($im, mt_rand(0, $width), mt_rand(0, $height), $pix);
}
//输出字符
imagestring($im, 5, 7, 5, $str, $font);
//输出矩形
imagerectangle($im, 0, 0, $width -1, $height -1, $font);
//输出图片
imagepng($im);
imagedestroy($im);
//$str = md5($str);
//选择 cookie
//SetCookie("verification", $str, time() + 7200, "/");
//选择 Session
$_SESSION["verification"] = $str;
?>

打开App,阅读手记
1人推荐
发表评论
随时随地看视频慕课网APP