PHP Token(令牌)設計

轉載鏈接:http://www.jb51.net/article/13756.htm


PHP Token(令牌)設計 設計目標: 避免重復提交數據. 檢查來路,是否是外部提交 匹配要執行的動作(如果有多個邏輯在同一個頁面實現,比如新增,刪除,修改放到一個PHP文件里操作) 這里所說的token是在頁面顯示的時候,寫到FORM的一個隱藏表單項(type=hidden). token不可明文,如果是明文,那就太危險了,所以要采用一定的加密方式.密文要可逆.俺算法很白癡,所以采用了網上一個現成的方法.

怎樣避免重復提交?
在SESSION里要存一個數組,這個數組存放以經成功提交的token.在后臺處理時,先判斷這個token是否在這個數組里,如果存在,說明是重復提交.?
如何檢查來路?
可選項,這個token在生成的時候,加入了當前的session_id.如果別人copy你的html(token一迸copy),在提交時,理論上token里包含的session_id不等于當前session_id,就可以判斷這次提交是外部提交.?
如何匹配要執行的動作?
在token的時候,要把這個token的動作名稱寫進這個token里,這樣,在處理的時候,把這個動作解出來進行比較就行了.

GEncrypt.inc.php:

<?php  
class GEncrypt extends GSuperclass {  protected static function keyED($txt,$encrypt_key){     $encrypt_key = md5($encrypt_key);     $ctr=0;     $tmp = "";     for ($i=0;$i<strlen($txt);$i++){     if ($ctr==strlen($encrypt_key)) $ctr=0;     $tmp.= substr($txt,$i,1) ^ substr($encrypt_key,$ctr,1);     $ctr++;     }     return $tmp;     }  public static function encrypt($txt,$key){     //$encrypt_key = md5(rand(0,32000));  $encrypt_key = md5(((float) date("YmdHis") + rand(10000000000000000,99999999999999999)).rand(100000,999999));  $ctr=0;     $tmp = "";     for ($i=0;$i<strlen($txt);$i++){  if ($ctr==strlen($encrypt_key)) $ctr=0;     $tmp.= substr($encrypt_key,$ctr,1) . (substr($txt,$i,1) ^ substr($encrypt_key,$ctr,1));     $ctr++;     }     return base64_encode(self::keyED($tmp,$key));  }  public static function decrypt($txt,$key){  $txt = self::keyED( base64_decode($txt),$key);     $tmp = "";  for ($i=0;$i<strlen($txt);$i++){     $md5 = substr($txt,$i,1);     $i++;     $tmp.= (substr($txt,$i,1) ^ $md5);     }  return $tmp;  }      
}  
?> 
方法:

(1)granteToken?參數:formName,即動作名稱,key是加密/解密?密鑰.
返回一個字符串,形式是:?加密(formName:session_id)
(2)isToken?參數:token?即granteToken產生的結果,formName,動作名稱,fromCheck是否檢查來路,如果為真,還要判斷token里的session_id是否和當前的session_id一至.
(3)dropToken,當成功執行一個動作后,調用這個函數,把這個token記入session里,

GToken.inc.php

<?php  
/**  
* 原理:請求分配token的時候,想辦法分配一個唯一的token, base64( time + rand + action)  
* 如果提交,將這個token記錄,說明這個token以經使用,可以跟據它來避免重復提交。  
*  
*/  
class GToken {  /**  * 得到當前所有的token  *  * @return array  */  public static function getTokens(){  $tokens = $_SESSION[GConfig::SESSION_KEY_TOKEN ];  if (empty($tokens) && !is_array($tokens)) {  $tokens = array();  }  return $tokens;  }  /**  * 產生一個新的Token  *  * @param string $formName  * @param 加密密鑰 $key  * @return string  */  public static function granteToken($formName,$key = GConfig::ENCRYPT_KEY ){  $token = GEncrypt::encrypt($formName.":".session_id(),$key);  return $token;  }  /**  * 刪除token,實際是向session 的一個數組里加入一個元素,說明這個token以經使用過,以避免數據重復提交。  *  * @param string $token  */  public static function dropToken($token){  $tokens = self::getTokens();  $tokens[] = $token;  GSession::set(GConfig::SESSION_KEY_TOKEN ,$tokens);  }  /**  * 檢查是否為指定的Token  *  * @param string $token    要檢查的token值  * @param string $formName  * @param boolean $fromCheck 是否檢查來路,如果為true,會判斷token中附加的session_id是否和當前session_id一至.  * @param string $key 加密密鑰  * @return boolean  */  public static function isToken($token,$formName,$fromCheck = false,$key = GConfig::ENCRYPT_KEY){  $tokens = self::getTokens();  if (in_array($token,$tokens)) //如果存在,說明是以使用過的token  return false;  $source = split(":", GEncrypt::decrypt($token,$key));  if($fromCheck)  return $source[1] == session_id() && $source[0] == $formName;  else  return $source[0] == $formName;  }  
}  
?> 

從$_POST里取出token,用isToken判斷.

如果想判斷是否是執行的匹配動作,可以把isToken里的formName改一下,運行,很好,沒有匹配上.證明這個成功.?

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/276101.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/276101.shtml
英文地址,請注明出處:http://en.pswp.cn/news/276101.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

java rwd_面向任務的設計-不僅限于Mobile First和RWD

java rwdWe already know that majority of solutions should start with a design for smartphones, we know that all websites should be responsive. Now, it’s time to think about holistic solutions with specific tasks adapted to all kind of devices.我們已經知道…

python中關鍵字 表示空類型_python中什么表示空類型

python中什么表示空類型&#xff1f;python中None表示空類型。表示該值是一個空對象&#xff0c;空值是Python里一個特殊的值&#xff0c;用None表示。None不能理解為0&#xff0c;因為0是有意義的&#xff0c;而None是一個特殊的空值。可以將None賦值給任何變量&#xff0c;也…

HOJ 1015 Nearly prime numbers

代碼 //Nearly prime number is an integer positive number for which it is possible //to find such primes P1 and P2 that given number is equal to P1*P2.#include <stdio.h>#include <stdlib.h>#include <math.h>//decide n whither is a nearly pri…

「前端工程化」該怎么理解?

大家好&#xff0c;我是若川。今天分享一篇「前端工程化」的好文。非廣告&#xff0c;請放心閱讀。可點擊下方卡片關注我&#xff0c;或者查看系列文章。今天發文比較晚&#xff0c;以往都是定時早上7:30發文&#xff0c;也不知道是不是有點早。一.什么是前端工程&#xff1f;一…

axure文本框值相加_Axure教程:計數文本域實現

原標題&#xff1a;Axure教程&#xff1a;計數文本域實現制定UI規范時&#xff0c;遇到實現“限制字數的文本域”的交互問題&#xff0c;即當用戶輸入的字數長度超過限制要求&#xff0c;如何只保留規定長度的文本&#xff1f;效果如下&#xff1a;我們知道【Number】類型的文本…

figma下載_Figma和ProtoPie中的原型制作,比較

figma下載第1部分 (Part 1) Prototyping has never had such a high profile with a whole host of tools that now give you varying ability to realize your designs beyond their static UI and into a working usable thing. It’s fair to say that prototyping within t…

拗口翻譯

I find many times people use temporary tables because they learned in other databases that joining too many tables in a single query is a ?bad thing?. This is a practice that must be unlearned for Oracle development. Rather then trying to out‐smart the …

javascript 手機手勢動作touch觸屏原理分析

轉載鏈接&#xff1a;http://www.lvtao.net/web/220.html $(function(){document.getElementById("moveId").addEventListener(touchstart, touchStart);document.getElementById("moveId").addEventListener(touchmove, touchMove);document.getElementBy…

并發工具類(四)線程間的交換數據 Exchanger

前言JDK中為了處理線程之間的同步問題&#xff0c;除了提供鎖機制之外&#xff0c;還提供了幾個非常有用的并發工具類&#xff1a;CountDownLatch、CyclicBarrier、Semphore、Exchanger、Phaser&#xff1b;??CountDownLatch、CyclicBarrier、Semphore、Phaser 這四個工具類提…

「前端組件化」該怎么理解?

大家好&#xff0c;我是若川。今天分享一篇關于「前端組件化」的好文。歡迎點擊下方卡片關注我。以下是正文~這里我們一起來學習前端組件化的知識&#xff0c;而組件化在前端架構里面是最重要的一個部分。講到前端架構&#xff0c;其實前端架構中最熱門的就有兩個話題&#xff…

大屏設計的視覺統一_視覺設計中的統一

大屏設計的視覺統一視覺設計的統一性是什么&#xff1f; (What is unity in visual design?) The concept of unity in visual design means a group of elements working together to create a greater whole. It means, as the clich goes: A whole that is greater than th…

l2范數求導_機器學習中的范數規則化之(一)L0、L1與L2范數

source: https://blog.csdn.net/zouxy09/article/details/24971995zouxy09qq.comhttp://blog.csdn.net/zouxy09今天我們聊聊機器學習中出現的非常頻繁的問題&#xff1a;過擬合與規則化。我們先簡單的來理解下常用的L0、L1、L2和核范數規則化。最后聊下規則化項參數的選擇問題。…

9.struts1.x中tiles框架的使用

在頁面直接使用titles標簽先引入標簽&#xff1a;<%taglib uri"http://struts.apache.org/tags-tiles" prefix"tiles" %> 將模板頁面要代替的內容用標簽占位&#xff1a;<tiles:insert attribute"content"></tiles:insert> 在…

Debian 9.6.0 + OpenMediaVault 4.x : U盤作系統盤時遇到的問題

前幾天在虛擬機試驗的時候還說裝到實機一般也沒什么問題&#xff0c;然后突然間想試試如果把 Debian9OMV 都放到U盤里會怎么樣。于是就折騰&#xff08;然后懵逼&#xff09; 先總結一下 寫入openmediavault官方的iso到U盤使用UNetbootin寫入Debian9的iso使用UltraISO的默認設置…

新浪微博、騰訊微博、QQ空間、人人網、豆瓣 一鍵分享API

轉載鏈接&#xff1a;http://www.bluesdream.com/blog/sina-tencent-renren-douban-share-a-key-api.html 新浪微博&#xff1a; http://service.weibo.com/share/share.php?url count表示是否顯示當前頁面被分享數量(1顯示)(可選&#xff0c;允許為空) &url將頁面地址轉…

跟著官方文檔能學懂React Hooks就怪了

大家好&#xff0c;我是若川。今天分享一篇關于「React Hooks」的好文。歡迎點擊下方卡片關注我。以下是正文~回想下你入門Hooks的過程&#xff0c;是不是經歷過&#xff1a;類比ClassComponent的生命周期&#xff0c;學習Hooks的執行時機慢慢熟練以后&#xff0c;發現Hooks的執…

origin圖上顯示數據標簽_Origin(Pro):寒假都結束了,這個圖還是不會畫?【數據繪圖】...

寒假前給大家分享了一個圖&#xff0c;大家要的教程來了。【數據繪圖】好圖分享&#xff1a;寒假&#xff1f;不存在的&#xff01;?mp.weixin.qq.com繪圖思路&#xff1a;左側起止時間&#xff1a;散點圖&#xff0c;交換XY坐標軸&#xff1b;中間的連線為Drop Lines&#xf…

可以激發設計靈感的音樂_建立靈感庫以激發您的創造力

可以激發設計靈感的音樂I often find a lot of inspiration from work I see while scrolling social media. Saving art or images that inspire you allows you to build a library of resources to draw from whenever you’re working on a project.在滾動社交媒體時&#…

CentOS服務器上部署 oracle10gr2

1、下載Centos系統 Linux 鏡像文件。 推薦使用 CentOS5.4&#xff0c;下載地址&#xff1a;http://isoredirect.centos.org/centos/5/isos/i386/ 。這個是 32 位的 Linux 系統鏡像安裝文件&#xff0c;進入下載頁面后&#xff0c;如果是 DVD 光盤安裝&#xff0c;可以僅…

回顧:中網通訊網絡公司CEO羅與曾作客新浪嘉賓聊天室

轉載鏈接&#xff1a;http://tech.sina.com.cn/it/w/2001-11-09/91253.shtml 回顧&#xff1a;中網通訊網絡公司CEO羅與曾作客新浪嘉賓聊天室 大家好&#xff01;   主持人 &#xff1a;各位網友&#xff0c;下午好&#xff0c;今天我們請到了中網通訊網絡公司首席執行官羅…