cookie鍜宻ession鏈哄埗涔嬮棿鐨勫尯鍒笌鑱旂郴_PHP鏁欑▼ | 甯涔嬪
鑰宻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>