ThinkPHP Captcha using a concise tutorial
- 2021-01-25 07:19:09
- OfStack
1. The first thing you need to add validation code in controller methods admin Lib/Action/LoginAction class. php
Knowledge:
1, ob_clean function purpose
2. The import method calls the think default library
3, Image class buildImageVerify method use
The code is as follows:
Public function verify(){
ob_clean();
//ob_clean function Clear previous output
import('ORG.Util.Image');
//import Call is message/ThinkPHP Expansion packages under the framework directory Extend/Library/ORG/Util/ In the Image.class.php The class file
Image::buildImageVerify();
// call buildImageVerify Method generates a CAPTCHA with the default parameter ( $length=4, $mode=1, $type='png', $width=48, $height=22, $verifyName='verify' ), interested friends can study Image class
}
2. Add captcha module in a template file admin Tpl/Login/index html
Knowledge:
1. Captcha picture call
2. Understand js related operation process
3, __PUBLIC__ constant
Enter the following code after the password field:
<!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__/Js/jquery-1.7.2.min.js"></script>
//__PUBLIC__ Constants can be defined in "View Page Source File" after the page has loaded __PUBLIC__ Directory, default to the project root directory message Under the Public Directory, how do I customize it __PUBLIC__ The system constant
//jquery File, can only be called when the file is loaded jquery The method of
<script type="text/javascript" src="__PUBLIC__/Js/login.js"></script>
// The actual code here is posted below, but it's actually just defined 1 a change_code Function, which is an asynchronous operation to replace the captcha (which can be changed without refreshing the page)
<title>Message Board BackGround</title>
</head>
<body>
<form action="{:U('admin.php/Login/login')}" method="post" name="back_login" >
<h2> Simple background login system </h2>
User name: <input type='username' name='username' id='username' />
<br />
Password: <input type='password' name='password' id='password' />
<br />
Verification code: <input type="code" name="code"/>
// It's important to note here that type Set to code . name named code
<img src="{:U('Admin/Login/verify','','')}" id="code"/>
//img Under the label of src It is called Login In the controller veryfy methods
// The following two parameters are required to be left blank, where the first 2 No practical significance, mainly for the first 3 This setting can be used to cancel the pseudo-static suffix name, otherwise the default pseudo-static suffix name html , will cause the image to fail to load properly
<a href="javascript:void(change_code(this));"> Can't see </a>
// There's a call here
<br />
<input type="submit" value=" The login "/>
</form>
</body>
</html>
The login. js file is in the message/Public/ directory
The document reads as follows:
verifyURL = 'http://localhost/message/admin.php/Login/verify';
// Define the CAPTCHA path
function change_code(obj){
$("#code").attr("src",verifyURL+'/'+Math.random());
// Dynamic generation of verification code method, interested friends can study in depth jq methods
return false;
}
Other: __PUBLIC__ file defined in message admin/Conf/config php
In the configuration, add the following to change the __PUBLIC__ path
The configuration is as follows:
// Constant dependent configuration
'TMPL_PARSE_STRING' => array(
'__PUBLIC__' => __ROOT__ . '/' . APP_NAME . '/Public',
// The path is changed to message/admin/Public , refresh the page, the verification code can still be refreshed, indicating js In effect, also can "view page source file"
),