BVB Source Codes

The latest thinkphp v5.2 work order system Show TokenBuildBehavior.class.php Source code

Return Download The latest thinkphp v5.2 work order system: download TokenBuildBehavior.class.php Source code - Download The latest thinkphp v5.2 work order system Source code - Type:.php
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | TOPThink [ WE CAN DO IT JUST THINK ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2010 http://topthink.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: liu21st <liu21st@gmail.com>
  10. // +----------------------------------------------------------------------
  11. namespace Behavior;
  12. /**
  13.  * 系统行为扩展:表单令牌生成
  14.  */
  15. class TokenBuildBehavior {
  16.  
  17.     public function run(&$content){
  18.         if(C('TOKEN_ON')) {
  19.             list($tokenName,$tokenKey,$tokenValue)=$this->getToken();
  20.             $input_token = '<input type="hidden" name="'.$tokenName.'" value="'.$tokenKey.'_'.$tokenValue.'" />';
  21.             $meta_token = '<meta name="'.$tokenName.'" content="'.$tokenKey.'_'.$tokenValue.'" />';
  22.             if(strpos($content,'{__TOKEN__}')) {
  23.                 // 指定表单令牌隐藏域位置
  24.                 $content = str_replace('{__TOKEN__}',$input_token,$content);
  25.             }elseif(preg_match('/<\/form(\s*)>/is',$content,$match)) {
  26.                 // 智能生成表单令牌隐藏域
  27.                 $content = str_replace($match[0],$input_token.$match[0],$content);
  28.             }
  29.             $content = str_ireplace('</head>',$meta_token.'</head>',$content);
  30.         }else{
  31.             $content = str_replace('{__TOKEN__}','',$content);
  32.         }
  33.     }
  34.  
  35.     //获得token
  36.     private function getToken(){
  37.         $tokenName  = C('TOKEN_NAME',null,'__hash__');
  38.         $tokenType  = C('TOKEN_TYPE',null,'md5');
  39.         if(!isset($_SESSION[$tokenName])) {
  40.             $_SESSION[$tokenName]  = array();
  41.         }
  42.         // 标识当前页面唯一性
  43.         $tokenKey   =  md5($_SERVER['REQUEST_URI']);
  44.         if(isset($_SESSION[$tokenName][$tokenKey])) {// 相同页面不重复生成session
  45.             $tokenValue = $_SESSION[$tokenName][$tokenKey];
  46.         }else{
  47.             $tokenValue = is_callable($tokenType) ? $tokenType(microtime(true)) : md5(microtime(true));            
  48.             $_SESSION[$tokenName][$tokenKey]   =  $tokenValue;
  49.             if(IS_AJAX && C('TOKEN_RESET',null,true))
  50.                 header($tokenName.': '.$tokenKey.'_'.$tokenValue); //ajax需要获得这个header并替换页面中meta中的token值
  51.         }
  52.         return array($tokenName,$tokenKey,$tokenValue);
  53.     }
  54. }
downloadTokenBuildBehavior.class.php Source code - Download The latest thinkphp v5.2 work order system Source code
Related Source Codes/Software:
V3.3.6 Workerman high-performance PHP Socket (framework) - High performance PHP Socket framework Workerman re... 2017-05-03
The phosphor meeting room reservation system MRBS (loop reservation v16.0) - The phosphor meeting room reservation system MRBS ... 2017-05-03
IDC v5.2.3 agent center - IDC Centre IDC for exploitation is a PHP+MySQL age... 2017-05-03
Zen master v9.0.1 project management software - Zen is the first domestic open source project mana... 2017-05-03
Ray speed v7.03 employee files management system - Employee files project is various, and some conten... 2017-05-03
HITCMS Haitian responsive electronic mechanical v3.0 foreign trade website source code - HITCMS Haitian response electronic machinery trade... 2017-05-03
Ray v7.02 speed survey system - Lightning speed survey system is a. NET+MSSQL deve... 2017-05-03
Cool HITCMS reactive black v3.0 enterprise website source code - HITCMS-Haitian response black enterprises website ... 2017-05-03
WebOA v17.2 network office automation system - WebOA network using b/s structure development of o... 2017-05-03
Pure imitation of han site source code, one a 】 【 c9cms kernel v1.0 - Generic http://www.wufazhuce.com/Han one · Config... 2017-05-04
zone.js - Implements Zones for JavaScript ... 2017-05-11
dragonfly - A Ruby gem for on-the-fly processing - suitable fo... 2017-05-11
oh-my-fish - The Fishshell Framework. 2017-05-11
canvas-lms - The open LMS by Instructure, Inc. 2017-05-11
ledger - Double-entry accounting system with a command-line... 2017-05-11
Python - My Python Examples http://w... 2017-05-11
blade - 2017-05-11
LaZagne - Credentials recovery project 2017-05-10
Pull-to-Refresh.Rentals-iOS - This project aims to provide a simple and customiz... 2017-05-10
spoon - Distributing instrumentation tests to all your And... 2017-05-10

 Back to top