欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

cookie鍜宻ession鏈哄埗涔嬮棿鐨勫尯鍒笌鑱旂郴_PHP鏁欑▼ | 甯涔嬪

程序员文章站 2022-05-15 09:55:05
...
鍏蜂綋鏉ヨcookie鏈哄埗閲囩敤鐨勬槸鍦ㄥ鎴风淇濇寔鐘舵€佺殑鏂规銆傚畠鏄湪鐢ㄦ埛绔殑浼氳瘽鐘舵€佺殑瀛樿串鏈哄埗锛屼粬闇€瑕佺敤鎴锋墦寮€瀹㈡埛绔殑cookie鏀寔銆俢ookie鐨勪綔鐢ㄥ氨鏄负浜嗚В鍐矵TTP鍗忚鏃犵姸鎬佺殑缂洪櫡鎵€浣滅殑鍔姏.
鑰宻ession鏈哄埗閲囩敤鐨勬槸涓€绉嶅湪瀹㈡埛绔笌鏈嶅姟鍣ㄤ箣闂翠繚鎸佺姸鎬佺殑瑙e喅鏂规銆傚悓鏃舵垜浠篃鐪嬪埌锛岀敱浜庨噰鐢ㄦ湇鍔″櫒绔繚鎸佺姸鎬佺殑鏂规鍦ㄥ鎴风涔熼渶瑕佷繚瀛樹竴涓爣璇嗭紝鎵€浠ession鏈哄埗鍙兘闇€瑕佸€熷姪浜巆ookie鏈哄埗鏉ヨ揪鍒颁繚瀛樻爣璇嗙殑鐩殑銆傝€宻ession鎻愪緵浜嗘柟渚跨鐞嗗叏灞€鍙橀噺鐨勬柟寮?br /> session鏄拡瀵规瘡涓€涓敤鎴风殑锛屽彉閲忕殑鍊间繚瀛樺湪鏈嶅姟鍣ㄤ笂锛岀敤涓€涓猻essionID鏉ュ尯鍒嗘槸鍝釜鐢ㄦ埛session鍙橀噺,杩欎釜鍊兼槸閫氳繃鐢ㄦ埛鐨勬祻瑙堝櫒鍦ㄨ闂殑鏃跺€欒繑鍥炵粰鏈嶅姟鍣紝褰撳鎴风鐢╟ookie鏃讹紝杩欎釜鍊间篃鍙兘璁剧疆涓虹敱get鏉ヨ繑鍥炵粰鏈嶅姟鍣ㄣ€?br /> 灏卞畨鍏ㄦ€ф潵璇达細褰撲綘璁块棶涓€涓娇鐢╯ession 鐨勭珯鐐癸紝鍚屾椂鍦ㄨ嚜宸辨満瀛愪笂寤虹珛涓€涓猚ookie锛屽缓璁湪鏈嶅姟鍣ㄧ鐨凷ESSION鏈哄埗鏇村畨鍏ㄤ簺.鍥犱负瀹冧笉浼氫换鎰忚鍙栧鎴峰瓨鍌ㄧ殑淇℃伅銆?/p>

姝g粺鐨刢ookie鍒嗗彂鏄€氳繃鎵╁睍HTTP鍗忚鏉ュ疄鐜扮殑锛屾湇鍔″櫒閫氳繃鍦℉TTP鐨勫搷搴斿ご涓姞涓婁竴琛岀壒娈婄殑鎸囩ず浠ユ彁绀烘祻瑙堝櫒鎸夌収鎸囩ず鐢熸垚鐩稿簲鐨刢ookie
浠庣綉缁滄湇鍔″櫒瑙傜偣鐪嬫墍鏈塇TTP璇锋眰閮界嫭绔嬩簬鍏堝墠璇锋眰銆傚氨鏄姣忎竴涓狧TTP鍝嶅簲瀹屽叏渚濊禆浜庣浉搴旇姹備腑鍖呭惈鐨勪俊鎭?br /> 鐘舵€佺鐞嗘満鍒跺厠鏈嶄簡HTTP鐨勪竴浜涢檺鍒跺苟鍏佽缃戠粶瀹㈡埛绔強鏈嶅姟鍣ㄧ缁存姢璇锋眰闂寸殑鍏崇郴銆傚湪杩欑鍏崇郴缁存寔鐨勬湡闂村彨鍋氫細璇?session)銆?br /> Cookies鏄湇鍔″櫒鍦ㄦ湰鍦版満鍣ㄤ笂瀛樺偍鐨勫皬娈垫枃鏈苟闅忔瘡涓€涓姹傚彂閫佽嚦鍚屼竴涓湇鍔″櫒銆侷ETF RFC 2965 HTTP State Management Mechanism 鏄€氱敤cookie瑙勮寖銆傜綉缁滄湇鍔″櫒鐢℉TTP澶村悜瀹㈡埛绔彂閫乧ookies锛屽湪瀹㈡埛缁堢锛屾祻瑙堝櫒瑙f瀽杩欎簺cookies骞跺皢瀹冧滑淇濆瓨涓轰竴涓湰鍦版枃浠讹紝瀹冧細鑷姩灏嗗悓涓€鏈嶅姟鍣ㄧ殑浠讳綍璇锋眰缂氫笂杩欎簺cookies
-------------------------------------------------------------------------------------------------------------------------------------------------------------------
鐞嗚Вsession鏈哄埗
session鏈哄埗鏄竴绉嶆湇鍔″櫒绔殑鏈哄埗锛屾湇鍔″櫒浣跨敤涓€绉嶇被浼间簬鏁e垪琛ㄧ殑缁撴瀯锛堜篃鍙兘灏辨槸浣跨敤鏁e垪琛級鏉ヤ繚瀛樹俊鎭€?/p>

褰撶▼搴忛渶瑕佷负鏌愪釜瀹㈡埛绔殑璇锋眰鍒涘缓涓€涓猻ession鐨勬椂鍊欙紝鏈嶅姟鍣ㄩ鍏堟鏌ヨ繖涓鎴风鐨勮姹傞噷鏄惁宸插寘鍚簡涓€涓猻ession鏍囪瘑 - 绉颁负 session id锛屽鏋滃凡鍖呭惈涓€涓猻ession id鍒欒鏄庝互鍓嶅凡缁忎负姝ゅ鎴风鍒涘缓杩噑ession锛屾湇鍔″櫒灏辨寜鐓ession id鎶婅繖涓?session妫€绱㈠嚭鏉ヤ娇鐢紙濡傛灉妫€绱笉鍒帮紝鍙兘浼氭柊寤轰竴涓級锛屽鏋滃鎴风璇锋眰涓嶅寘鍚玸ession id锛屽垯涓烘瀹㈡埛绔垱寤轰竴涓猻ession骞朵笖鐢熸垚涓€涓笌姝ession鐩稿叧鑱旂殑session id锛宻ession id鐨勫€煎簲璇ユ槸涓€涓棦涓嶄細閲嶅锛屽張涓嶅鏄撹鎵惧埌瑙勫緥浠ヤ豢閫犵殑瀛楃涓诧紝杩欎釜 session id灏嗚鍦ㄦ湰娆″搷搴斾腑杩斿洖缁欏鎴风淇濆瓨銆?/p>

淇濆瓨杩欎釜session id鐨勬柟寮忓彲浠ラ噰鐢╟ookie锛岃繖鏍峰湪浜や簰杩囩▼涓祻瑙堝櫒鍙互鑷姩鐨勬寜鐓ц鍒欐妸杩欎釜鏍囪瘑鍙戞尌缁欐湇鍔″櫒銆備竴鑸繖涓猚ookie鐨勫悕瀛楅兘鏄被浼间簬SEEESIONID锛岃€屻€傛瘮濡倃eblogic瀵逛簬web搴旂敤绋嬪簭鐢熸垚鐨刢ookie锛孞SESSIONID= ByOK3vjFD75aPnrF7C2HmdnV6QZcEbzWoWiBYEnLerjQ99zWpBng!-145788764锛屽畠鐨勫悕瀛楀氨鏄?JSESSIONID銆?/p>

鐢变簬cookie鍙互琚汉涓虹殑绂佹锛屽繀椤绘湁鍏朵粬鏈哄埗浠ヤ究鍦╟ookie琚姝㈡椂浠嶇劧鑳藉鎶妔ession id浼犻€掑洖鏈嶅姟鍣ㄣ€傜粡甯歌浣跨敤鐨勪竴绉嶆妧鏈彨鍋歎RL閲嶅啓锛屽氨鏄妸session id鐩存帴闄勫姞鍦║RL璺緞鐨勫悗闈紝闄勫姞鏂瑰紡涔熸湁涓ょ锛屼竴绉嶆槸浣滀负URL璺緞鐨勯檮鍔犱俊鎭紝琛ㄧ幇褰㈠紡涓篽ttp://...../xxx;jsessionid= ByOK3vjFD75aPnrF7C2HmdnV6QZcEbzWoWiBYEnLerjQ99zWpBng!-145788764
鍙︿竴绉嶆槸浣滀负鏌ヨ瀛楃涓查檮鍔犲湪URL鍚庨潰锛岃〃鐜板舰寮忎负http://...../xxx?jsessionid=ByOK3vjFD75aPnrF7C2HmdnV6QZcEbzWoWiBYEnLerjQ99zWpBng!-145788764
杩欎袱绉嶆柟寮忓浜庣敤鎴锋潵璇存槸娌℃湁鍖哄埆鐨勶紝鍙槸鏈嶅姟鍣ㄥ湪瑙f瀽鐨勬椂鍊欏鐞嗙殑鏂瑰紡涓嶅悓锛岄噰鐢ㄧ涓€绉嶆柟寮忎篃鏈夊埄浜庢妸session id鐨勪俊鎭拰姝e父绋嬪簭鍙傛暟鍖哄垎寮€鏉ャ€?br /> 涓轰簡鍦ㄦ暣涓氦浜掕繃绋嬩腑濮嬬粓淇濇寔鐘舵€侊紝灏卞繀椤诲湪姣忎釜瀹㈡埛绔彲鑳借姹傜殑璺緞鍚庨潰閮藉寘鍚繖涓猻ession id銆?/p>

鍙︿竴绉嶆妧鏈彨鍋氳〃鍗曢殣钘忓瓧娈点€傚氨鏄湇鍔″櫒浼氳嚜鍔ㄤ慨鏀硅〃鍗曪紝娣诲姞涓€涓殣钘忓瓧娈碉紝浠ヤ究鍦ㄨ〃鍗曟彁浜ゆ椂鑳藉鎶妔ession id浼犻€掑洖鏈嶅姟鍣ㄣ€傛瘮濡備笅闈㈢殑琛ㄥ崟




鍦ㄨ浼犻€掔粰瀹㈡埛绔箣鍓嶅皢琚敼鍐欐垚




杩欑鎶€鏈幇鍦ㄥ凡杈冨皯搴旂敤锛岀瑪鑰呮帴瑙﹁繃鐨勫緢鍙よ€佺殑iPlanet6(SunONE搴旂敤鏈嶅姟鍣ㄧ殑鍓嶈韩)灏变娇鐢ㄤ簡杩欑鎶€鏈€?br /> 瀹為檯涓婅繖绉嶆妧鏈彲浠ョ畝鍗曠殑鐢ㄥaction搴旂敤URL閲嶅啓鏉ヤ唬鏇裤€?/p>

鍦ㄨ皥璁簊ession鏈哄埗鐨勬椂鍊欙紝甯稿父鍚埌杩欐牱涓€绉嶈瑙?ldquo;鍙鍏抽棴娴忚鍣紝session灏辨秷澶变簡”銆傚叾瀹炲彲浠ユ兂璞′竴涓嬩細鍛樺崱鐨勪緥瀛愶紝闄ら潪椤惧涓诲姩瀵瑰簵瀹舵彁鍑洪攢鍗★紝鍚﹀垯搴楀缁濆涓嶄細杞绘槗鍒犻櫎椤惧鐨勮祫鏂欍€傚session鏉ヨ涔熸槸涓€鏍风殑锛岄櫎闈炵▼搴忛€氱煡鏈嶅姟鍣ㄥ垹闄や竴涓猻ession锛屽惁鍒欐湇鍔″櫒浼氫竴鐩翠繚鐣欙紝绋嬪簭涓€鑸兘鏄湪鐢ㄦ埛鍋歭og off鐨勬椂鍊欏彂涓寚浠ゅ幓鍒犻櫎session銆傜劧鑰屾祻瑙堝櫒浠庢潵涓嶄細涓诲姩鍦ㄥ叧闂箣鍓嶉€氱煡鏈嶅姟鍣ㄥ畠灏嗚鍏抽棴锛屽洜姝ゆ湇鍔″櫒鏍规湰涓嶄細鏈夋満浼氱煡閬撴祻瑙堝櫒宸茬粡鍏抽棴锛屼箣鎵€浠ヤ細鏈夎繖绉嶉敊瑙夛紝鏄ぇ閮ㄥ垎session鏈哄埗閮戒娇鐢ㄤ細璇漜ookie鏉ヤ繚瀛榮ession id锛岃€屽叧闂祻瑙堝櫒鍚庤繖涓?session id灏辨秷澶变簡锛屽啀娆¤繛鎺ユ湇鍔″櫒鏃朵篃灏辨棤娉曟壘鍒板師鏉ョ殑session銆傚鏋滄湇鍔″櫒璁剧疆鐨刢ookie琚繚瀛樺埌纭洏涓婏紝鎴栬€呬娇鐢ㄦ煇绉嶆墜娈垫敼鍐欐祻瑙堝櫒鍙戝嚭鐨凥TTP璇锋眰澶达紝鎶婂師鏉ョ殑session id鍙戦€佺粰鏈嶅姟鍣紝鍒欏啀娆℃墦寮€娴忚鍣ㄤ粛鐒惰兘澶熸壘鍒板師鏉ョ殑session銆?/p>

鎭版伆鏄敱浜庡叧闂祻瑙堝櫒涓嶄細瀵艰嚧session琚垹闄わ紝杩娇鏈嶅姟鍣ㄤ负seesion璁剧疆浜嗕竴涓け鏁堟椂闂达紝褰撹窛绂诲鎴风涓婁竴娆′娇鐢╯ession鐨勬椂闂磋秴杩囪繖涓け鏁堟椂闂存椂锛屾湇鍔″櫒灏卞彲浠ヨ涓哄鎴风宸茬粡鍋滄浜嗘椿鍔紝鎵嶄細鎶妔ession鍒犻櫎浠ヨ妭鐪佸瓨鍌ㄧ┖闂淬€?br /> -----------------------------------------------------------------------------------------------------------------------------------------------------------------------
鐢盝SESSIONID璋坈ookie涓嶴ESSION鐨勫尯鍒拰鑱旂郴
鍦ㄤ竴浜涙姇绁ㄤ箣绫荤殑鍦哄悎锛屾垜浠線寰€鍥犱负鍏钩鐨勫師鍒欒姹傛瘡浜哄彧鑳芥姇涓€绁紝鍦ㄤ竴浜沇EB寮€鍙戜腑涔熸湁绫讳技鐨勬儏鍐碉紝杩欐椂鍊欐垜浠€氬父浼氫娇鐢–OOKIE鏉ュ疄鐜帮紝渚嬪濡備笅鐨勪唬鐮侊細
if (cookies.lenght == 0 || cookies == null)
doStuffForNewbie();
//娌℃湁璁块棶杩?br /> }

else
{
doStuffForReturnVisitor(); //宸茬粡璁块棶杩囦簡
}

% >

杩欐槸寰堟祬鏄炬槗鎳傜殑閬撶悊锛屾娴婥OOKIE鐨勫瓨鍦紝濡傛灉瀛樺湪璇存槑宸茬粡杩愯杩囧啓鍏OOKIE鐨勪唬鐮佷簡锛岀劧鑰岃繍琛屼互涓婄殑浠g爜鍚庯紝鏃犺浣曟椂缁撴灉閮芥槸鎵цdoStuffForReturnVisitor()锛岄€氳繃鎺у埗闈㈡澘-Internet閫夐」-璁剧疆-瀵熺湅鏂囦欢鍗村缁堢湅涓嶅埌鐢熸垚鐨刢ookie鏂囦欢锛屽鎬紝浠g爜鏄庢槑娌℃湁闂锛屼笉杩囨棦鐒舵湁cookie锛岄偅灏辨樉绀哄嚭鏉ョ湅鐪嬨€?br /> cookie[]cookies = request.getCookies();
if (cookies.lenght == 0 || cookies == null)
out.println("Has not visited this website");
}

else
{
for (int i = 0; i {
out.println("cookie name:" + cookies[i].getName() + "cookie value:" +
cookie[i].getValue());
}
}

杩愯缁撴灉:
cookie name:JSESSIONID cookie value:KWJHUG6JJM65HS2K6 涓轰粈涔堜細鏈塩ookie鍛?澶у閮界煡閬擄紝http鏄棤鐘舵€佺殑鍗忚锛屽鎴锋瘡娆¤鍙杦eb椤甸潰鏃讹紝鏈嶅姟鍣ㄩ兘鎵撳紑鏂扮殑浼氳瘽锛岃€屼笖鏈嶅姟鍣ㄤ篃涓嶄細鑷姩缁存姢瀹㈡埛鐨勪笂涓嬫枃淇℃伅锛岄偅涔堣鎬庝箞鎵嶈兘瀹炵幇缃戜笂鍟嗗簵涓殑璐墿杞﹀憿锛宻ession灏辨槸涓€绉嶄繚瀛樹笂涓嬫枃淇℃伅鐨勬満鍒讹紝瀹冩槸閽堝姣忎竴涓敤鎴风殑锛屽彉閲忕殑鍊间繚瀛樺湪鏈嶅姟鍣ㄧ锛岄€氳繃SessionID鏉ュ尯鍒嗕笉鍚岀殑瀹㈡埛,session鏄互cookie鎴朥RL閲嶅啓涓哄熀纭€鐨勶紝榛樿浣跨敤cookie鏉ュ疄鐜帮紝绯荤粺浼氬垱閫犱竴涓悕涓篔SESSIONID鐨勮緭鍑篶ookie锛屾垜浠彨鍋歴ession cookie,浠ュ尯鍒玴ersistent cookies,涔熷氨鏄垜浠€氬父鎵€璇寸殑cookie,娉ㄦ剰session cookie鏄瓨鍌ㄤ簬娴忚鍣ㄥ唴瀛樹腑鐨勶紝骞朵笉鏄啓鍒扮‖鐩樹笂鐨勶紝杩欎篃灏辨槸鎴戜滑鍒氭墠鐪嬪埌鐨凧SESSIONID锛屾垜浠€氬父鎯呮槸鐪嬩笉鍒癑SESSIONID鐨勶紝浣嗘槸褰撴垜浠妸娴忚鍣ㄧ殑cookie绂佹鍚庯紝web鏈嶅姟鍣ㄤ細閲囩敤URL閲嶅啓鐨勬柟寮忎紶閫扴essionid锛屾垜浠氨鍙互鍦ㄥ湴鍧€鏍忕湅鍒皊essionid=KWJHUG6JJM65HS2K6涔嬬被鐨勫瓧绗︿覆銆?br /> 鏄庣櫧浜嗗師鐞嗭紝鎴戜滑灏卞彲浠ュ緢瀹规槗鐨勫垎杈ㄥ嚭persistent cookies鍜宻ession cookie鐨勫尯鍒簡锛岀綉涓婇偅浜涘叧浜庝袱鑰呭畨鍏ㄦ€х殑璁ㄨ涔熷氨涓€鐩簡鐒朵簡锛宻ession cookie閽堝鏌愪竴娆′細璇濊€岃█锛屼細璇濈粨鏉焥ession cookie涔熷氨闅忕潃娑堝け浜嗭紝鑰宲ersistent cookie鍙槸瀛樺湪浜庡鎴风纭洏涓婄殑涓€娈垫枃鏈紙閫氬父鏄姞瀵嗙殑锛夛紝鑰屼笖鍙兘浼氶伃鍒癱ookie娆洪獥浠ュ強閽堝cookie鐨勮法绔欒剼鏈敾鍑伙紝鑷劧涓嶅session cookie瀹夊叏浜嗐€?br /> 閫氬父session cookie鏄笉鑳借法绐楀彛浣跨敤鐨勶紝褰撲綘鏂板紑浜嗕竴涓祻瑙堝櫒绐楀彛杩涘叆鐩稿悓椤甸潰鏃讹紝绯荤粺浼氳祴浜堜綘涓€涓柊鐨剆essionid锛岃繖鏍锋垜浠俊鎭叡浜殑鐩殑灏辫揪涓嶅埌浜嗭紝姝ゆ椂鎴戜滑鍙互鍏堟妸sessionid淇濆瓨鍦╬ersistent cookie涓紝鐒跺悗鍦ㄦ柊绐楀彛涓鍑烘潵锛屽氨鍙互寰楀埌涓婁竴涓獥鍙essionID浜嗭紝杩欐牱閫氳繃session cookie鍜宲ersistent cookie鐨勭粨鍚堟垜浠氨瀹炵幇浜嗚法绐楀彛鐨剆ession tracking锛堜細璇濊窡韪級銆?br /> 鍦ㄤ竴浜泈eb寮€鍙戠殑涔︿腑锛屽線寰€鍙槸绠€鍗曠殑鎶奡ession鍜宑ookie浣滀负涓ょ骞跺垪鐨刪ttp浼犻€佷俊鎭殑鏂瑰紡锛宻ession cookies浣嶄簬鏈嶅姟鍣ㄧ锛宲ersistent cookie浣嶄簬瀹㈡埛绔紝鍙槸session鍙堟槸浠ookie涓哄熀纭€鐨勶紝鏄庣櫧鐨勪袱鑰呬箣闂寸殑鑱旂郴鍜屽尯鍒紝鎴戜滑灏变笉闅鹃€夋嫨鍚堥€傜殑鎶€鏈潵寮€鍙憌eb service浜嗐€?/p>

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/477283.htmlTechArticle鍏蜂綋鏉ヨcookie鏈哄埗閲囩敤鐨勬槸鍦ㄥ鎴风淇濇寔鐘舵€佺殑鏂规銆傚畠鏄湪鐢ㄦ埛绔殑浼氳瘽鐘舵€佺殑瀛樿串鏈哄埗锛屼粬闇€瑕佺敤鎴锋墦寮€瀹㈡埛绔殑cookie鏀寔銆俢ookie鐨勪綔...