日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > HTML >内容正文

HTML

token在浏览器和服务端接收和发送

發布時間:2024/9/30 HTML 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 token在浏览器和服务端接收和发送 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1.使用header

客戶端?

index.html

<html> <head><script src="http://libs.baidu.com/jquery/1.10.2/jquery.min.js"></script> <style>.oc{width: 200px;height: 100px;position: center;background: aqua;border :1px solid black;} </style> </head> <body> <div class="oc"></div> <script>$(".oc").click(function () {$.ajax({url: './index.php',data: {'name':'user'},beforeSend: function (request) {request.setRequestHeader("token","111111");},method:'POST',success:function (data,status,request) {alert(data);var re=request.getResponseHeader("Authorization"); //返回返回的 header 對應的字段console.log("auth:",re)}});}); </script> </body> </html>

?

服務器端

index.php

<?php//生成tokenfunction generateToken(){// 32個字符組成一組隨機字符串$randChars = rand(1,10);//getRandChars(32);//用三組字符串,進行md5加密$timestamp = $_SERVER['REQUEST_TIME_FLOAT'];$salt = 'salt';//config('secure.token_salt');return md5($randChars.$timestamp.$salt); } $token = $_SERVER['HTTP_TOKEN']; //獲取header里面token的信息$service_token = generateToken(); //可以存到redis中,設置過期時間if ($token == $service_token) {$data = ['code'=>1,'message'=>'success','data'=>'index',]; }else{header("Content-Type: text/html;charset=utf-8");header("Authorization: ".$service_token);//把token放在設置 header里面的Authorization發送費客戶端$data = ['code'=>1,'message'=>'fail','data'=>[],]; } exit(json_encode($data));

效果如下所示:

?

2.服務器使用echo返回客戶端,客戶端post方式發送token到服務器端也行

class Token {public function getToken($code = ''){//return 'success';(new TokenGet())->goCheck();$ut = new UserToken($code);$token = $ut->get();return ['token'=>$token];}/** 第三方應用獲取令牌*/public function getAppToken($ac='',$se=''){(new AppTokenGet())->goCheck();$app = new AppToken();$token = $app->get($ac,$se);return ['token'=>$token];}public function verifyToken($token=''){if(!$token){throw new ParameterException(['token不允許為空']);}$valid = TokenService::verifyToken($token);return['isValid'=>$valid];}}

?

tokenservice

class Token {public static function generateToken(){// 32個字符組成一組隨機字符串$randChars = getRandChars(32);//用三組字符串,進行md5加密$timestamp = $_SERVER['REQUEST_TIME_FLOAT'];$salt = config('secure.token_salt');return md5($randChars.$timestamp.$salt);}public static function getCurrentTokenVar($key){$token = Request::instance()->header('token');$vars = Cache::get($token);if(!$vars){throw new TokenException();}else{if(!is_array($vars)){$vars = json_decode($vars,true);}if(array_key_exists($key,$vars)){return $vars[$key];}else{throw new Exception('嘗試獲取的Token變量不存在');}}}public static function verifyToken($token){$exits = Cache::get('token');if($exits){return true;}else{return false;}}}

?

總結

以上是生活随笔為你收集整理的token在浏览器和服务端接收和发送的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。