ThinkPHP verification code and paging instance tutorial, thinkphp instance tutorial

Source: Internet
Author: User

ThinkPHP verification code and paging instance tutorial, thinkphp instance tutorial

This example describes two common features of ThinkPHP: verification code and paging. ThinkPHP is very common in project development and has high practical value. The complete instance is shared with you for your reference. The details are as follows:

I. Verification Code:

Import the verification code class. There is a verification code method 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: the length of the verification code. The default value is 4 digits.
Mode: Verify the string type. The default value is a number. Other supported types include 0 letters, 1 digits, 2 uppercase letters, 3 lowercase letters, and 4.
Mix Chinese 5 (remove confusing characters oOLl and number 01)
Type: The image type of the verification code. The default value is png.
Width: The Verification Code width. It is automatically calculated based on the verification code length by default.
Height: the height of the verification code. The default value is 22.
VerifyName: The SESSION Record Name of the verification code. The default value is verify.

2. Chinese verification code:

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

The parameters are as follows:

Length: the length of the verification code. The default value is 4 digits.
Type: The image type of the verification code. The default value is png.
Width: The Verification Code width. It is automatically calculated based on the verification code length by default.
Height: height of the verification code. The default value is 50.
Fontface: The font file used. Use the complete file name or put it under the directory where the image class is located. The default font file used is simhei. ttf (this file can be found in the Fonts directory of the window)
VerifyName: The SESSION Record Name of the verification code. The default value is verify.

3. If the Verification Code cannot be displayed, check:

①. Has PHP installed GD library support;
②. Whether there is any output before the output (especially the UTF8 BOM header information output );
③. Whether the Image class library is correctly imported;
④. Check whether the font file has been copied to the directory where the class library is located using the Chinese verification code;

4. action:

The CommonAction. class. php Page code is as follows:

<? Phpclass 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 is as follows:

Verification Code: <input type = "text" name = "verify"/>  <br/> <input type =" submit "value =" register "/> </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:

// Verify the verification code if ($ _ SESSION ['verify ']! = Md5 ($ _ POST ['verify ']) {$ this-> error ('incorrect Verification Code ');}

Ii. Paging:

1. Import the paging class. The verification code method is available in aoli \ ThinkPHP \ Lib \ ORG \ Util \ Page. class. php.

2. action:

The UserAction. class. php page is as follows:

Function index () {import ('org. util. page '); // introduces the distribution class $ user = M ('user'); $ count = $ user-> count (); $ page = new Page ($ count, 3); // five $ page-> setConfig ('Theme ',' <div style = "font-weight: bold;"> total: % totalRow % header % nowPage %/% totalPage % PAGE % first % upPage % prePage % linkPage % nextPage % downPage % 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:

The template page index.html is as follows:

<Volist name = "alist" id = "vo"> <li> <span> ID: </span >{$ vo ['id']} <span> User name: </span >{$ vo ['username']} <span> ip address registration: </span> {$ vo ['createip']} <a href = "_ URL _/del/id/{$ vo ['id']}"> Delete </a> <a href = "_ URL _/edit/id/{$ vo ['id']}"> edit </a> </li> </volist> {$ page}

Interested readers can debug and run the ThinkPHP verification code and paging instances in this article. I believe there will be new gains.


How to change the page pages and THINKPHP kernel, for example, http: // wwwttplmgcom/special/8 html

Use the setConfig method in the paging class to customize the paging style:
I have a previously used custom paging class. Let's change it.

/***** Enter public paging class * @ param array $ map paging filter condition * @ param class $ Form data model * @ param integer $ number of lines displayed on the limit page * @ param string $ order sorting * @ return array */public function _ list ($ map, $ Form, $ limit = 9, $ order = 'add _ Time') {$ res = array (); $ p = empty ($ _ GET ['P'])? 0: (int) $ _ GET ['P']; $ res ['LIST'] = $ Form-> field (true)-> where ($ map) -> order ($ order)-> page ($ p. ','. $ limit)-> select (); import ('org. util. page '); // import the paging class $ count = $ Form-> where ($ map)-> count (); // query the total number of records meeting the requirements $ Page = new Page ($ count, $ limit ); // The total number of incoming records and the number of records displayed on each Page in the instantiated paging class $ Page-> rollPage = 3; $ Page-> setConfig ('Theme ', "% upPage % linkPage % downPage % <li style = 'width: 50px; '> <a href = 'javascript: void (0) '> % nowPage %/% totalPage % page </a> </li> "); $ res ['page'] = $ Page-> show (); // return $ res ;}

When thinkphp is paging, how can we get the transfer variable of the next page on the previous page? I don't want to use the original paging effect to attach the above and next pages to the image.

Public function index () {// you can change the icon style to $ user = M ('user'); import ('org. util. page '); $ count = $ user-> count (); $ listRows = 5; $ page = new Page ($ count, $ listRows ); $ list = $ user-> limit ("{$ page-> firstRow}, {$ page-> listRows}")-> select (); $ page-> setConfig ('prev', ' '); // Previous page $ page-> setConfig ('Next', ' '); // next page // $ page-> setConfig ('first ', ''); // $ page-> setConfig ('last ',''); $ page-> setConfig ('Theme ',' % upPage % downPage % '); // only show the upper and lower page options $ this-> assign ('page ', $ page-> show (); $ this-> assign ('LIST', $ list); $ this-> display ();}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.