Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use ThinkPHP CAPTCHA

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces "how to use ThinkPHP CAPTCHA". In daily operation, I believe many people have doubts about how to use ThinkPHP CAPTCHA. The editor consulted all kinds of data and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "how to use ThinkPHP CAPTCHA". Next, please follow the editor to study!

First, we need to add the verification code method admin/Lib/Action/LoginAction.class.php to the controller.

Knowledge points:

1. The purpose of ob_clean function

2. Import method calls think default class library

3. Use the buildImageVerify method of the Image class

The code is as follows:

The copy code is as follows:

Public function verify () {

Ob_clean ()

/ / the ob_clean function clears the previous output

Import ('ORG.Util.Image')

/ / import calls the Image.class.php class file in the expansion pack Extend/Library/ORG/Util/ in the message/ThinkPHP framework directory

Image::buildImageVerify ()

/ / call the buildImageVerify method to generate the verification code. The default parameters are ($length=4, $mode=1, $type='png', $width=48, $height=22, $verifyName='verify'). If you are interested, you can study the Image class.

}

Add the CAPTCHA module admin/Tpl/Login/index.html to the template file

Knowledge points:

1. CAPTCHA picture call

2. Understand the related operation process of js

3. _ _ PUBLIC__ constant

Add the following code after the password entry box:

The copy code is as follows:

/ / _ _ PUBLIC__ constant can be found in the defined _ _ PUBLIC__ directory in "View Page Source File" after the page is loaded. By default, it is the Public directory under the project root directory message. The following will explain how to customize the _ _ PUBLIC__ system constant.

/ / jquery file. Only when this file is loaded can the method of jquery be called.

/ / the specific code here will be posted below, but it only defines a change_code function, that is, the asynchronous operation of changing the CAPTCHA (which can be changed without refreshing the page)

Message Board BackGround

Simple background login system

User name:

Password:

CAPTCHA:

/ / notice that type is set to code,name and named code.

/ / the src under the img tag calls the veryfy method in the Login controller.

/ / it is necessary to leave the last two parameters blank, of which the second parameter has no practical meaning, mainly for the third parameter. This setting can cancel the pseudo-static suffix name, otherwise the default pseudo-static suffix name is html, which will cause the picture not to be loaded normally.

Can't see clearly.

/ / A

The login.js file is in the message/Public/ directory

The contents of the document are as follows:

The copy code is as follows:

VerifyURL = 'http://localhost/message/admin.php/Login/verify';

/ / define the CAPTCHA path

Function change_code (obj) {

("# code") .attr ("src", verifyURL+'/'+Math.random ())

/ / the method of generating CAPTCHA dynamically. If you are interested, you can further study the jq method.

Return false

}

Also: _ _ PUBLIC__ file is defined in message/admin/Conf/config.php

In the configuration, add the following to change the _ _ PUBLIC__ path

The configuration is as follows:

The copy code is as follows:

/ / constant related configuration

'TMPL_PARSE_STRING' = > array (

'_ _ PUBLIC__' = > _ _ ROOT__. '/'. APP_NAME. '/ Public'

/ / change the path to message/admin/Public, refresh the page, and the CAPTCHA code can still be refreshed, indicating that js is effective, or you can "view the page source file".

),

At this point, the study on "how to use ThinkPHP CAPTCHA" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report