tp框架实现验证码验证

时间:2023-03-08 20:37:29
tp框架实现验证码验证

//实现验证页面

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head>
<script src="__PUBLIC__/js/jquery-1.11.2.min.js"></script>
<body>
<form action="__ACTION__" method="post">
<img src="__CONTROLLER__/yzm" id="img" />
<input type="text" name="yzm" />
<input type="submit" value="注册"/> </form>
<script>
$("#img").click(function(){
var a = Math.ceil(Math.random()*);//如果浏览器不兼容可以设置随机数(Math.ceil)是四舍五入化为整数
$(this).attr("src","__CONTROLLER__/yzm/a/"+a+"");//设点击事件从新设置src属性($(this).attr("src","__CONTROLLER__))如果是兼容性好的(浏览器可以使用)
//如果兼容不好的不许用生成随机数的方式如($(this).attr("src","__CONTROLLER__/yzm/a/"+a+"");)
})
</script>
</body>
</html>

//调方法实现验证<?php

namespace Home\Controller;
use Think\Controller;
class TestController extends Controller
{ public function yanzhengma()
{ if(empty($_POST))
{
$this->show();
}
else
{
$Verify= new \Think\Verify();//掉方法
var_dump($Verify->check($_POST["yzm"]));//输出在该验证码的常量如果为true成功 为false失败
} }
public function yzm()
{
$Verify= new \Think\Verify();
$Verify->fontSize=;//输出验证码的大小
//$Verify->length=2;//输出验证码的长度:如果=2就有2个随机数(默认是6位数) tp框架实现验证码验证
//$Verify->fontttf="5.ttf";//输出为5.ttf的字体
//$Verify->useImgBg=true;//开启验证码背景图片功能 随机使用Thinkphp/Library/Think/Verify/bgs目录下的随机数
tp框架实现验证码验证
$Verify->useZh=true;//使用中文验证码,可以设置为:true在(Think/Library/Think/Verify/zhttf)的目录下设置中文字体
tp框架实现验证码验证
$Verify->entry();//生成验证码
}
}