Joke Collection Website - Public benefit messages - How to call thinkphp verification code

How to call thinkphp verification code

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

Knowledge points:

1. ob_clean Function usage

2. The import method calls the think default class library

3. The Image class buildImageVerify method is used

The code is as follows:

. The code is as follows:

Public function verify(){

ob_clean();

//ob_clean function clears previous output

import( 'ORG.Util.Image');

//The import calls the Image.class.php class file in the extension package Extend/Library/ORG/Util/ in the message/ThinkPHP framework directory

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 to the template file Module admin/Tpl/Login/index.html

Knowledge points:

1. Verification code image call

2. Understanding js related running processes

3. __PUBLIC__ constant

Add the following code after the password input box:

. The code is as follows:

lt;!DOCTYPE html PUBLIC " -//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"gt;

lt;htmlgt;

lt;headgt;

lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8"gt;

lt; script type="text/javascript" src="__PUBLIC__/Js/jquery-1.7.2.min.js"gt;lt;/scriptgt;

//__PUBLIC__ constant can be used after the page is loaded. Find the defined __PUBLIC__ directory in "View page source file". The default is the Public directory under the message root directory of the project. The following will explain how to customize the __PUBLIC__ system constants

//jquery files, only Only after loading this file can you call the jquery method

lt;script type="text/javascript" src="__PUBLIC__/Js/login.js"gt;lt;/scriptgt;

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

lt; titlegt;Message Board BackGro

undlt;/titlegt;

lt;/headgt;

lt;bodygt;

lt;form action="{:U('admin.php/ Login/login')}" method="post" name="back_login" gt;

lt; h2gt; Simple background login system lt; /h2gt;

Username: lt ; input type='username' name='username' id='username' /gt;

lt; br /gt;

Password: lt; input type='password' name='password' id='password' /gt;

lt; br /gt;

Verification code: lt; input type="code" name="code"/ gt;

//You need to pay attention here to set type to code and name to code

lt; img src="{:U('Admin/Login/verify',' ', '')}" id="code"/gt;

//The src under the img tag is to call the veryfy method in the Login controller

//The next two It is necessary to leave the two parameters blank. The second one has no practical meaning. It is mainly to leave the third parameter blank. This setting can cancel the pseudo-static suffix name. Otherwise, the default pseudo-static suffix name is html, which will cause abnormality. Load images

lt;a href="javascript:void(change_code(this));"gt;cannot see clearlylt;/agt;

//A call is made here

lt; br /gt;

lt; input type="submit" value="Login"/gt;

lt;/formgt;

lt;/bodygt;

lt;/htmlgt;

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

The content of the file is as follows:

. 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 code method, interested friends can study the jq method in depth

return false;

}

Another: __PUBLIC__ The 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:

. The code is as follows:

//Constant related configuration

'TMPL_PARSE_STRING' =gt; array(

'__PUBLIC__' =gt; __ROOT__ . '/' .APP_NAME .'/Public',

//Change the path to message/admin/Public, refresh the page, and the verification code can still be refreshed, which means that the js is effective, and you can also "View the page source file"

),