一、验证码
1.页面前端显示 (验证码是图片标签,来源是控制器里的yzm()操作方法)
<h1>登录</h1>
<form action="__ACTION__" method="post">
<div>用户名:<input type="text" name="uid"/></div>
<div>密码:<input type="password" name="pwd"/></div>
<div>验证码:<input type="text" name="yzm"/><img id="yzm" src="__CONTROLLER__/yzm"/></div>
<input type="submit" value="登录"/>
</form>
注意:点击验证码进行刷新,使用jq
<script type="text/javascript">
$(document).ready(function(e) {
$("#yzm").click(function(){
$(this).attr("src","__CONTROLLER__/yzm");
})
});
</script>
2.后端验证码生成,操作方法yzm()
//生成验证码
function yzm()
{
$config=array(
"footSize"=>15,
"imgW"=>100,
"imgH"=>15,
"length"=>4,
"useZh"=>true, ); $Verify=new \Think\Verify($config); //引用验证码类Verify
$Verify->entry(2);//生成验证码标示为2
}
3.登陆后盾操作方法:Login()
function Login()
{
if(empty($_POST))
{
$this->display();
}
else
{
$code=$_POST["yzm"];
$verify=new \Think\Verify();
$z=$verify->check($code,2); //检测验证码
if($z)
{
if($_POST["uid"]!=""&&$_POST["pwd"]!="")//判断账号和密码是否为空
{
$uid=$_POST["uid"];
$pwd=$_POST["pwd"];
$model=D("user");
$z=$model->field("pwd")->find($uid); if($z["pwd"]==$pwd)
{
session("uid",$uid);
$this->success("登陆成功",U("Main/Main"));
}
else
{
$this->error("登陆失败","Login");
}
}
else
{
$this->error("用户名或密码不能为空","Login",1);
}
}
else
{
$this->error("验证码错误!");
}
}
}
二、文件上传
1.文件上传前端:shangchuan.html
<body>
<h1>文件上传</h1>
<form action="__ACTION__" enctype="multipart/form-data" method="post">
<div><input type="file" name="photo"/></div>
<div><input type="submit" value="提交"/></div>
</form>
</body>
2.后端操作方法:shangchuan()
//上传表单
function ShangChuan()
{
if(empty($_FILES))
{
$this->display();
}
else
{
$upload=new \Think\Upload();//实例化上传类
$upload->maxSize=3145728;//上传文件大小
$upload->exts=array("jpg","gif","png","jpeg");//上传文件格式
$upload->rootPath='./Public/Uploads/';
//$upload->savaPath='Uploads/';
$upload->saveName="" ; $info=$upload->upload();
//var_dump($info);
if($info)
{
$this->success("上传成功");
}
else
{
$this->error($upload->getError(),"ShangChuan");
}
}
}
三、获取ip地址
function ip()
{
$ip=get_client_ip(); //获取客户端ip地址 $IP=new Org\Net\Iplocation("UTFWry.dat"); //实例化类,参数表示ip地址库文件
$area=$IP->getlocation($ip); //获取某个ip地址所在的位置 }