search
HomeBackend DevelopmentPHP TutorialThinkPHP5 development (2) Detailed explanation of graphic code to implement registration function with background verification

Effect:

ThinkPHP5 development (2) Detailed explanation of graphic code to implement registration function with background verification
Verification effect:
ThinkPHP5 development (2) Detailed explanation of graphic code to implement registration function with background verification
ThinkPHP5 development (2) Detailed explanation of graphic code to implement registration function with background verification
ThinkPHP5 development (2) Detailed explanation of graphic code to implement registration function with background verification
ThinkPHP5 development (2) Detailed explanation of graphic code to implement registration function with background verification## 1. Database SQL script

-- phpMyAdmin SQL Dump
-- version 4.4.15.5
-- http://www.phpmyadmin.net
--
-- Host: localhost
-- Generation Time: 2016-07-09 13:12:42
-- 服务器版本: 5.7.12-log
-- PHP Version: 7.0.7

SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";


/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8mb4 */;

--
-- Database: `thinkphp5`
--

-- --------------------------------------------------------

--
-- 表的结构 `think_user`
--

CREATE TABLE IF NOT EXISTS `think_user` (
  `user_id` int(11) NOT NULL,
  `user_name` varchar(255) NOT NULL,
  `user_sex` int(11) DEFAULT NULL,
  `user_tel` varchar(255) DEFAULT NULL,
  `user_email` varchar(255) DEFAULT NULL,
  `user_address` varchar(255) DEFAULT NULL,
  `user_birth` varchar(255) DEFAULT NULL,
  `user_jointime` varchar(255) DEFAULT NULL,
  `user_passwd` varchar(255) DEFAULT NULL,
  `user_signature` varchar(255) DEFAULT NULL,
  `user_hobby` varchar(255) DEFAULT NULL
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4;

--
-- 转存表中的数据 `think_user`
--

INSERT INTO `think_user` (`user_id`, `user_name`, `user_sex`, `user_tel`, `user_email`, `user_address`, `user_birth`, 
`user_jointime`, `user_passwd`, `user_signature`, `user_hobby`) VALUES
(1, 'thinkphp', 1, '15700000000', 'emial@email.com', '山东省济南市****路', '1111111', '111111', 'qqq', NULL, NULL),
(2, 'pangPython', 1, '15700000000', 'email@emial.com', '山东省济南市', '201607096', NULL, '123456', 'nihao', '上速度速度');

--
-- Indexes for dumped tables
--

--
-- Indexes for table `think_user`
--
ALTER TABLE `think_user`
  ADD PRIMARY KEY (`user_id`);

--
-- AUTO_INCREMENT for dumped tables
--

--
-- AUTO_INCREMENT for table `think_user`
--
ALTER TABLE `think_user`
  MODIFY `user_id` int(11) NOT NULL AUTO_INCREMENT,AUTO_INCREMENT=3;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;

Code

Directory structure:
ThinkPHP5 development (2) Detailed explanation of graphic code to implement registration function with background verification2. Write
view view/regist/index. html

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>注册</title>
  </head>
  <style>
    dl{
        text-align:center;
        border:2px solid #00CC99;
        margin-top:100px;
      margin-bottom:100px;
      margin-right:400px;
      margin-left:400px;
    }
  </style>
  <body>

<div id="regist_form">
<dl>
  <form action="regist" method="post">
<dt>
    <p>姓名:<input type="text" name="user_name"></p>
</dt>

<dt>
  <p>性别:<input type="text" name="UserSex"></p>
</dt>

<dt>

  <p>手机:<input type="text" name="UserTel"></p>
</dt>
<dt>

  <p>邮件:<input type="text" name="UserEmail"></p>
</dt>

<dt>

  <p>地址:<input type="text" name="UserAddress"></p>
</dt>

<dt>
  <p>生日:<input type="text" name="UserBirth"></p>
</dt>

<dt>
    <p>加入时间:<input type="text" name="UserJoinTime"></p>
</dt>

<dt>
  <p>密码:<input type="password" name="UserPasswd"></p>
</dt>

<dt>
  <p>个性签名:<input type="text" name="UserSignature"></p>
</dt>

<dt>
  <p>喜好:<input type="text" name="UserHobby"></p>
</dt>
<dt>
  <p><input type="submit" value="注册"></p>
</dt>
  </form>

</dl>
</div>
  </body>
</html>

3. Write

controller controller/Regist.php

<?php
namespace app\index\controller;
use think\View;
use think\Controller;
use app\index\model\User;
class Regist extends Controller{

  public function index(){
    $view = new View();
    return $view->fetch(&#39;index&#39;);

  }

  //用户注册
  public function regist(){
    //实例化User
    $user = new User;
    //接收前端表单提交的数据
    $user->user_name = input(&#39;post.user_name&#39;);
    $user->user_sex = input(&#39;post.UserSex&#39;);
    $user->user_tel = input(&#39;post.UserTel&#39;);
    $user->user_email = input(&#39;post.UserEmail&#39;);
    $user->user_address = input(&#39;post.UserAddress&#39;);
    $user->user_birth = input(&#39;post.UserJoinTime&#39;);
    $user->user_passwd = input(&#39;post.UserPasswd&#39;);
    $user->user_signature = input(&#39;post.UserSignature&#39;);
    $user->user_hobby = input(&#39;post.UserHobby&#39;);
    //进行规则验证
    $result = $this->validate(
      [
        &#39;name&#39; => $user->user_name,
        &#39;email&#39; => $user->user_email,
        &#39;sex&#39; => $user->user_sex,
        &#39;tel&#39; => $user->user_tel,
        &#39;address&#39; => $user->user_address,
        &#39;birth&#39; => $user->user_birth,
        &#39;password&#39; => $user->user_passwd,
      ],
      [
        &#39;name&#39; => &#39;require|max:10&#39;,
        &#39;email&#39; => &#39;email&#39;,
        &#39;sex&#39; => &#39;number|between:0,1&#39;,
        &#39;tel&#39; => &#39;require&#39;,
        &#39;address&#39; => &#39;require&#39;,
        &#39;birth&#39; => &#39;require&#39;,
        &#39;password&#39; => &#39;require&#39;,
      ]);
    if (true !== $result) {
      $this->error($result);
    }

    //写入数据库
    if ($user->save()) {
      return $this->success(&#39;注册成功&#39;);
    } else {
      return $this->success(&#39;注册失败&#39;);
    }


  }
}

thinkphp5 manual provides many methods for verification, which can be done in the controller For verification, you can use the

helper function , which can be verified in the model. However, verification in the model requires the User class to inherit the verification class, but PHP does not support multiple inheritance. You need to choose which method is best for you. In terms of details,
Verification rules can be customized to use regular expressions to verify mobile phone numbers, ID numbers, etc.

model/User.php

<?phpnamespace app\index\model;use think\Model;class User extends Model{
    protected $pk = &#39;user_id&#39;;

}

Visited URL:

http://localhost/tp5/public/index.php/index/regist/

The above is the detailed content of ThinkPHP5 development (2) Detailed explanation of graphic code to implement registration function with background verification. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
How to make PHP applications fasterHow to make PHP applications fasterMay 12, 2025 am 12:12 AM

TomakePHPapplicationsfaster,followthesesteps:1)UseOpcodeCachinglikeOPcachetostoreprecompiledscriptbytecode.2)MinimizeDatabaseQueriesbyusingquerycachingandefficientindexing.3)LeveragePHP7 Featuresforbettercodeefficiency.4)ImplementCachingStrategiessuc

PHP Performance Optimization Checklist: Improve Speed NowPHP Performance Optimization Checklist: Improve Speed NowMay 12, 2025 am 12:07 AM

ToimprovePHPapplicationspeed,followthesesteps:1)EnableopcodecachingwithAPCutoreducescriptexecutiontime.2)ImplementdatabasequerycachingusingPDOtominimizedatabasehits.3)UseHTTP/2tomultiplexrequestsandreduceconnectionoverhead.4)Limitsessionusagebyclosin

PHP Dependency Injection: Improve Code TestabilityPHP Dependency Injection: Improve Code TestabilityMay 12, 2025 am 12:03 AM

Dependency injection (DI) significantly improves the testability of PHP code by explicitly transitive dependencies. 1) DI decoupling classes and specific implementations make testing and maintenance more flexible. 2) Among the three types, the constructor injects explicit expression dependencies to keep the state consistent. 3) Use DI containers to manage complex dependencies to improve code quality and development efficiency.

PHP Performance Optimization: Database Query OptimizationPHP Performance Optimization: Database Query OptimizationMay 12, 2025 am 12:02 AM

DatabasequeryoptimizationinPHPinvolvesseveralstrategiestoenhanceperformance.1)Selectonlynecessarycolumnstoreducedatatransfer.2)Useindexingtospeedupdataretrieval.3)Implementquerycachingtostoreresultsoffrequentqueries.4)Utilizepreparedstatementsforeffi

Simple Guide: Sending Email with PHP ScriptSimple Guide: Sending Email with PHP ScriptMay 12, 2025 am 12:02 AM

PHPisusedforsendingemailsduetoitsbuilt-inmail()functionandsupportivelibrarieslikePHPMailerandSwiftMailer.1)Usethemail()functionforbasicemails,butithaslimitations.2)EmployPHPMailerforadvancedfeatureslikeHTMLemailsandattachments.3)Improvedeliverability

PHP Performance: Identifying and Fixing BottlenecksPHP Performance: Identifying and Fixing BottlenecksMay 11, 2025 am 12:13 AM

PHP performance bottlenecks can be solved through the following steps: 1) Use Xdebug or Blackfire for performance analysis to find out the problem; 2) Optimize database queries and use caches, such as APCu; 3) Use efficient functions such as array_filter to optimize array operations; 4) Configure OPcache for bytecode cache; 5) Optimize the front-end, such as reducing HTTP requests and optimizing pictures; 6) Continuously monitor and optimize performance. Through these methods, the performance of PHP applications can be significantly improved.

Dependency Injection for PHP: a quick summaryDependency Injection for PHP: a quick summaryMay 11, 2025 am 12:09 AM

DependencyInjection(DI)inPHPisadesignpatternthatmanagesandreducesclassdependencies,enhancingcodemodularity,testability,andmaintainability.Itallowspassingdependencieslikedatabaseconnectionstoclassesasparameters,facilitatingeasiertestingandscalability.

Increase PHP Performance: Caching Strategies & TechniquesIncrease PHP Performance: Caching Strategies & TechniquesMay 11, 2025 am 12:08 AM

CachingimprovesPHPperformancebystoringresultsofcomputationsorqueriesforquickretrieval,reducingserverloadandenhancingresponsetimes.Effectivestrategiesinclude:1)Opcodecaching,whichstorescompiledPHPscriptsinmemorytoskipcompilation;2)DatacachingusingMemc

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.