Home  >  Article  >  Backend Development  >  A concise tutorial on using ThinkPHP verification code_PHP tutorial

A concise tutorial on using ThinkPHP verification code_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:37:06866browse

1. First, you need to add the verification code method admin/Lib/Action/LoginAction.class.php in the controller
Knowledge points:
1. Purpose of ob_clean function
2 , the import method calls think default class library
3. The Image class buildImageVerify method is used

The code is as follows:

Copy codeThe code is as follows:

Public function verify(){
ob_clean() ;
//ob_clean function clears the previous output
import('ORG.Util.Image');
//import calls the extension package Extend/Library/ORG/Util in the message/ThinkPHP framework directory Image.class.php class file in /
Image::buildImageVerify();
//Call the buildImageVerify method to generate a verification code. The default parameters are ($length=4, $mode=1, $type=' png', $width=48, $height=22, $verifyName='verify'), interested friends can study the Image class
}

2. Add the verification code module admin/Tpl/Login/index.html to the template file
Knowledge points:
1. Verification code image call
2. js related Understanding the running process
3. __PUBLIC__ constant

Add the following code after the password input box:

Copy the code The code is as follows:






//__PUBLIC__ constant can be found in the defined __PUBLIC__ directory in "View Page Source File" after the page is loaded. The default is the project root Public directory under the message directory. The following will explain how to customize the __PUBLIC__ system constant
//jquery file. Only when this file is loaded can the jquery method be called

//The specific code here will be posted below. In fact, it just defines a change_code function, which is an asynchronous operation to change the verification code ( Can be changed without refreshing the page)

Message Board BackGround


Simple background login system< /h2>
Username:


Password:


Verification code:
/ /Here you need to pay attention to setting type to code and name to code
//The src under the img tag is to call the veryfy method in the Login controller
//It is necessary to leave the last two parameters blank, the second one has no practical meaning, mainly for the third one Leave the parameter blank, so that the pseudo-static suffix can be canceled. Otherwise, the default pseudo-static suffix is ​​html, which will cause the image to fail to load normally
Can’t see clearly
//A
is called here





The login.js file is in the message/Public/ directory
The content of the file is as follows:
Copy code The code is as follows:

verifyURL = 'http://localhost/message/admin.php/Login/verify';
// Define verification code path
function change_code(obj){
$("#code").attr("src",verifyURL+'/'+Math.random());
//Dynamic generation of verification Coding method, interested friends can delve into the jq method
return false;
}

Another: The __PUBLIC__ file is defined in message/admin/Conf/config.php
In the configuration, add the following content to change the __PUBLIC__ path
The configuration is as follows:

Copy code The 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, the verification code can still be refreshed, which means that js is effective, you can also "View page source file"
),

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/736799.htmlTechArticle1. First, you need to add the verification code method admin/Lib/Action/LoginAction.class.php to the controller. Knowledge Points: 1. Purpose of ob_clean function 2. The import method calls think default class library 3. Image class...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn