ThinkPHP验证码与文件上传
【验证码】
首先 在控制器中做一个操纵方法 这个方法中造一个验证码类的对象 调用entry()方法
在哪个页面显示验证码 就在哪个页面调用 <img src="__CONTROLLER__/YZM"> 指向方法名
function YZM()
{
$v= new \Think\Verify();
$v->entry();
}
function Showyzm()
{
$this->display();
}
如果你需要在一个页面中生成多个验证码的话,entry方法需要传入可标识的信息
// 验证码1 $Verify = new \Think\Verify(); $Verify->entry(1); // 验证码2 $Verify = new \Think\Verify(); $Verify->entry(2);
可以对生成的验证码设置相关的参数,以达到不同的显示效果

参数设置使用两种方式
实例化传入参数:
$config=array(
'fontSize'=>50,
'length'=>3,
);
$v= new \Think\Verify($config);
$v->entry();
需注意 这里的参数 imageH imageW不好用 如需设置图片大小 可以在网页中<img>标签中设置
使用中文验证码 需同时设置以下两项
'useZh'=>true,
'fontttf'=>'simyou.ttf',ThinkPHP/Library/Think/Verify/zhttfs 需提前将中文字体放在此目录下指定出现的字符集
$Verify->codeSet = '0123456789';$Verify->zhSet = '们以我到他会作时要动国产的一是工就年阶义发成部民可出能方进在了不和有大这';
验证码检测
function Showyzm()
{
if(empty($_POST))
{
$this->display();
}
else
{
$yzm=$_POST["yzm"];
$verify = new \Think\Verify();
var_dump($verify->check($yzm));
}
}
如果是多个验证码,需要多加个标识
function YZM()
{
$config=array(
'fontSize'=>50,
'length'=>3,
//'useImgBg'=>true,
'useZh'=>true,
'fontttf'=>'simyou.ttf',
'zhSet'=>'一二三四',
);
$v= new \Think\Verify($config);
$v->entry(1);
}
function Showyzm()
{
if(empty($_POST))
{
$this->display();
}
else
{
$yzm=$_POST["yzm"];
$verify = new \Think\Verify();
var_dump($verify->check($yzm,1));
}
}
点击验证码图片更换验证码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<load href="__PUBLIC__/JS/jquery-1.11.2.min.js" />
</head>
<body>
<img id="tp" width="200" height="50" src="__CONTROLLER__/YZM">
<form action="__ACTION__" METHOD="post">
<input type="text" name="yzm">
<input type="submit">
</form>
</body>
<script type="text/javascript">
$(document).ready(function (e) {
$("#tp").click(function () {
var sj=Math.random();
$(this).attr("src","__CONTROLLER__/YZM/c/"+sj+"");
})
})
</script>
</html>
为避免浏览器缓存,需在图片地址后面加一个随机数 传一个值如c 然后跟上随机数 Math.random();
【文件上传】
function Fileup()
{
if(empty($_FILES))
{
$this->display();
}
else
{
$upload = new \Think\Upload();// 实例化上传类
$upload->maxSize=10240000;// 设置附件上传大小
$upload->exts=array("jpg","jpeg");// 设置附件上传类型
$upload->rootPath="./Public/";
$upload->savePath="Uploads/";//设置附件上传目录
$upload->saveName="time";//上传文件命名时间戳
$info=$upload->upload();
var_dump($info);
}
}
注意 设置文件上传目录时 以下两个属性一起设置
$upload->savePath="Uploads/";//设置附件上传目录 $upload->saveName="time";//上传文件命名时间戳
<form action="__ACTION__" method="post" enctype="multipart/form-data">
<input type="file" name="file">
<input type="submit">
</form>

浙公网安备 33010602011771号