Thinkphp 驗證碼、文件上傳

1、驗證碼

 

驗證碼參數javascript

例題:登陸時驗證下驗證碼php

LoginController.class.phphtml

<?php
namespace Home\Controller;
use Think\Controller;
class LoginController extends Controller
{
    public function Login()
    {
        if(empty($_POST))
        {
            $this->display();    
        }    
        else
        {
            //判斷驗證碼是否正確
            $code = $_POST["yzm"];//用戶輸入的驗證碼的值
            $verify = new \Think\Verify(); //生成驗證碼 
            if($verify->check($code))
            {
                if($_POST["uid"]!="")
                {
                    $model = D("users");        
        
                    $uid = $_POST["uid"];
                    $pwd = $_POST["pwd"];
                    
                    $attr = $model->field("Pwd")->find($uid);
                    //echo $attr["pwd"];
                    
                    if($pwd == $attr["pwd"])
                    {
                        session("uid",$uid);
                        $this->success("登陸成功","Main");
                    }
                    else
                    {
                        $this->error("登陸失敗");    
                    }
                }
                else
                {
                    $this->error("登陸失敗");    
                }
            
            }
            else
            {
                $this->error("驗證碼錯誤");    
            }
        }
    }
    
    //生成驗證碼的操做
    public function yzm()
    {
        $config =    array(   
        'fontSize'    =>    30,    // 驗證碼字體大小    
        'length'      =>    5,     // 驗證碼位數 
        //'useNoise'    =>    false, // 關閉驗證碼雜點
        'imageW'  => 200,//寬度
        'imageH'  => 100,//高度
        //'useZh' => true,//中文驗證碼
        //'fontttf' => 'Arvo-Regular.ttf',//指定驗證碼字體
        );
         
        $Verify = new \Think\Verify($config);
        //$Verify->fontttf = '7.ttf';  // 驗證碼字體使用 ThinkPHP/Library/Think/Verify/ttfs/5.ttf
        $Verify->entry();    
    }
    
View Code

Login.htmljava

<!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>
<script src="../../../../Public/four/llx/js/jquery-1.3.2.js"></script>
</head>

<body>
<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" /><br />
<img id="yzm" src="__CONTROLLER__/yzm" /></div>
<input type="submit" value="登陸" />

</form>
</body>
</html>
<script type="text/javascript">
$(document).ready(function(e) {
    $("#yzm").click(function(){
        //點擊圖片驗證碼改變
        $(this).attr("src","__CONTROLLER__/yzm");
        
        })
});
</script>
View Code

           

2、文件上傳

上傳參數

每一個文件信息又是一個記錄了下面信息的數組,包括:jquery

 

//文件上傳
    public function ShangChuan()
    {
         if(empty($_FILES))
         {
             $this->display();     
         }
         else
         {
              $upload = new \Think\Upload();// 實例化上傳類
              $upload->maxSize = 3145728 ;// 設置附件上傳大小
              $upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 設置附件上傳類型
            
              //$upload->mimes = '';
              $upload->rootPath = './Public/';
              $upload->savePath = 'Uploads/'; // 設置附件上傳目錄
              $upload->saveName = '';//保持上傳文件名不變

              
              // 上傳文件   
              $info = $upload->upload();
              var_dump($info);
              
              if(!$info)
              {
                   $this->error($upload->getError());
              }
              else
              {
                  // 上傳成功 獲取上傳文件信息
                  foreach($info as $file)
                  {        
                          $url=$file['savepath'].$file['savename'];
                          echo $url;
                          $this->assign("url",$url);
                            $this->display();
                           //$this->success('上傳成功!');
                  }
              }

         }    
    }
    
<!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>

<body>
<form action="__ACTION__" enctype="multipart/form-data" method="post" >

<input type="file" name="photo" />
<input type="submit" value="提交" >

</form>
</body>
</html>

相關文章
相關標籤/搜索