API 浜や簰涓€庝箞鍋氬ソ鍥剧墖楠岃瘉鐮侊紵
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了API 浜や簰涓€庝箞鍋氬ソ鍥剧墖楠岃瘉鐮侊紵相关的知识,希望对你有一定的参考价值。
鏍囩锛?a href='http://www.mamicode.com/so/1/server' title='server'>server
鏈嶅姟 閫掑綊 鎼滅储 璇锋眰 show function set comment鍓嶈█
鍦ㄤ紶缁熺殑 Web 寮€鍙戣繃绋嬩腑锛屽鐞嗗浘褰㈤獙璇佺爜寰堢畝鍗曪紝鍙渶瑕佸湪鍚庡彴鐢ㄩ殢鏈哄瓧绗︿覆鐢熸垚涓€涓浘鐗囷紝灏嗛獙璇佺爜鍐呭鏀捐繘 Session 鍗冲彲锛岀敤鎴锋彁浜よ〃鍗曟椂浠?Session[1] 鍙栧嚭鍒ゆ柇鍗冲彲銆?/p>
浣嗘槸鐜板浠婏紝瓒婃潵瓒婃帹宕?API 浜や簰锛屾棤鐘舵€侊紝鍦?Session 杩欎竴鍧楋紝铏界劧榛樿閰嶇疆鏄笉鏀寔浜嗭紝浣嗘槸杩樻槸鏈夊緢澶氭洸绾挎晳鍥界殑鏂规硶銆?/p>
鍩轰簬 Session 瀹炵幇
鍦?API 寮€鍙戜腑锛屾垜浠篃鍙互缁欏墠绔鍙?SessionID 锛屽苟涓旈€氳繃 php 鐨勫唴缃柟娉曪紝鏉ュ疄鐜拌繖涓€鍒囥€?br>姣斿 鎴戜滑涓庡墠娈电害瀹氾紝褰撳湪璇锋眰涓寘鍚湁 X-Session-Id 锛屼笖涓嶄负绌烘椂锛岃〃绀鸿繖涓細璇濆凡缁忔敞鍐岃繃 SessionID 锛屽惁鍒欏氨棰佸竷涓€涓?SessionID 骞惰繑鍥炲湪 Response Header 涓殑 X-Session-Id 璁╁墠娈佃褰曡繖涓?SessionID 锛屼笅闈㈢畝鍗曞疄鐜颁竴涓嬨€?/p>
// code_session.php
session_start();
// 杩欓噷鍋囪宸茬粡閫氳繃 Header 鑾峰彇鍒颁簡 SessionID锛屽苟淇濆瓨鍒颁簡 $sessionId 鍙橀噺涓€?// 褰?SessionID 涓嶅瓨鍦紝鎴栬€?涓虹┖ 鍒欏垱寤烘柊鐨?SessionID 銆?if(!isset($sessionId) || empty($sessionId)){
$sessionId = session_create_id();
// 鍥犱负鍓嶅彴杩樻病鏈?SessionID 锛屾墍浠ヤ笅鍙戜竴涓紝閫氱煡鍓嶇淇濆瓨銆? header(鈥榅-Session-Id: 鈥?$sessionId);
}
// 璁剧疆褰撳墠浼氳瘽鐨?SessionID 銆?session_id($sessionId);
// 杩欓噷鎴戜滑灏卞彲浠ヨ嚜鐢辩殑璇诲啓 Session 浜嗐€?// 鐢熸垚楠岃瘉鐮?$code = mt_rand(1e3 ,1e4-1);
// create_image 璇疯嚜琛屽疄鐜?鎴栬€呬娇鐢ㄧ幇鏈夌殑鍥惧舰楠岃瘉鐮佸簱鐢熸垚銆?$image = create_image($code);
// 瀛樺偍杩涘幓 Session
$_SESSION[鈥榗ode鈥榏 = $code;
// 杈撳嚭涓€寮犲浘鐗?$image->output();
涓婇潰鍩烘湰瀹炵幇浜嗙敓鎴愬浘鐗囷紝鍓嶇闇€瑕佹牴鎹?鍙渶瑕佸啀鎻愪氦琛ㄥ崟鏃讹紝鍦?headers 涓甫涓?nbsp;X-Session-ID 鍗冲彲銆?/p>
// code_session_validate.php
session_start();
// 杩欓噷鍋囪宸茬粡閫氳繃 Header 鑾峰彇鍒颁簡 SessionID锛屽苟淇濆瓨鍒颁簡 $sessionId 鍙橀噺涓€?// 褰?SessionID 涓嶅瓨鍦紝鎴栬€?涓虹┖ 鍒欏垱寤烘柊鐨?SessionID 銆?if(
!isset($sessionId)
|| empty($sessionId)
|| !isset($_POST[鈥榗ode鈥榏)
|| empty($_POST[鈥榗ode鈥榏)
){
// 鍥犱负娌℃湁鎻愪氦 SessionID 杩囨潵 杩欎釜鑲畾灏辨槸涓嶆垚绔嬬殑浜嗭紝鎵€浠ョ洿鎺ョ粓姝㈠嵆鍙€? exit;
}
// 璁剧疆褰撳墠浼氳瘽鐨?SessionID 銆?session_id($sessionId);
if($_POST[鈥榗ode鈥榏!=$_SESSION[鈥榗ode鈥榏){
// 楠岃瘉鐮侀敊璇暒
exit;
}
// 楠岃瘉閫氳繃浜嗗氨鍒犳帀 code锛?unset($_SESSION[鈥榗ode鈥榏);
涓婇潰浣跨敤 Session 锛屾垜浠熀鏈氨瀹炵幇浜嗕竴涓畝鍗曠殑楠岃瘉锛岃€屼笖鏄熀浜?API 浜や簰鐨勶紝涓嶄緷璧栨祻瑙堝櫒 cookie 銆傚綋鎴戜滑闇€瑕佷竴浜涘鏉傜殑姣斿鍏变韩 Session 锛岃繖浜涘氨涓嶅湪鏈枃鐨勮璁鸿寖鍥翠簡(鍏跺疄鐜板湪涔熷凡缁忚秴绾蹭簡)
鍩轰簬瀹㈡埛绔富鍔ㄧ鍙?/h2>
鎺ヤ笅鏉ョ殑鏂规硶鏄棤鐘舵€佺殑锛屼絾鏄渶瑕佺敤鍒?Redis 銆傝繖閲屼娇鐢?PHPRedis 杩欎釜鎵╁睍鏉ュ鐞嗐€?/p>
鍦ㄥぇ澶氭暟鎯呭喌涓嬶紝鎴戜滑骞朵笉闇€瑕佸儚涓婇潰浣跨敤 Session 閭f牱鏉ュ垱寤鸿繃澶氱殑 Session 锛岄€犳垚鏈変竴浜涜祫婧愭氮璐癸紝褰撶劧锛孲ession 鍙互鍋氱殑涓嶆杩欎簺锛屼笅闈㈡垜浠氨鐢?Redis 鏉ュ仛涓€涓鎴风涓诲姩绛惧彂 鐨勫浘鐗囬獙璇佺爜銆?/p>
鐞嗚鍘熺悊
鐢卞鎴风鏈湴鐢熸垚闅忔満瀛楃涓诧紝鐒跺悗鎷兼帴鍦ㄨ幏鍙栭獙璇佺爜鍦板潃鐨勫悗闈紝鍚庣鎴彇瀹㈡埛绔敓鎴愮殑闅忔満瀛楃涓诧紝鐢ㄦ浣滀负楠岃瘉鍑瘉鏀惧叆 Redis 涓幓锛屽啀瀹㈡埛绔彁浜ゆ椂闇€瑕佸甫涓婂厛鍓嶇敓鎴愮殑闅忔満瀛楃涓蹭竴鍚岃繘椤归獙璇併€?/p>
// code_client.php
$salt = 鈥榳ertyujkdbaskndasda鈥?
if(!isset($_GET[鈥榮ign鈥榏)){
// 瀹㈡埛绔病鏈夋彁渚涚鍚嶏紝鍋滄鎵ц
exit;
}
// 鐢ㄦ埛浼犳潵鐨勪竴鍒?span class="hljs-comment">鏁版嵁閮芥槸涓嶅彲闈犵殑锛屾垜浠渶瑕佸鍏跺姞鐩愬悗鎵ц md5
$sign = md5($_GET[鈥榮ign鈥榏.$salt);
// 鎷兼帴涓婄鍚嶄綔涓?Redis 鐨?key
$key = 鈥榗ode:鈥?$sign;
// 杩炴帴 Redis
$cache = new Redis();
// 鐢熸垚楠岃瘉鐮?$code = mt_rand(1e3,1e4-1);
// 淇濆瓨楠岃瘉鐮佸埌 Redis 骞惰缃?鍒嗛挓鐨勬湁鏁堟湡銆?if($cache->exists($key)){
// 杩欎釜 Key 宸茬粡琚崰鐢ㄤ簡锛岃繖閲屽厛鍋滄銆? exit;
}
$cache->set($key,$code,60*2);
// 鍒涘缓鍥剧墖骞惰繑鍥?$image = create_image($code);
$image->output();
濂戒簡锛屾帴涓嬫潵楠岃瘉涓€涓嬨€?/p>
// code_client_validate.php
$salt = 鈥榳ertyujkdbaskndasda鈥?
if(
!isset($_POST[鈥榮ign鈥榏)
|| !isset($_POST[鈥榗ode鈥榏) // 娌℃湁鎻愪氦楠岃瘉鐮佽繃鏉ャ€?|| !empty($_POST[鈥榗ode鈥榏)
){
// 瀹㈡埛绔病鏈夋彁渚涚鍚嶏紝鍋滄鎵ц
exit;
}
// 鐢ㄦ埛浼犳潵鐨勪竴鍒?span class="hljs-comment">鏁版嵁閮芥槸涓嶅彲闈犵殑锛屾垜浠渶瑕佸鍏跺姞鐩愬悗鎵ц md5
$sign = md5($_POST[鈥榮ign鈥榏.$salt);
// 鎷兼帴涓婄鍚嶄綔涓?Redis 鐨?key
$key = 鈥榗ode:鈥?$sign;
// 杩炴帴 Redis
$cache = new Redis();
if(!$cache->exists($key)){
// 鏍规湰娌℃湁杩欎釜 key
eixt;
}
if($cache->get($key)!=$_POST[鈥榗ode鈥榏){
// 楠岃瘉鐮侀敊璇?}
// 楠岃瘉閫氳繃浜嗗氨鍒犻櫎
$cache->del($key);
鐪嬬潃鏄笉鏄澶嶆潅鐐瑰効锛岀敋鑷宠繕鐢ㄤ笂浜?Redis 锛岃櫧鐒剁湅鐫€涓嶅拫鍦帮紝浣嗘槸浠栦篃瀹炵幇浜嗘垜浠兂瑕佺殑锛屼笉杩囪繖涓篃涓嶇畻鏄お濂界殑鏂规锛岃€屼笖锛岃繕瑕佽€冭檻瀹㈡埛绔瓧绗︿覆涓嶅闅忔満鐨勬儏鍐碉紝鎺ヤ笅鏉ユ垜浠敼鍙樹竴涓嬫柟鍚戯紝鎹㈡垚鏈嶅姟绔鍙戙€?/p>
鍩轰簬鏈嶅姟绔鍙?/h2>
鍒氬垰鐨勬槸鍩轰簬瀹㈡埛绔鍙戠殑瀹炵幇锛屼笅闈㈡潵鎻愪緵鍙︿竴绉嶆€濊矾锛屼絾鏄ぇ浣撲笂锛岃繖涓槸宸笉澶氱殑鍝堥兘銆?/p>
鐞嗚鍘熺悊
鍚屾牱鏄鍙?Sign 锛屽彧涓嶈繃杩欐鐢辨湇鍔$鏉ョ鍙戯紝鐒跺悗灏?Sign 閫氳繃 Header 鍙戦€佺粰瀹㈡埛绔紝瀹㈡埛绔渶瑕佸厛鍙栧埌鍥剧墖璧勬簮锛屾敞鎰忚繖閲岃繑鍥炵殑搴旇鏄竴涓悎娉曠殑浜岃繘鍒舵祦锛岀劧鍚庝粠 header 涓彇鍑?Sign 锛屽悓鏃跺睍绀虹粰鐢ㄦ埛銆?/p>
// code_server.php
$cache = new Redis();
$salt = 鈥榳ertyujkdbaskndasda鈥?
function generateSign(){
global $cache,$salt;
$sign = md5(mt_rand().$salt);
// 鎷兼帴涓婄鍚嶄綔涓?Redis 鐨?key
$key = 鈥榗ode:鈥?$sign;
if($cache->exists($key)){
// 鏄殑 浣犱箞鏈夌湅閿欙紝灏辨槸濡傛灉鐢熸垚鐨?Sign 宸插瓨鍦紝灏辫繘琛岄€掑綊锛岀洿鍒扮敓鎴愬嚭涓€涓笉瀛樺湪鐨勩€? return generateSign();
}
return $key;
}
// 杩炴帴 Redis
$key = generateSign();
// 鐢熸垚楠岃瘉鐮?$code = mt_rand(1e3,1e4-1);
// 淇濆瓨楠岃瘉鐮佸埌 Redis 骞惰缃?鍒嗛挓鐨勬湁鏁堟湡銆?$cache->set($key,$code,60*2);
// 鍒涘缓鍥剧墖骞惰繑鍥?$image = create_image($code);
// 鍝堝搱 瑕佸墐鎺夊墠缂€鍝?header(鈥榅-Captcha-Sign: 鈥?. str_replace(鈥榗ode:鈥?鈥樷€?$key));
$image->output();
鐪嬭捣鏉ュ嚑涔庢病鏈夊彉鍖栵紝鍙槸鐢熸垚 Sign 鐨勬柟寮忓彉浜嗕竴涓嬶紝浣嗘槸锛岃繖鏍锋悶鐨勮瘽锛屽墠绔悓瀛﹀彲鑳藉氨涓嶇埥浜嗭紝浠栦滑瑕佸厛鑾峰彇杩欎釜璧勬簮鍜?headers 涓殑 X-Captcha-Sign 鍐?show 鍒扮晫闈笂锛屽綋鐒?鍙互鐩存帴灏嗙粨鏋?base64 鎴栬€?鐩存帴鐢ㄧ敤浜岃繘鍒舵祦鐢熸垚浣嶅浘鏄剧ず閮芥槸鍙互鐨勶紝鎴戜滑鍙槸闇€瑕佸彲浠ラ獙璇侊紝楠岃瘉鏂规硶鐩存帴浣跨敤涓婇潰鐨勫嵆鍙€?/p>
璧勬簮鎼滅储缃戠珯澶у叏 https://www.renrenfan.com.cn
鐗瑰埆娉ㄦ剰
褰撲綘浣跨敤 ajax 鑾峰彇杩欎釜璧勬簮鏄紝濡傛灉浣犵殑涓氬姟娑夊強鍒颁簡璺ㄥ煙锛屼綘杩橀渶瑕佸湪鍝嶅簲澶磋缃?nbsp;Access-Control-Expose-Headers - HTTP | MDN锛屽惁鍒?ajax 鏃犳硶鑾峰彇鑷畾涔夌殑鍝嶅簲澶淬€傘€?/p>
header(鈥楢ccess-Control-Expose-Headers: X-Captcha-Sign鈥?;
鎬荤粨
鐪嬩簡杩欎笁绉嶈В鍐虫柟妗堬紝鍩烘湰閮借兘婊¤冻鎴戜滑鐨勯渶姹傦紝鍙兘杩樻湁浜烘兂鍒颁簡鍙︿竴绉嶆柟妗堛€傛彁渚涗竴涓?nbsp;json 鎺ュ彛鍚嶏紝鍦ㄥ悗鍙扮敓鎴愬浘鐗囩劧鍚庝繚瀛樿捣鏉ワ紝杩斿洖 url 鍜?sign 缁欏墠绔紝杩欐牱灏卞ソ浜嗭紝浣嗘槸杩欐牱鍋氾紝鎴戜滑鐨勮祫婧愬苟涓嶅お鍙帶锛屼細閫犳垚涓€瀹氱殑璧勬簮娴垂锛岃繖閲屾垜骞舵病鏈夎€冭檻 杩欑鏂规銆?/p>
以上是关于API 浜や簰涓€庝箞鍋氬ソ鍥剧墖楠岃瘉鐮侊紵的主要内容,如果未能解决你的问题,请参考以下文章
django鍓嶅悗绔垎绂?form_03(楠岃瘉锛屾暟鎹煡璇紝浠g爜浼樺寲)