Example of verification code login function implemented by thinkPHP
- 2021-08-12 02:11:01
- OfStack
This paper describes the verification code login function realized by thinkPHP. Share it for your reference, as follows:
Use the verification provided by thinkphp to realize the verification of account password + verification code on the login page
<?php
namespace Admin\Controller;
use Think\Controller;
use Think\Verify;
class LoginController extends Controller{
public function login(){
if($_POST){
$obj = new Verify();
if($obj->check(I('post.yanzhengma','','trim'))){
// The comment section is additional 1 A method of verifying passwords from a database
// $data['name'] = I('post.user_name');
// $data['psd'] = I('post.password');
// $row = M('user')->where($data)->find();
$name = I('post.user_name');
$psd = I('post.password');
$str = 'name ="'.$name. '" and tel = "'.$psd.'"';
var_dump($str);
$row = M('user')->where($str)->find();
if($row)
$this->redirect("Index/index");
else
$this->redirect('login','',1,' Wrong username or password ');
}
else{
$this->redirect('login','',1,' Verification code error ');
}
}
$this->display();
}
public function verifyImg(){
// Set the width, height, font size and the number of verification codes, and design other references Think\Verify The settings inside
$config=array(
'imageW' => 150,
'imageH' => 40,
'fontSize' => 20,
'length' => 4
);
$obj = new \Think\Verify($config);
$obj->entry();
}
}
Form section
<form action="login" method="post">
<table valign="top" width="50%">
<tr><td colspan="2"><h4 style="letter-spacing:1px;font-size:16px;">RainMan Website management background </h4></td></tr>
<tr><td> Administrator: </td><td><input type="text" name="user_name" value="" /></td></tr>
<tr><td> Dense Code: </td><td><input type="password" name="password" value="" /></td></tr>
<tr><td> Verification code: </td>
<td><input type="text" name="yanzhengma" value="" style="width:80px;"/></td>
<td><img src="__URL__/verifyImg" onclick="this.src='__URL__/verifyImg/'+Math.random()" alt=""/></td>
</tr>
<tr class="bt" align="center"><td> <input type="submit" value=" Landing " /></td><td> <input type="reset" value=" Refill " /></td></tr>
</table>
</form>
Readers who are interested in thinkPHP can check the topics of this site: "ThinkPHP Introduction Tutorial", "thinkPHP Template Operation Skills Summary", "ThinkPHP Common Methods Summary", "codeigniter Introduction Tutorial", "CI (CodeIgniter) Framework Advanced Tutorial", "Zend FrameWork Framework Introduction Tutorial" and "PHP Template Technology Summary".
I hope this article is helpful to the PHP programming based on ThinkPHP framework.