


Implement login, registration, and password retrieval functions under the thinkphp framework_php skills
The example in this article shares with you how to implement login, registration, and password retrieval using ajax form submission under the thinkphp framework, as well as the background review required for registered users.
The fields of the user table are id, num, password, name, email, addtime, status
The specific code is as follows
<?php namespace Home\Controller; use Think\Controller; class LoginController extends Controller { //处理登录 public function signin(){ if(IS_GET){ $this->display(); } if(IS_POST){ /* 调用登录接口登录 */ $User = M('user') ; //I方法接收页面传递来的值 $num = I('num') ; $password = I('password') ; //查找user表中num等于$num的值 $datanum = $User->where(array('num'=>$num))->find(); //判断$datanum的值 if ($datanum){ if (md5($password) === $datanum['password']) { if ($datanum['status'] == 0) { $this->error('用户处于未审核状态,请联系管理员'); }elseif($datanum['status'] == 2){ $this->error('用户处于禁用状态,请联系管理员'); }else{ $this->autoLogin($datanum) ; //调用私有方法自动登录. $uid = $datanum['id']; if($_SESSION['user_auth']['uid'] && $_SESSION['user_auth']['role'] == 'user'){ $this->success('登录成功!', U('Index/index')); }else{ $this->error('存储错误.'); } } }else{ $this->error('密码填写不正确,请重新填写'); exit(); } }else{ $this->error('用户不存在,请注册',U('signup')); } } } public function autoLogin($user){ /* 记录登录SESSION */ $auth = array( 'uid' => $user['id'], 'num' => $user['num'], 'role' => 'user' , //记录用户类型 ); session('user_auth', $auth); session('user_auth_sign', data_auth_sign($auth)); } /* * 用户注册 */ public function signup(){ if(is_user_login()){ $this->redirect('Index/index'); } if(IS_GET){ //注册页面 $this->display(); } if(IS_POST){ //判断用户 $data['num'] = I('num') ; $User = M('user') ; $datanum = $User->where($data)->find(); if ($datanum){ $this->success('您已经注册过,请直接登录',U('signin')); }else{ $data['password'] = md5(I('password')); $data['name'] = I('name'); $data['email'] = I('email'); $data['addtime'] = time(); $uid = $User->add($data); if($uid) $this->success('注册成功',U('signin')) ; else $this->error('注册失败') ; } } } public function logout(){ if(is_user_login()){ $User = M('user') ; session('user_auth', null); session('user_auth_sign', null); session('[destroy]'); $this->success('登出成功!', U('signin')); } else { $this->redirect('signin'); } } //忘记密码 public function wjpas(){ if(IS_GET){ $this->display(); } if(IS_POST){ $User = M('user') ; $num = I('num') ; $data['password'] = md5(I('password')) ; $email = I('email') ; $datanum = $User->where(array('num'=>$num))->find(); if ($datanum){ if ($email === $datanum['email']) { $User->where(array('num'=>$num))->save($data); // 根据条件更新记录 $this->success('密码修改成功',U('signin')) ; }else{ $this->error('邮箱填写不正确,请重新填写'); exit(); } }else{ $this->error('用户不存在,请注册',U('signup')); } } } } ?>
The above is the entire content of this article. I hope it will be helpful to everyone in learning PHP programming.

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP is not dying, but constantly adapting and evolving. 1) PHP has undergone multiple version iterations since 1994 to adapt to new technology trends. 2) It is currently widely used in e-commerce, content management systems and other fields. 3) PHP8 introduces JIT compiler and other functions to improve performance and modernization. 4) Use OPcache and follow PSR-12 standards to optimize performance and code quality.

The future of PHP will be achieved by adapting to new technology trends and introducing innovative features: 1) Adapting to cloud computing, containerization and microservice architectures, supporting Docker and Kubernetes; 2) introducing JIT compilers and enumeration types to improve performance and data processing efficiency; 3) Continuously optimize performance and promote best practices.

In PHP, trait is suitable for situations where method reuse is required but not suitable for inheritance. 1) Trait allows multiplexing methods in classes to avoid multiple inheritance complexity. 2) When using trait, you need to pay attention to method conflicts, which can be resolved through the alternative and as keywords. 3) Overuse of trait should be avoided and its single responsibility should be maintained to optimize performance and improve code maintainability.

Dependency Injection Container (DIC) is a tool that manages and provides object dependencies for use in PHP projects. The main benefits of DIC include: 1. Decoupling, making components independent, and the code is easy to maintain and test; 2. Flexibility, easy to replace or modify dependencies; 3. Testability, convenient for injecting mock objects for unit testing.

SplFixedArray is a fixed-size array in PHP, suitable for scenarios where high performance and low memory usage are required. 1) It needs to specify the size when creating to avoid the overhead caused by dynamic adjustment. 2) Based on C language array, directly operates memory and fast access speed. 3) Suitable for large-scale data processing and memory-sensitive environments, but it needs to be used with caution because its size is fixed.

PHP handles file uploads through the $\_FILES variable. The methods to ensure security include: 1. Check upload errors, 2. Verify file type and size, 3. Prevent file overwriting, 4. Move files to a permanent storage location.

In JavaScript, you can use NullCoalescingOperator(??) and NullCoalescingAssignmentOperator(??=). 1.??Returns the first non-null or non-undefined operand. 2.??= Assign the variable to the value of the right operand, but only if the variable is null or undefined. These operators simplify code logic, improve readability and performance.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Linux new version
SublimeText3 Linux latest version

Zend Studio 13.0.1
Powerful PHP integrated development environment

SublimeText3 Chinese version
Chinese version, very easy to use

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),