您现在的位置是:孟垂博自媒体博客 ✈ 学习笔记
PHP实现登录验证码功能及调用方法实例教程
08-02最后更新时间:2023-12-10【已收录】人已围观收藏
简介功能很简单,主要是怎么去验证这个思路,没写过,想的很复杂,知道了,感觉很简单。


以上为效果图。
实现流程:
1.新加一个ShowKey.php文件,这里是验证码显示的文件,具体怎么写的自己可以研究一下,这个是别人写的,拿来用就可以了,自己写的也不一定好。
实现流程:
1.新加一个ShowKey.php文件,这里是验证码显示的文件,具体怎么写的自己可以研究一下,这个是别人写的,拿来用就可以了,自己写的也不一定好。
<?php
session_start();
//设置COOKIE或Session
function esetcookie($name,$str,$life=0){
//本函数将字符串 str 全部变小写字符串使验证码输入不区分大小写----在提交表单进行session比较同样需要次函数转化
$_SESSION[$name]=strtolower($str);
}
//获取随机字符 此函数区分字符大小写 如果不区分大小写可加入函数strtolower
function domake_password($len)
{
$chars = array(
/*"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k",
"l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v",
"w", "x", "y", "z", "A", "B", "C", "D", "E", "F", "G",
"H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R",
"S", "T", "U", "V", "W", "X", "Y", "Z",*/ "0", "1", "2",
"3", "4", "5", "6", "7", "8", "9"
);
$charsLen = count($chars) - 1;
shuffle($chars);// 将数组打乱
$output = "";
for ($i=0; $i<$len; $i++)
{
$output .= $chars[mt_rand(0, $charsLen)]; //获得一个数组元素
}
return $output;
}
//显示验证码
function ShowKey(){
$key=domake_password(4);//获取随机值
$set=esetcookie("checkkey",$key);//将随机值写入cookie或session
//是否支持gd库
if(function_exists("imagejpeg"))
{
header ("Content-type: image/jpeg");
$img=imagecreate(47,20);
$blue=imagecolorallocate($img,102,102,102);
$white=ImageColorAllocate($img,255,255,255);
$black=ImageColorAllocate($img,71,71,71);
imagefill($img,0,0,$blue);
imagestring($img,5,6,3,$key,$white);
for($i=0;$i<90;$i++) //加入干扰象素
{
imagesetpixel($img,rand()%70,rand()%30,$black);
}
imagejpeg($img);
imagedestroy($img);
}
elseif (function_exists("imagepng"))
{
header ("Content-type: image/png");
$img=imagecreate(47,20);
$blue=imagecolorallocate($img,102,102,102);
$white=ImageColorAllocate($img,255,255,255);
$black=ImageColorAllocate($img,71,71,71);
imagefill($img,0,0,$blue);
imagestring($img,5,6,3,$key,$white);
for($i=0;$i<90;$i++) //加入干扰象素
{
imagesetpixel($img,rand()%70,rand()%30,$black);
}
imagepng($img);
imagedestroy($img);
}
elseif (function_exists("imagegif"))
{
header("Content-type: image/gif");
$img=imagecreate(47,20);
$blue=imagecolorallocate($img,102,102,102);
$white=ImageColorAllocate($img,255,255,255);
$black=ImageColorAllocate($img,71,71,71);
imagefill($img,0,0,$blue);
imagestring($img,5,6,3,$key,$white);
for($i=0;$i<90;$i++) //加入干扰象素
{
imagesetpixel($img,rand()%70,rand()%30,$black);
}
imagegif($img);
imagedestroy($img);
}
elseif (function_exists("imagewbmp"))
{
header ("Content-type: image/vnd.wap.wbmp");
$img=imagecreate(47,20);
$blue=imagecolorallocate($img,102,102,102);
$white=ImageColorAllocate($img,255,255,255);
$black=ImageColorAllocate($img,71,71,71);
imagefill($img,0,0,$blue);
imagestring($img,5,6,3,$key,$white);
for($i=0;$i<90;$i++) //加入干扰象素
{
imagesetpixel($img,rand()%70,rand()%30,$black);
}
imagewbmp($img);
imagedestroy($img);
}
else
{
//不支持验证码
header("content-type:image/jpegrn");
header("Pragma:no-cachern");
header("Cache-Control:no-cachern");
header("Expires:0rn");
$fp = fopen("data/vdcode.jpg","r");
}
}
ShowKey();
?>
2.前台页面调用:注意input里面的name值,这个值要传递到检查登录的文件中。
<div class="form-group">
<label class="col-sm-2 control-label">验证码:</label>
<div class="col-sm-10">
<input type="text" name="imgId" class="form-control" placeholder="验证码"><img src="ShowKey.php" name="KeyImg" id="KeyImg" onClick="KeyImg.src='ShowKey.php?'+Math.random()">
</div>
</div>
3.session_start();这个必须要加到顶部,不然是验证不过的。
<?php
session_start(); //必须的,很多都是因为这个一个验证不通过
header("Content-type:text/html;charset=utf-8");
//接收表单的数据
$username = $_POST['username'];
$password = $_POST['password'];
$imgId_req = $_REQUEST['imgId']; //接受表单传递的数据
//验证该字符串是否注册了session变量
if($_SESSION['checkkey']==$imgId_req){
echo "<script>alert('验证通过');
window.location.href='login.php';</script>";
}
else{
echo "<script>alert('验证不通过');
window.location.href='login.php';</script>";
}

《PHP实现登录验证码功能及调用方法实例教程.doc》
如果这篇文章对你有所帮助,劳烦点个赞
推荐度:

很赞哦! ()
相关文章
- 帝国cms7.5改版会员中心点击显示验证码没反应问题解决2019-08-23
文章评论
点击排行
本栏推荐
标签云(最新)
站点信息
- 建站时间:2019年06月15日
- 网站程序:帝国CMS7.5
- 博客模板:可免费共享
- 文章统计:235篇文章
- 时间卷轴:时间轴
- 标签管理:标签云
- 网站地图:XML网站地图
- 微信二维码:扫描一下,你我就是有缘