ThinkPHP Verification Code and Paging Example Tutorial

  • 2021-07-13 04:58:45
  • OfStack

In this paper, two common functions of ThinkPHP are described as examples: verification code and paging. It is very common in ThinkPHP project development and has high practical value. Complete examples are shared for your reference. The details are as follows:

1. Verification code:

Import a verification code class with verification code methods in aoli\ ThinkPHP\ Lib\ ORG\ Util\ Image.class. php

1. English verification code:


buildImageVerify($length,$mode,$type,$width,$height,$verifyName)

The parameters are as follows:

length: Length of verification code, default to 4 digits
mode: Verify the type of string, default to number, other supported types are 0 letter 1 number 2 uppercase letter 3 lowercase letter 4
Chinese 5 mixed (without the confusing characters oOLl and the number 01)
type: Image type of verification code, default to png
width: The width of the verification code is automatically calculated according to the length of the verification code by default
height: Verification code height, default is 22
verifyName: SESSION record name of verification code, default is verify

2. Chinese verification code:


GBVerify ($length,$type,$width,$height,$fontface,$verifyName)

The parameters are as follows:

length: Length of verification code, default to 4 digits
type: Image type of verification code, default to png
width: The width of the verification code is automatically calculated according to the length of the verification code by default
height: The height of the verification code, which defaults to 50
fontface: The font file used, using the full file name or under the directory where the image class is located. The default font file used is simhei. ttf (this file can be found under the Fonts directory of window)
verifyName: SESSION record name of verification code, default is verify

3. If the verification code cannot be displayed, please check:

① Whether GD library support has been installed on PHP;
② Whether there is any output before the output (especially the BOM header information output of UTF8);
③ Whether the Image class library is imported correctly;
4. If it is a Chinese verification code, check whether there are copied font files to the directory where the class library is located;

4. action section:

The code for the CommonAction. class. php page is as follows:


<?php
class CommonAction extends Action{
  function verify(){    
    import('ORG.Util.Image');
    // English verification code 
    //Image::buildImageVerify(5,5,gif,90,30,'verify');
    // Chinese verification code 
    Image::GBVerify();
  }  
  
}
?>

5. view template section:

The template index. html page reads as follows:


  Verification code: <input type="text" name="verify" /><img src="__APP__/common/verify" onclick="show(this)" /><br />
 <input type="submit" value=" Registration " />
</form>

<script type="text/javascript">
  function show(obj){
    obj.src="__APP__/common/verify/random/"+Math.random();    
  }
</script>

6. Controller:

The controller UserAction. class. php is as follows:


// Verification code verification 
if($_SESSION['verify']!=md5($_POST['verify'])){
  $this->error(' Incorrect verification code ');   
}

2. Paging:

1. Import the paging class with verification code methods in aoli\ ThinkPHP\ Lib\ ORG\ Util\ Page. class. php

2. action section:

The UserAction. class. php page reads as follows:


function index(){
  import('ORG.Util.Page');// Introducing distribution class 
  $user=M('user');
  $count=$user->count();
  $page=new Page($count,3);//1 Page display 5 Article 
  $page->setConfig('theme','<div style="font-weight:bold;"> Total: %totalRow%%header%&nbsp;%nowPage%/%totalPage% Page &nbsp;%first%&nbsp;%upPage%&nbsp;%prePage%&nbsp;%linkPage%&nbsp;%nextPage%&nbsp;%downPage%&nbsp;%end%</div>');
  $show=$page->show();
  $list=$user->field(array('id','username','createip'))->order('id desc')->limit($page->firstRow.','.$page->listRows)->select();
  $this->assign('alist',$list);
  $this->assign('page',$show);
  $this->display();
}

3. view template section:

Template page index. html page is as follows:


<volist name="alist" id="vo">
 <li><span>ID : </span>{$vo['id']}<span> User name: </span>{$vo['username']}<span> Registration ip : </span>{$vo['createip']}<a href="__URL__/del/id/{$vo['id']}"> Delete </a>&nbsp;&nbsp;<a href="__URL__/edit/id/{$vo['id']}"> Edit </a></li>
</volist>
{$page}

Interested readers can debug and run 1 ThinkPHP verification code and paging examples in this paper, and I believe there will be new gains.


Related articles: