Home Backend Development PHP Tutorial ThinkPHP中的create方法与自动令牌验证实例教程_php实例

ThinkPHP中的create方法与自动令牌验证实例教程_php实例

May 16, 2016 pm 08:37 PM
create thinkphp method automatic

本文实例形式展示了ThinkPHP中的create方法与自动令牌验证的实现方法,具体步骤如下:

一、数据表结构

user表结构如下:

id username password

二、view模板部分

\aoli\Home\Tpl\default\User\create.html页面如下:

<form action="__URL__/addit" method="post">
 <input type="text" name="id" />
 <input type="text" name="username" />
 <input type="password" name="password" />
 <input type="submit" name="sub" value="提交" />
</form>

Copy after login

三、action部分:

\aoli\Home\Lib\Action.php页面如下:

<&#63;php
 class UserAction extends Action {
  function create(){
     $this->display();   
   }
   
   function addit(){
     //向表user中添加表单内容
     $user=M('user');
     $user->create();
     $user->add();
     //判断是否存在令牌验证
     if(!$user->autoCheckToken($_POST)){
       dump('no'); 
     }else{
       dump('yes');   
     }
 }
&#63;>

Copy after login

1、在对表单提交过来的数据进行操作之前,我们往往需要手动创建需要的数据,例如上面提交的表单数据:

 //实例化User模型
  $user=M('user');
 
 //获取表单的POST数据
  $data['username']=$_POST['username']
  $data['password']=$_POST['password']
 
 //写入到数据库
   $user->data($data)->add();
Copy after login

附:使用data方法创建的数据对象不会进行自动验证和过滤操作,需要自行处理,如果只是想简单创建一个数据对象,并且不需要完成一些额外的功能的话,可以使用data方法简单的创建数据对象。

2、ThinkPHP可以帮助我们快速地创建数据对象,最典型的应用就是自动根据表单数据创建数据对象。create方法创建的数据对象是保存在内存中的,并没有实际的写入到数据库中。

   //实例化user模型
    $user=M('user');
  
   //根据表单提交的POST数据创建数据对象,并保存在内存中,可以通过dump($user)查看
    $user=create();

   //把创建的数据对象写入数据库中
    $user->add();

Copy after login

3、create方法支持从其它方式创建数据对象,如,从其它的数据对象或者数组等。

   $data['name']='ThinkPHP';
   $data['eamil']='ThinkPHP@gmail.com';
   $user->create($data);

   甚至还可以支持从对象创建新的数据对象,如从user数据对象创建新的member数据对象
   $user=M('user');
   $user->find(1);
   $member=M('member');
   $member->create($user);

Copy after login

4、create方法在创建数据对象的同时,还完成了一些很有意义的工作,包括令牌验证、数据自动验证、字段类型查找,数据自动完成等。

因些,我们熟悉的令牌验证、自动验证和自动完成功能,其实都必须通过create方法才能生效。

5、令牌验证:

功能:可以有效防止表单的远程提交等安全防护。

config.php中添加如下配置:

   'TOKEN_ON'   =>  true, //是否开启令牌验证
   'TOKEN_NAME'  =>  'token',// 令牌验证的表单隐藏字段名称
   'TOKEN_TYPE'  =>  'md5',//令牌验证哈希规则

Copy after login

自动令牌会向当前SESSION会话当中放上一个md5加密的字符串。并将这个字符串以隐藏域的形式插入到表单的form之前。这个字符串出现在两个地方,一个是在SESSION当中,另一个就是在表单当中。当你提交表单后,服务器第一件事就是对比这个SESSION信息,如果正确的话,准许表单提交,否则不允许提交。

查看create.html的的源代码会看到在表单form的结束标志之前会多了一个自动生成的隐藏域

<input type="hidden" name="token" value="eef419c3d14c9c93caa7627eedaba4a5" />
Copy after login

(1)、如果希望自己控制隐藏域的位置,可以手动在表单页面添加 {__TOKEN__} 标识,系统会在输出模板的时候自动替换。

(2)、如果在开启表单令牌验证的情况下,个别表单不需要使用令牌验证
功能,可以在表单页面添加 {__NOTOKEN__} ,则系统会忽略当前表单的令牌验证。

(3)、如果页面中存在多个表单,建议添加 {__TOKEN__} 标识,并确保只有一个表单需要令牌验证。

(4)、如果使用create方法创建数据对象的话,会同时自动进行表单验证,如果没有使用该方法的话,则需要手动调用模型的autoCheckToken方法进行表单验证。

if (!$User->autoCheckToken($_POST)){
// 令牌验证错误
}
Copy after login

希望本文所示实例对大家的ThinkPHP程序设计有所帮助。

Statement of this Website
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

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to write a novel in the Tomato Free Novel app. Share the tutorial on how to write a novel in Tomato Novel. How to write a novel in the Tomato Free Novel app. Share the tutorial on how to write a novel in Tomato Novel. Mar 28, 2024 pm 12:50 PM

Tomato Novel is a very popular novel reading software. We often have new novels and comics to read in Tomato Novel. Every novel and comic is very interesting. Many friends also want to write novels. Earn pocket money and edit the content of the novel you want to write into text. So how do we write the novel in it? My friends don’t know, so let’s go to this site together. Let’s take some time to look at an introduction to how to write a novel. Share the Tomato novel tutorial on how to write a novel. 1. First open the Tomato free novel app on your mobile phone and click on Personal Center - Writer Center. 2. Jump to the Tomato Writer Assistant page - click on Create a new book at the end of the novel.

How to enter bios on Colorful motherboard? Teach you two methods How to enter bios on Colorful motherboard? Teach you two methods Mar 13, 2024 pm 06:01 PM

Colorful motherboards enjoy high popularity and market share in the Chinese domestic market, but some users of Colorful motherboards still don’t know how to enter the bios for settings? In response to this situation, the editor has specially brought you two methods to enter the colorful motherboard bios. Come and try it! Method 1: Use the U disk startup shortcut key to directly enter the U disk installation system. The shortcut key for the Colorful motherboard to start the U disk with one click is ESC or F11. First, use Black Shark Installation Master to create a Black Shark U disk boot disk, and then turn on the computer. When you see the startup screen, continuously press the ESC or F11 key on the keyboard to enter a window for sequential selection of startup items. Move the cursor to the place where "USB" is displayed, and then

How to recover deleted contacts on WeChat (simple tutorial tells you how to recover deleted contacts) How to recover deleted contacts on WeChat (simple tutorial tells you how to recover deleted contacts) May 01, 2024 pm 12:01 PM

Unfortunately, people often delete certain contacts accidentally for some reasons. WeChat is a widely used social software. To help users solve this problem, this article will introduce how to retrieve deleted contacts in a simple way. 1. Understand the WeChat contact deletion mechanism. This provides us with the possibility to retrieve deleted contacts. The contact deletion mechanism in WeChat removes them from the address book, but does not delete them completely. 2. Use WeChat’s built-in “Contact Book Recovery” function. WeChat provides “Contact Book Recovery” to save time and energy. Users can quickly retrieve previously deleted contacts through this function. 3. Enter the WeChat settings page and click the lower right corner, open the WeChat application "Me" and click the settings icon in the upper right corner to enter the settings page.

How to run thinkphp project How to run thinkphp project Apr 09, 2024 pm 05:33 PM

To run the ThinkPHP project, you need to: install Composer; use Composer to create the project; enter the project directory and execute php bin/console serve; visit http://localhost:8000 to view the welcome page.

The secret of hatching mobile dragon eggs is revealed (step by step to teach you how to successfully hatch mobile dragon eggs) The secret of hatching mobile dragon eggs is revealed (step by step to teach you how to successfully hatch mobile dragon eggs) May 04, 2024 pm 06:01 PM

Mobile games have become an integral part of people's lives with the development of technology. It has attracted the attention of many players with its cute dragon egg image and interesting hatching process, and one of the games that has attracted much attention is the mobile version of Dragon Egg. To help players better cultivate and grow their own dragons in the game, this article will introduce to you how to hatch dragon eggs in the mobile version. 1. Choose the appropriate type of dragon egg. Players need to carefully choose the type of dragon egg that they like and suit themselves, based on the different types of dragon egg attributes and abilities provided in the game. 2. Upgrade the level of the incubation machine. Players need to improve the level of the incubation machine by completing tasks and collecting props. The level of the incubation machine determines the hatching speed and hatching success rate. 3. Collect the resources required for hatching. Players need to be in the game

Quickly master: How to open two WeChat accounts on Huawei mobile phones revealed! Quickly master: How to open two WeChat accounts on Huawei mobile phones revealed! Mar 23, 2024 am 10:42 AM

In today's society, mobile phones have become an indispensable part of our lives. As an important tool for our daily communication, work, and life, WeChat is often used. However, it may be necessary to separate two WeChat accounts when handling different transactions, which requires the mobile phone to support logging in to two WeChat accounts at the same time. As a well-known domestic brand, Huawei mobile phones are used by many people. So what is the method to open two WeChat accounts on Huawei mobile phones? Let’s reveal the secret of this method. First of all, you need to use two WeChat accounts at the same time on your Huawei mobile phone. The easiest way is to

There are several versions of thinkphp There are several versions of thinkphp Apr 09, 2024 pm 06:09 PM

ThinkPHP has multiple versions designed for different PHP versions. Major versions include 3.2, 5.0, 5.1, and 6.0, while minor versions are used to fix bugs and provide new features. The latest stable version is ThinkPHP 6.0.16. When choosing a version, consider the PHP version, feature requirements, and community support. It is recommended to use the latest stable version for best performance and support.

How to set font size on mobile phone (easily adjust font size on mobile phone) How to set font size on mobile phone (easily adjust font size on mobile phone) May 07, 2024 pm 03:34 PM

Setting font size has become an important personalization requirement as mobile phones become an important tool in people's daily lives. In order to meet the needs of different users, this article will introduce how to improve the mobile phone use experience and adjust the font size of the mobile phone through simple operations. Why do you need to adjust the font size of your mobile phone - Adjusting the font size can make the text clearer and easier to read - Suitable for the reading needs of users of different ages - Convenient for users with poor vision to use the font size setting function of the mobile phone system - How to enter the system settings interface - In Find and enter the "Display" option in the settings interface - find the "Font Size" option and adjust it. Adjust the font size with a third-party application - download and install an application that supports font size adjustment - open the application and enter the relevant settings interface - according to the individual

See all articles