PHP 의 Token 응용

2945 단어 token영패
더 읽 기
gen_token();
 * 2.    Action    Token,             ,      :
 * 		$isValid = Utils_Token::getInstance()->validate($token_value);
 * 3.            URL :
 * 		Utils_Token::getInstance()->validate($token_value,'http://xx.xxx.com')
 * 
 * @package Utils
 * @author    
 * @version 1.0
 * @example
 * 		$token_value = Utils_Token::getInstance()->gen_token(); //  Token
 *		echo 'TOKEN_VALUE:'.$token_value.'
'; // Token * $checkValue = Utils_Token::getInstance()->validate($token_value);// * echo $checkValue;// :1- 0- * */ class Utils_Token { const TOKEN_NAME = 'TOKEN'; // private static $instance; // private function __construct() { // } /** * * * @access public * @return Token Utils_Token */ public static function getInstance() { if (! isset ( $_SESSION )) session_start (); if (! isset ( self::$instance )) return new Utils_Token (); else return self::$instance; } /** * , * * @access public * @return string */ public function gen_token() { $hash = md5 ( uniqid ( rand () . 'WXT', true ) ); $n = rand ( 0, 24 ); $token = substr ( $hash, $n, 8 ); $this->_tokenValue = $token; $this->destroy_token (); $_SESSION [self::TOKEN_NAME] = $this->_tokenValue; return $token; } /** * * * @access private * @return void */ private function destroy_token() { if ($_SESSION [self::TOKEN_NAME]) unset ( $_SESSION [self::TOKEN_NAME] ); } /** * * * @access public * @param string $token * * @param string $url * URL * @return boolean 1- 0- * */ public function validate($token, $url = '') { if ($token == $_SESSION [self::TOKEN_NAME]) { $this->destroy_token (); return 1; } elseif ($url) { $this->destroy_token (); Header ( "Location: $url" ); } else { $this->destroy_token (); return 0; } } } /* $token_value = Utils_Token::getInstance()->gen_token(); // Token echo 'TOKEN_VALUE:'.$token_value.'
'; // Token $checkValue = Utils_Token::getInstance()->validate($token_value);// echo $checkValue;// :1- 0- */

좋은 웹페이지 즐겨찾기