Simple ThinkPHP Verification Code tutorial

Source: Internet
Author: User

1. Add the verification code admin/Lib/Action/LoginAction. class. php to the Controller.
Knowledge point:
1. usage of the ob_clean Function
2. The import method calls the think default class library.
3. Use the Image buildImageVerify Method

The Code is as follows:
Copy codeThe 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 Extend/Library/ORG/Util/extension package under the message/ThinkPHP framework directory.
Image: buildImageVerify ();
// Call the buildImageVerify method to generate the verification code. The default parameter is ($ length = 4, $ mode = 1, $ type = 'png ', $ width = 48, $ height = 22, $ verifyName = 'verify '). If you are interested, you can study the Image class.
}

2. Add the verification code module admin/Tpl/Login/index.html to the template file.
Knowledge point:
1. Verification Code image call
2. About js-related Running Processes
3. _ PUBLIC _ constant

Add the following code to the password input box:
Copy codeThe Code is as follows:
<! DOCTYPE html PUBLIC "-// W3C // dtd html 4.01 Transitional // EN" "http://www.w3.org/TR/html4/loose.dtd">
<Html>
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8">
<Script type = "text/javascript" src = "_ PUBLIC _/javascript/jquery-1.7.2.min.js"> </script>
// The _ PUBLIC _ constant can find the defined _ PUBLIC _ directory in "view page source file" after loading the page, the default value is the Public directory under the project root directory message. The following describes how to customize _ PUBLIC _ system constants.
// Jquery file. The jquery method can be called only when this file is loaded.
<Script type = "text/javascript" src = "_ PUBLIC _/Js/login. js"> </script>
// The specific code here will be posted below. In fact, it only defines a change_code function, that is, an asynchronous operation to replace the verification code (you can change it without refreshing the new page)

<Title> Message Board BackGround </title>
</Head>
<Body>

<Form action = "{: U ('admin. php/Login/login')}" method = "post" name = "back_login">
<H2> simple background logon System
Username: <input type = 'username' name = 'username' id = 'username'/>
<Br/>
Password: <input type = 'Password' name = 'Password' id = 'Password'/>
<Br/>
Verification code: <input type = "code" name = "code"/>
// Set type to code and name it code.

// The src under the img label is to call the veryfy method in the Login controller.
// It is necessary to leave the remaining two parameters blank, of which 2nd do not have practical significance. This is mainly because 3rd parameters are left blank. In this way, the pseudo-static suffix name can be canceled, otherwise, the default pseudo-static suffix html will cause image loading failure.
<A href = "javascript: void (change_code (this);"> cannot see clearly </a>
// Call
<Br/>
<Input type = "submit" value = "login"/>
</Form>

</Body>
</Html>

The login. js file is in the message/Public/directory.
The file content is as follows:
Copy codeThe Code is as follows:
VerifyURL = 'HTTP: // localhost/message/admin. php/Login/verify ';
// Define the verification code path
Function change_code (obj ){
$ ("# Code"). attr ("src", verifyURL + '/' + Math. random ());
// Dynamically generate the verification code method. If you are interested, you can study the jq method in depth.
Return false;
}

The file __public _ is defined in message/admin/Conf/config. php.
Add the following content to the configuration to change the _ PUBLIC _ path.
The configuration is as follows:
Copy codeThe Code is as follows:
// Constant Configuration
'Tmpl _ PARSE_STRING '=> array (
'_ PUBLIC _' => _ ROOT _. '/'. APP_NAME. '/Public ',
// The path is changed to message/admin/Public. When the page is refreshed, the verification code can still be refreshed, indicating that js takes effect or "view the page source file"
),

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.