PHP sessionç详ç»åæ_PHPæç¨
程序员文章站
2022-04-13 15:20:49
...
1ï¼PHP session å·¥ä½åç
Sessionæ件å¨åäºæå¡å¨ç«¯ï¼ï¼é»è®¤æ åµä¸SESSION æ件ä¿åçç®å½ç±session.save_path æå®ï¼æ件å以sess_ 为åç¼ï¼åè·SESSION IDï¼å¦ï¼sess_c72665af28a8b14c0fe11afe3b59b51bãå¯ä»¥æ ¹æ®å®¢æ·ç«¯æä¾çsession idæ¥å¾å°ç¨æ·çæ件ï¼åå¾åéçå¼ï¼session idå¯ä»¥ä½¿ç¨å®¢æ·ç«¯çCookieæè Http1.1åè®®çQuery_Stringï¼å°±æ¯è®¿é®çURLç“?”åé¢çé¨åï¼æ¥ä¼ éç»æå¡å¨ï¼ç¶åæå¡å¨è¯»åSessionçç®å½ãä¹å°±æ¯è¯´ï¼session idæ¯åå¾åå¨å¨æå¡ä¸çsessionåéç身份è¯ã
å½ä»£ç session_start();è¿è¡çæ¶åï¼å°±å¨æå¡å¨ä¸äº§çäºä¸ä¸ªsessionæ件ï¼éä¹ä¹äº§çäºä¸ä¹å¯ä¸å¯¹åºçä¸ä¸ªsession idï¼å®ä¹sessionåé以ä¸å®å½¢å¼åå¨å¨åæ产ççsessionæ件ä¸ãéè¿session idï¼å¯ä»¥ååºå®ä¹çåéã跨页åï¼ä¸ºäºä½¿ç¨sessionï¼ä½ å¿ é¡»åæ§è¡session_start();å°åä¼äº§çä¸ä¸ªsessionæ件ï¼ä¸ä¹å¯¹åºäº§çç¸åºçsession idï¼ç¨è¿ä¸ªsession idæ¯åä¸åºåé¢æå°ç第ä¸ä¸ªsessionæ件ä¸çåéçï¼å 为è¿ä¸ªsession idä¸æ¯æå¼å®ç“é¥å”ãå¦æå¨session_start();ä¹åå 代ç session_id($session id);å°ä¸äº§çæ°çsessionæ件ï¼ç´æ¥è¯»åä¸è¿ä¸ªid对åºçsessionæ件ã
2. Session常è§å½æ°åç¨æ³
2.1 Session_start()ï¼ å¼å§ä¸ä¸ªä¼è¯æè è¿åå·²ç»åå¨çä¼è¯ã
è¿ä¸ªå½æ°æ²¡æåæ°ï¼ä¸è¿åå¼å为trueãå¦æä½ ä½¿ç¨åºäºcookieçsession,é£ä¹å¨ä½¿ç¨Session_start()ä¹åæµè§å¨ä¸è½æä»»ä½è¾åº.å¯ä»¥å¨php.iniéå¯å¨session.auto_start=1ï¼è¿æ ·å°±æ éæ¯æ¬¡ä½¿ç¨sessionä¹åé½è¦è°ç¨session_start()ãä½å¯ç¨è¯¥é项ä¹æä¸äºéå¶ï¼å¦æç¡®å®å¯ç¨äºsession.auto_startï¼åä¸è½å°å¯¹è±¡æ¾å ¥ä¼è¯ä¸ï¼å 为类å®ä¹å¿ é¡»å¨å¯å¨ä¼è¯ä¹åå 载以å¨ä¼è¯ä¸é建对象ã
2.2 注åSESSIONåé ï¼
PHP5使ç¨$_SESSION[‘xxx’]=xxx注åSESSIONå ¨å±åéã注æsession_register()ï¼
session_unregister ï¼session_is_registeredå¨php5ä¸ä¸å使ç¨ï¼é¤éå¨php.iniéæ
register_globle设为onï¼ä¸è¿åºäºå®å ¨èèï¼å¼ºçå»ºè®®å ³éregister_globleã
HTTP_SESSION_VARSä¹ä¸æå¡ä½¿ç¨äºï¼å®æ¹å»ºè®®ç¨$_SESSION代æ¿ä¹ã
Page1.php
session_start(); //使ç¨SESSIONåå¿ é¡»è°ç¨è¯¥å½æ°ã
$_SESSION[‘name’]=”ææ¯é»æé£æéµ!”; //注åä¸ä¸ªSESSIONåé
$_SESSION[‘passwd’]=”mynameislikui”;
$_SESSION[‘time’]=time();
//å¦æ客æ·ç«¯æ¯æcookieï¼å¯éè¿è¯¥é¾æ¥ä¼ ésessionå°ä¸ä¸é¡µã
echo '
éè¿COOKIEä¼ éSESSION';
//客æ·ç«¯ä¸æ¯æcookieæ¶ï¼ä½¿ç¨è¯¥åæ³ä¼ ésession.
echo '
éè¿URLä¼ éSESSION';
Page2.php
session_start();
echo $_SESSION['name'];
echo $_SESSION['passwd'];
echo date('Y m d H:i:s', $_SESSION['time']);
echo '
è¿åä¸ä¸é¡µ';
?>
2.3 session_id ([ string $id ] )ï¼Get and/or set the current session id
php5ä¸æ¢å¯ä»¥ä½¿ç¨session_id()ï¼ä¹å¯ä»¥éè¿éå å¨urlä¸çSIDåå¾å½åä¼è¯çsession_idåsession_nameã
å¦æsession_id()æå ·ä½æå®å¼çè¯ï¼å³æå®äºåæ°$idï¼ï¼å°å代å½åçsession_idå¼ã使ç¨è¯¥å½æ°åå¿ é¡»å¯å¨ä¼è¯ï¼session_start();
ä¾åï¼æå¨è®¾ç½®Session ççåæï¼
session_start();
// ä¿åä¸å¤©
$lifeTime = 24 * 3600;
setcookie(session_name(), session_id(), time() + $lifeTime, "/");
?>
å ¶å®Session è¿æä¾äºä¸ä¸ªå½æ°session_set_cookie_params(); æ¥è®¾ç½®Session ççåæçï¼è¯¥å½æ°å¿ é¡»å¨session_start() å½æ°è°ç¨ä¹åè°ç¨ï¼
// ä¿åä¸å¤©
$lifeTime = 24 * 3600;
session_set_cookie_params($lifeTime);
session_start();
$_SESSION["admin"] = true;
?>
å¦æ客æ·ç«¯ä½¿ç¨IE 6.0 ï¼session_set_cookie_params(); å½æ°è®¾ç½®Cookie ä¼æäºé®é¢ï¼æ以æ们è¿æ¯æå¨è°ç¨setcookie å½æ°æ¥å建cookieã
2.4 æ£æ¥sessionæ¯å¦åå¨ï¼
å¨ä»¥å¾çphpçæ¬ä¸é常使ç¨session_is_register()æ£æ¥sessionæ¯å¦åå¨ï¼å¦ææ¨ä½¿ç¨$_SESSION[‘XXX’]=XXXæ¥æ³¨åä¼è¯åéï¼åsession_is_register()å½æ°ä¸åèµ·ä½ç¨ãä½ å¯ä»¥ä½¿ç¨
isset($_SESSION[‘xxx’])æ¥æ¿ä»£ã
2.5æ´æ¹session_id session_regenerate_id([bool $delete_old_session]) æ´æ¹æååè¿åtrueï¼å¤±è´¥åè¿åfalseã
使ç¨è¯¥å½æ°å¯ä»¥ä¸ºå½åsessionæ´æ¹session_idï¼ä½é»è®¤ä¸æ¹åå½åsessionçå ¶ä»ä¿¡æ¯ï¼é¤é$delete_old_session为trueãä¾å¦ï¼
session_start();
$old_sessionid = session_id();
session_regenerate_id();
$new_sessionid = session_id();
echo "åå§SessionID: $old_sessionid
";
echo "æ°çSessionID: $new_sessionid
";
echo"
?>
2.6 session_name() è¿åå½åsessionçnameææ¹åå½åsessionçnameãå¦æè¦æ¹åå½åsessionçnameï¼å¿ é¡»å¨session_start()ä¹åè°ç¨è¯¥å½æ°ã注æï¼session_nameä¸è½åªç±æ°åç»æï¼å®è³å°å å«ä¸ä¸ªåæ¯ãå¦åä¼å¨æ¯æ¶æ¯å»é½çæä¸ä¸ªæ°çsession id.
sessionæ¹å示ä¾ï¼
$previous_name = session_name("WebsiteID");
echo "æ°çsessionå为ï¼$previous_name
";
?>
2.7 å¦ä½å é¤session
(1) unset ($_SESSION['xxx']) å é¤å个sessionï¼unset($_SESSION['xxx']) ç¨æ¥unregisterä¸ä¸ªå·²æ³¨åçsessionåéãå ¶ä½ç¨åsession_unregister()ç¸åãsession_unregister()å¨PHP5ä¸ä¸å使ç¨ï¼å¯å°ä¹æå ¥å·å®«ã
unset($_SESSION) æ¤å½æ°åä¸ä¸å¯ä½¿ç¨ï¼å®ä¼å°å ¨å±åé$_SESSIONéæ¯ï¼èä¸è¿æ²¡æå¯è¡çåæ³å°å ¶æ¢å¤ãç¨æ·ä¹ä¸åå¯ä»¥æ³¨å$_SESSIONåéã
(2) $_SESSION=array() å é¤å¤ä¸ªsession
(3) session_destroy()ç»æå½åçä¼è¯ï¼å¹¶æ¸ 空ä¼è¯ä¸çææèµæºã该å½æ°ä¸ä¼unsetåå½åsessionç¸å ³çå ¨å±åé(globalvariables),ä¹ä¸ä¼å é¤å®¢æ·ç«¯çsession cookie.PHPé»è®¤çsessionæ¯åºäºcookieçï¼å¦æè¦å é¤cookieçè¯ï¼å¿ é¡»åå©setcookie()å½æ°ã
ä¸é¢æ¯PHPå®æ¹å ³äºå é¤sessionçæ¡ä¾ï¼
// åå§åsession.
session_start();
/*** å é¤ææçsessionåé..ä¹å¯ç¨unset($_SESSION[xxx])é个å é¤ã****/
$_SESSION = array();
/***å é¤sessin id.ç±äºsessioné»è®¤æ¯åºäºcookieçï¼æ以使ç¨setcookieå é¤å å«session idçcookie.***/
if (isset($_COOKIE[session_name()])) {
setcookie(session_name(), '', time()-42000, '/');
}
// æåå½»åºéæ¯session.
session_destroy();
?>
ç±æ¤æ们å¯ä»¥å¾åºå é¤Sessionçæ¥éª¤ï¼
â session_start()
â¡$_SESSION=array()/unset($_SESSION['xxx'])
â¢session_destroy()
3. Sessionè·¨é¡µä¼ éé®é¢ï¼
3.1æ两ç§æ¹æ³ä¼ éä¸ä¸ªä¼è¯IDï¼cookie URL åæ°
ä¼è¯æ¨¡åæ¯æè¿ä¸¤ç§æ¹æ³ãcookie æ´ä¼åï¼ä½ç±äºä¸æ»æ¯å¯ç¨ï¼ä¹æä¾æ¿ä»£çæ¹æ³ã第äºç§æ¹æ³ç´æ¥å°ä¼è¯ID åµå ¥å°URL ä¸é´å»ã
PHP å¯ä»¥éæå°è½¬æ¢é¡µé¢ä¹é´çé¾æ¥ãå¦æ使ç¨ä½äºPHP 4.2ççæ¬ï¼åéè¦æå·¥å¨ç¼è¯PHP æ¶æ¿æ´»ï¼å¨Unix ä¸ï¼ç¨--enable-trans-sid é ç½®é项ãå¦ææ¤é ç½®é项åè¿è¡æ¶é项session.use_trans_sid é½è¢«æ¿æ´»(ä¿®æ¹php.ini)ï¼ç¸å¯¹URI å°è¢«èªå¨ä¿®æ¹ä¸ºå å«ä¼è¯IDã
Note: éç¸å¯¹çURL 被åå®ä¸ºæåå¤é¨ç«ç¹ï¼å æ¤æ²¡æéå SIDï¼å 为è¿å¯è½æ¯ä¸ªå®å ¨éæ£å°SID æ³é²ç»ä¸åçæå¡å¨ã
å¦å¤ï¼ä¹å¯ä»¥ç¨å¸¸éSIDãå¦æ客æ·ç«¯æ²¡æåéä¼è¯cookie ï¼åSID çæ ¼å¼ä¸ºsession_name=session_idï¼å¦å就为ä¸ä¸ªç©ºå符串ãå æ¤å¯ä»¥æ æ¡ä»¶å°å ¶åµå ¥å°URL ä¸å»ã
3. 2 解å³sessionè·¨é¡µä¼ éé®é¢çä¸æ¡éå¾
â 客æ·ç«¯ç¦ç¨äºcookieã
â¡æµè§å¨åºç°é®é¢ï¼ææ¶æ æ³ååcookie
â¢php.iniä¸çsession.use_trans_sid = 0æè ç¼è¯æ¶æ²¡ææå¼--enable-trans-sidé项
å½å®¢æ·ç«¯çCookie被ç¦ç¨æåºç°é®é¢æ¶ï¼PHPä¼èªå¨æsession idéçå¨URLä¸,è¿æ ·åéè¿session idå°±è½è·¨é¡µä½¿ç¨sessionåéäºãä½è¿ç§éçä¹æ¯æä¸å®æ¡ä»¶ç3php.iniä¸çsession.use_trans_sid = 1æè ç¼è¯æ¶æå¼æå¼äº--enable-trans-sidé项”ï¼
æç½äºä»¥ä¸çéçï¼æ们就å¯ä»¥å¾åºè§£å³sessionè·¨é¡µä¼ éé®é¢çä¸æ¡éå¾ï¼
1ã设置php.iniä¸çsession.use_trans_sid = 1æè ç¼è¯æ¶æå¼æå¼äº--enable-trans-sidé项ï¼è®©PHPèªå¨è·¨é¡µä¼ ésession idã
ï¼æ人说ï¼ä½å¨æµè¯çæ¶åï¼ä¿®æ¹php.iniåªç§æ¹å¼å¨é¡µé¢ä¸ç¨header('location: xx.php') åjavascriptãwindow.locationï¼xx.php æ åµä¸æ²¡æè¾¾å°æ³è¦çææãç®ååç°å¨xxä¸æ£å¸¸ãï¼
2ãæå¨éè¿URLä¼ å¼ãéè表åä¼ ésession idã
3ãç¨æ件ãæ°æ®åºçå½¢å¼ä¿åsession_id,å¨è·¨é¡µè¿ç¨ä¸æå¨è°ç¨ã
ä¸é¢ä¸¾ä¾è¯´æï¼
第ä¸ç§æ åµï¼
page1.php
session_start();
$_SESSION['var1']="ä¸å人æ°å ±åå½";
$url="ä¸ä¸é¡µ";
echo $url;
?>
page2.php
session_start();
echo "ä¼ éçsessionåévar1çå¼ä¸ºï¼".$_SESSION['var1'];
?>
è¿è¡ä»¥ä¸ä»£ç ï¼å¨å®¢æ·ç«¯cookieæ£å¸¸çæ åµä¸ï¼åºè¯¥å¯ä»¥å¨å¾å°ç»æ“ä¸å人æ°å ±åå½”ã
ç°å¨ä½ æå¨å ³é客æ·ç«¯çcookieï¼åè¿è¡ï¼å¯è½å¾ä¸å°ç»æäºå§ãå¦æå¾ä¸å°ç»æï¼å“设置php.iniä¸çsession.use_trans_sid = 1æè ç¼è¯æ¶æå¼æå¼äº--enable-trans-sidé项”ï¼åå¾å°ç»æ“ä¸å人æ°å ±åå½”
第äºç§éå¾ï¼
s1.php
session_start();
$_SESSION['var1']="ä¸å人æ°å ±åå½";
$sn = session_id();
//PHP5å®ä¹äºä¸ä¸ªå¸¸éSIDæ¥è¡¨ç¤ºsession_id()ï¼$urlè¿å¯ä»¥åæ$url='ä¸ä¸é¡µ';
$url="ä¸ä¸é¡µ";
echo $url;
?>
s2.php
session_id($_GET['s']);
session_start();
echo "ä¼ éçsessionåévar1çå¼ä¸ºï¼www.2cto.com".$_SESSION['var1'];
?>
第ä¸ç§éå¾ï¼
login.html
Login
请ç»å½ï¼
mylogin1.php
$name=$_POST['name'];
$pass=$_POST['pass'];
if(!$name || !$pass) {
echo "ç¨æ·åæå¯ç 为空ï¼è¯·éæ°ç»å½";
die();
}
if (!($name=="laogong" && $pass=="123")) {
echo "ç¨æ·åæå¯ç ä¸æ£ç¡®ï¼è¯·éæ°ç»å½";
die();
}
//注åç¨æ·
ob_start(); // Turn on output buffering
session_start();
$_SESSION['user']= $name;
$psid=session_id();
$fp=fopen("e:\\tmp\\phpsid.txt","w+");
fwrite($fp,$psid);
fclose($fp);
//身份éªè¯æåï¼è¿è¡ç¸å ³æä½
echo "å·²ç»å½
";
echo "ä¸ä¸é¡µ";
?>
mylogin2.php
$fp=fopen("e:\\tmp\\phpsid.txt","r");
$sid=fread($fp,1024);
fclose($fp);
session_id($sid);
session_start();
if(isset($_SESSION['user']) && $_SESSION['user']="laogong" ) {
echo "å·²ç»å½!";
}
else {
//æåç»å½è¿è¡ç¸å ³æä½
echo "æªç»å½ï¼æ æ访é®";
echo "请ç»å½åæµè§";
die();
}
?>
4ï¼å ³äºå¤æå¡å¨å ±äº«åä¸sessionç解å³åæ³
ç¨å¤§ä¸äºçç½ç«ï¼é常é½ä¼æ好å 个æå¡å¨ï¼æ¯ä¸ªæå¡å¨è¿è¡çä¸ååè½ç模åï¼ä½¿ç¨ä¸åçäºçº§ååï¼èä¸ä¸ªæ´ä½æ§å¼ºçç½ç«ï¼ç¨æ·ç³»ç»æ¯ç»ä¸çï¼å³ä¸å¥ç¨æ·åãå¯ç å¨æ´ä¸ªç½ç«çå个模åä¸é½æ¯å¯ä»¥ç»å½ä½¿ç¨çãå个æå¡å¨å ±äº«ç¨æ·æ°æ®æ¯æ¯è¾å®¹æå®ç°çï¼åªéè¦å¨å端æ¾ä¸ªæ°æ®åºæå¡å¨ï¼å个æå¡å¨éè¿ç»ä¸æ¥å£å¯¹ç¨æ·æ°æ®è¿è¡è®¿é®å³å¯ãä½è¿åå¨ä¸ä¸ªé®é¢ï¼å°±æ¯ç¨æ·å¨è¿ä¸ªæå¡å¨ç»å½ä¹åï¼è¿å ¥å¦ä¸ä¸ªæå¡å¨çå«ç模åæ¶ï¼ä»ç¶éè¦éæ°ç»å½ï¼è¿å°±æ¯ä¸æ¬¡ç»å½ï¼å ¨é¨éè¡çé®é¢ï¼æ å°å°ææ¯ä¸ï¼å ¶å®å°±æ¯å个æå¡å¨ä¹é´å¦ä½å®ç°å ±äº«SESSION æ°æ®çé®é¢ã
æ³è¦å ±äº«SESSION æ°æ®ï¼é£å°±å¿ é¡»å®ç°ä¸¤ä¸ªç®æ ï¼ä¸ä¸ªæ¯å个æå¡å¨å¯¹åä¸ä¸ªå®¢æ·ç«¯äº§ççSESSION ID å¿ é¡»ç¸åï¼å¹¶ä¸å¯éè¿åä¸ä¸ªCOOKIE è¿è¡ä¼ éï¼ä¹å°±æ¯è¯´å个æå¡å¨å¿ é¡»å¯ä»¥è¯»ååä¸ä¸ªå为PHPSESSID çCOOKIEï¼å¦ä¸ä¸ªæ¯SESSION æ°æ®çåå¨æ¹å¼/ä½ç½®å¿ é¡»ä¿è¯å个æå¡å¨é½è½å¤è®¿é®å°ãç®åå°è¯´å°±æ¯å¤æå¡å¨å ±äº«å®¢æ·ç«¯çSESSION IDï¼åæ¶è¿å¿ é¡»å ±äº«æå¡å¨ç«¯çSESSION æ°æ®ã
第ä¸ä¸ªç®æ çå®ç°å ¶å®å¾ç®åï¼åªéè¦å¯¹COOKIE çåï¼domainï¼è¿è¡ç¹æ®å°è®¾ç½®å³å¯ï¼é»è®¤æ åµä¸ï¼COOKIE çåæ¯å½åæå¡å¨çåå/IP å°åï¼èåä¸åçè¯ï¼å个æå¡å¨æ设置çCOOKIE æ¯ä¸è½ç¸äºè®¿é®çï¼å¦www.2cto.com çæå¡å¨æ¯ä¸è½è¯»åwww.bbb.com æå¡å¨è®¾ç½®çCOOKIE çãè¿éæ们æ说çåä¸ç½ç«çæå¡å¨æå ¶ç¹æ®æ§ï¼é£å°±æ¯ä»ä»¬åå±äºåä¸ä¸ªä¸çº§åï¼å¦ï¼aaa.infor96.com åwww.infor96.com é½å±äºå.infor96.comï¼é£ä¹æ们就å¯ä»¥è®¾ç½®COOKIE çå为.infor96.comï¼è¿æ ·aaa.infor96.comãwww.infor96.com ççé½å¯ä»¥è®¿é®æ¤COOKIEãPHP 代ç ä¸ç设置æ¹æ³å¦ä¸ï¼
CODE:
ini_set('session.cookie_domain', '.infor96.com');
第äºä¸ªç®æ çå®ç°å¯ä»¥ä½¿ç¨æä»¶å ±äº«æ¹å¼ï¼å¦NFS æ¹å¼ï¼ä½è®¾ç½®ãæä½ä¸æäºå¤æãæ们å¯ä»¥åèå åæ说çç»ä¸ç¨æ·ç³»ç»çæ¹å¼ï¼å³ä½¿ç¨æ°æ®åºæ¥ä¿åSESSION æ°æ®ï¼è¿æ ·å个æå¡å¨å°±å¯ä»¥æ¹ä¾¿å°è®¿é®åä¸ä¸ªæ°æ®æºï¼è·åç¸åçSESSION æ°æ®äºã
å ³äºå¦ä½å°sessionæ¾å ¥æ°æ®åºå¯ä»¥è§ãphp ç¨åºè®¾è®¡ãï¼å以ä¸ç½é¡µ
http://www.eb163.com/article.php?id=75&PHPSESSID=d226cc07cec0580ec7dad47119ee4667æèª æ²³å¤§æä¿¡çCrazy Coding人ç
Sessionæ件å¨åäºæå¡å¨ç«¯ï¼ï¼é»è®¤æ åµä¸SESSION æ件ä¿åçç®å½ç±session.save_path æå®ï¼æ件å以sess_ 为åç¼ï¼åè·SESSION IDï¼å¦ï¼sess_c72665af28a8b14c0fe11afe3b59b51bãå¯ä»¥æ ¹æ®å®¢æ·ç«¯æä¾çsession idæ¥å¾å°ç¨æ·çæ件ï¼åå¾åéçå¼ï¼session idå¯ä»¥ä½¿ç¨å®¢æ·ç«¯çCookieæè Http1.1åè®®çQuery_Stringï¼å°±æ¯è®¿é®çURLç“?”åé¢çé¨åï¼æ¥ä¼ éç»æå¡å¨ï¼ç¶åæå¡å¨è¯»åSessionçç®å½ãä¹å°±æ¯è¯´ï¼session idæ¯åå¾åå¨å¨æå¡ä¸çsessionåéç身份è¯ã
å½ä»£ç session_start();è¿è¡çæ¶åï¼å°±å¨æå¡å¨ä¸äº§çäºä¸ä¸ªsessionæ件ï¼éä¹ä¹äº§çäºä¸ä¹å¯ä¸å¯¹åºçä¸ä¸ªsession idï¼å®ä¹sessionåé以ä¸å®å½¢å¼åå¨å¨åæ产ççsessionæ件ä¸ãéè¿session idï¼å¯ä»¥ååºå®ä¹çåéã跨页åï¼ä¸ºäºä½¿ç¨sessionï¼ä½ å¿ é¡»åæ§è¡session_start();å°åä¼äº§çä¸ä¸ªsessionæ件ï¼ä¸ä¹å¯¹åºäº§çç¸åºçsession idï¼ç¨è¿ä¸ªsession idæ¯åä¸åºåé¢æå°ç第ä¸ä¸ªsessionæ件ä¸çåéçï¼å 为è¿ä¸ªsession idä¸æ¯æå¼å®ç“é¥å”ãå¦æå¨session_start();ä¹åå 代ç session_id($session id);å°ä¸äº§çæ°çsessionæ件ï¼ç´æ¥è¯»åä¸è¿ä¸ªid对åºçsessionæ件ã
2. Session常è§å½æ°åç¨æ³
2.1 Session_start()ï¼ å¼å§ä¸ä¸ªä¼è¯æè è¿åå·²ç»åå¨çä¼è¯ã
è¿ä¸ªå½æ°æ²¡æåæ°ï¼ä¸è¿åå¼å为trueãå¦æä½ ä½¿ç¨åºäºcookieçsession,é£ä¹å¨ä½¿ç¨Session_start()ä¹åæµè§å¨ä¸è½æä»»ä½è¾åº.å¯ä»¥å¨php.iniéå¯å¨session.auto_start=1ï¼è¿æ ·å°±æ éæ¯æ¬¡ä½¿ç¨sessionä¹åé½è¦è°ç¨session_start()ãä½å¯ç¨è¯¥é项ä¹æä¸äºéå¶ï¼å¦æç¡®å®å¯ç¨äºsession.auto_startï¼åä¸è½å°å¯¹è±¡æ¾å ¥ä¼è¯ä¸ï¼å 为类å®ä¹å¿ é¡»å¨å¯å¨ä¼è¯ä¹åå 载以å¨ä¼è¯ä¸é建对象ã
2.2 注åSESSIONåé ï¼
PHP5使ç¨$_SESSION[‘xxx’]=xxx注åSESSIONå ¨å±åéã注æsession_register()ï¼
session_unregister ï¼session_is_registeredå¨php5ä¸ä¸å使ç¨ï¼é¤éå¨php.iniéæ
register_globle设为onï¼ä¸è¿åºäºå®å ¨èèï¼å¼ºçå»ºè®®å ³éregister_globleã
HTTP_SESSION_VARSä¹ä¸æå¡ä½¿ç¨äºï¼å®æ¹å»ºè®®ç¨$_SESSION代æ¿ä¹ã
Page1.php
session_start(); //使ç¨SESSIONåå¿ é¡»è°ç¨è¯¥å½æ°ã
$_SESSION[‘name’]=”ææ¯é»æé£æéµ!”; //注åä¸ä¸ªSESSIONåé
$_SESSION[‘passwd’]=”mynameislikui”;
$_SESSION[‘time’]=time();
//å¦æ客æ·ç«¯æ¯æcookieï¼å¯éè¿è¯¥é¾æ¥ä¼ ésessionå°ä¸ä¸é¡µã
echo '
éè¿COOKIEä¼ éSESSION';
//客æ·ç«¯ä¸æ¯æcookieæ¶ï¼ä½¿ç¨è¯¥åæ³ä¼ ésession.
echo '
éè¿URLä¼ éSESSION';
Page2.php
session_start();
echo $_SESSION['name'];
echo $_SESSION['passwd'];
echo date('Y m d H:i:s', $_SESSION['time']);
echo '
è¿åä¸ä¸é¡µ';
?>
2.3 session_id ([ string $id ] )ï¼Get and/or set the current session id
php5ä¸æ¢å¯ä»¥ä½¿ç¨session_id()ï¼ä¹å¯ä»¥éè¿éå å¨urlä¸çSIDåå¾å½åä¼è¯çsession_idåsession_nameã
å¦æsession_id()æå ·ä½æå®å¼çè¯ï¼å³æå®äºåæ°$idï¼ï¼å°å代å½åçsession_idå¼ã使ç¨è¯¥å½æ°åå¿ é¡»å¯å¨ä¼è¯ï¼session_start();
ä¾åï¼æå¨è®¾ç½®Session ççåæï¼
session_start();
// ä¿åä¸å¤©
$lifeTime = 24 * 3600;
setcookie(session_name(), session_id(), time() + $lifeTime, "/");
?>
å ¶å®Session è¿æä¾äºä¸ä¸ªå½æ°session_set_cookie_params(); æ¥è®¾ç½®Session ççåæçï¼è¯¥å½æ°å¿ é¡»å¨session_start() å½æ°è°ç¨ä¹åè°ç¨ï¼
// ä¿åä¸å¤©
$lifeTime = 24 * 3600;
session_set_cookie_params($lifeTime);
session_start();
$_SESSION["admin"] = true;
?>
å¦æ客æ·ç«¯ä½¿ç¨IE 6.0 ï¼session_set_cookie_params(); å½æ°è®¾ç½®Cookie ä¼æäºé®é¢ï¼æ以æ们è¿æ¯æå¨è°ç¨setcookie å½æ°æ¥å建cookieã
2.4 æ£æ¥sessionæ¯å¦åå¨ï¼
å¨ä»¥å¾çphpçæ¬ä¸é常使ç¨session_is_register()æ£æ¥sessionæ¯å¦åå¨ï¼å¦ææ¨ä½¿ç¨$_SESSION[‘XXX’]=XXXæ¥æ³¨åä¼è¯åéï¼åsession_is_register()å½æ°ä¸åèµ·ä½ç¨ãä½ å¯ä»¥ä½¿ç¨
isset($_SESSION[‘xxx’])æ¥æ¿ä»£ã
2.5æ´æ¹session_id session_regenerate_id([bool $delete_old_session]) æ´æ¹æååè¿åtrueï¼å¤±è´¥åè¿åfalseã
使ç¨è¯¥å½æ°å¯ä»¥ä¸ºå½åsessionæ´æ¹session_idï¼ä½é»è®¤ä¸æ¹åå½åsessionçå ¶ä»ä¿¡æ¯ï¼é¤é$delete_old_session为trueãä¾å¦ï¼
session_start();
$old_sessionid = session_id();
session_regenerate_id();
$new_sessionid = session_id();
echo "åå§SessionID: $old_sessionid
";
echo "æ°çSessionID: $new_sessionid
";
echo"
";";
print_r($_SESSION);
echo"
?>
2.6 session_name() è¿åå½åsessionçnameææ¹åå½åsessionçnameãå¦æè¦æ¹åå½åsessionçnameï¼å¿ é¡»å¨session_start()ä¹åè°ç¨è¯¥å½æ°ã注æï¼session_nameä¸è½åªç±æ°åç»æï¼å®è³å°å å«ä¸ä¸ªåæ¯ãå¦åä¼å¨æ¯æ¶æ¯å»é½çæä¸ä¸ªæ°çsession id.
sessionæ¹å示ä¾ï¼
$previous_name = session_name("WebsiteID");
echo "æ°çsessionå为ï¼$previous_name
";
?>
2.7 å¦ä½å é¤session
(1) unset ($_SESSION['xxx']) å é¤å个sessionï¼unset($_SESSION['xxx']) ç¨æ¥unregisterä¸ä¸ªå·²æ³¨åçsessionåéãå ¶ä½ç¨åsession_unregister()ç¸åãsession_unregister()å¨PHP5ä¸ä¸å使ç¨ï¼å¯å°ä¹æå ¥å·å®«ã
unset($_SESSION) æ¤å½æ°åä¸ä¸å¯ä½¿ç¨ï¼å®ä¼å°å ¨å±åé$_SESSIONéæ¯ï¼èä¸è¿æ²¡æå¯è¡çåæ³å°å ¶æ¢å¤ãç¨æ·ä¹ä¸åå¯ä»¥æ³¨å$_SESSIONåéã
(2) $_SESSION=array() å é¤å¤ä¸ªsession
(3) session_destroy()ç»æå½åçä¼è¯ï¼å¹¶æ¸ 空ä¼è¯ä¸çææèµæºã该å½æ°ä¸ä¼unsetåå½åsessionç¸å ³çå ¨å±åé(globalvariables),ä¹ä¸ä¼å é¤å®¢æ·ç«¯çsession cookie.PHPé»è®¤çsessionæ¯åºäºcookieçï¼å¦æè¦å é¤cookieçè¯ï¼å¿ é¡»åå©setcookie()å½æ°ã
ä¸é¢æ¯PHPå®æ¹å ³äºå é¤sessionçæ¡ä¾ï¼
// åå§åsession.
session_start();
/*** å é¤ææçsessionåé..ä¹å¯ç¨unset($_SESSION[xxx])é个å é¤ã****/
$_SESSION = array();
/***å é¤sessin id.ç±äºsessioné»è®¤æ¯åºäºcookieçï¼æ以使ç¨setcookieå é¤å å«session idçcookie.***/
if (isset($_COOKIE[session_name()])) {
setcookie(session_name(), '', time()-42000, '/');
}
// æåå½»åºéæ¯session.
session_destroy();
?>
ç±æ¤æ们å¯ä»¥å¾åºå é¤Sessionçæ¥éª¤ï¼
â session_start()
â¡$_SESSION=array()/unset($_SESSION['xxx'])
â¢session_destroy()
3. Sessionè·¨é¡µä¼ éé®é¢ï¼
3.1æ两ç§æ¹æ³ä¼ éä¸ä¸ªä¼è¯IDï¼cookie URL åæ°
ä¼è¯æ¨¡åæ¯æè¿ä¸¤ç§æ¹æ³ãcookie æ´ä¼åï¼ä½ç±äºä¸æ»æ¯å¯ç¨ï¼ä¹æä¾æ¿ä»£çæ¹æ³ã第äºç§æ¹æ³ç´æ¥å°ä¼è¯ID åµå ¥å°URL ä¸é´å»ã
PHP å¯ä»¥éæå°è½¬æ¢é¡µé¢ä¹é´çé¾æ¥ãå¦æ使ç¨ä½äºPHP 4.2ççæ¬ï¼åéè¦æå·¥å¨ç¼è¯PHP æ¶æ¿æ´»ï¼å¨Unix ä¸ï¼ç¨--enable-trans-sid é ç½®é项ãå¦ææ¤é ç½®é项åè¿è¡æ¶é项session.use_trans_sid é½è¢«æ¿æ´»(ä¿®æ¹php.ini)ï¼ç¸å¯¹URI å°è¢«èªå¨ä¿®æ¹ä¸ºå å«ä¼è¯IDã
Note: éç¸å¯¹çURL 被åå®ä¸ºæåå¤é¨ç«ç¹ï¼å æ¤æ²¡æéå SIDï¼å 为è¿å¯è½æ¯ä¸ªå®å ¨éæ£å°SID æ³é²ç»ä¸åçæå¡å¨ã
å¦å¤ï¼ä¹å¯ä»¥ç¨å¸¸éSIDãå¦æ客æ·ç«¯æ²¡æåéä¼è¯cookie ï¼åSID çæ ¼å¼ä¸ºsession_name=session_idï¼å¦å就为ä¸ä¸ªç©ºå符串ãå æ¤å¯ä»¥æ æ¡ä»¶å°å ¶åµå ¥å°URL ä¸å»ã
3. 2 解å³sessionè·¨é¡µä¼ éé®é¢çä¸æ¡éå¾
â 客æ·ç«¯ç¦ç¨äºcookieã
â¡æµè§å¨åºç°é®é¢ï¼ææ¶æ æ³ååcookie
â¢php.iniä¸çsession.use_trans_sid = 0æè ç¼è¯æ¶æ²¡ææå¼--enable-trans-sidé项
å½å®¢æ·ç«¯çCookie被ç¦ç¨æåºç°é®é¢æ¶ï¼PHPä¼èªå¨æsession idéçå¨URLä¸,è¿æ ·åéè¿session idå°±è½è·¨é¡µä½¿ç¨sessionåéäºãä½è¿ç§éçä¹æ¯æä¸å®æ¡ä»¶ç3php.iniä¸çsession.use_trans_sid = 1æè ç¼è¯æ¶æå¼æå¼äº--enable-trans-sidé项”ï¼
æç½äºä»¥ä¸çéçï¼æ们就å¯ä»¥å¾åºè§£å³sessionè·¨é¡µä¼ éé®é¢çä¸æ¡éå¾ï¼
1ã设置php.iniä¸çsession.use_trans_sid = 1æè ç¼è¯æ¶æå¼æå¼äº--enable-trans-sidé项ï¼è®©PHPèªå¨è·¨é¡µä¼ ésession idã
ï¼æ人说ï¼ä½å¨æµè¯çæ¶åï¼ä¿®æ¹php.iniåªç§æ¹å¼å¨é¡µé¢ä¸ç¨header('location: xx.php') åjavascriptãwindow.locationï¼xx.php æ åµä¸æ²¡æè¾¾å°æ³è¦çææãç®ååç°å¨xxä¸æ£å¸¸ãï¼
2ãæå¨éè¿URLä¼ å¼ãéè表åä¼ ésession idã
3ãç¨æ件ãæ°æ®åºçå½¢å¼ä¿åsession_id,å¨è·¨é¡µè¿ç¨ä¸æå¨è°ç¨ã
ä¸é¢ä¸¾ä¾è¯´æï¼
第ä¸ç§æ åµï¼
page1.php
session_start();
$_SESSION['var1']="ä¸å人æ°å ±åå½";
$url="ä¸ä¸é¡µ";
echo $url;
?>
page2.php
session_start();
echo "ä¼ éçsessionåévar1çå¼ä¸ºï¼".$_SESSION['var1'];
?>
è¿è¡ä»¥ä¸ä»£ç ï¼å¨å®¢æ·ç«¯cookieæ£å¸¸çæ åµä¸ï¼åºè¯¥å¯ä»¥å¨å¾å°ç»æ“ä¸å人æ°å ±åå½”ã
ç°å¨ä½ æå¨å ³é客æ·ç«¯çcookieï¼åè¿è¡ï¼å¯è½å¾ä¸å°ç»æäºå§ãå¦æå¾ä¸å°ç»æï¼å“设置php.iniä¸çsession.use_trans_sid = 1æè ç¼è¯æ¶æå¼æå¼äº--enable-trans-sidé项”ï¼åå¾å°ç»æ“ä¸å人æ°å ±åå½”
第äºç§éå¾ï¼
s1.php
session_start();
$_SESSION['var1']="ä¸å人æ°å ±åå½";
$sn = session_id();
//PHP5å®ä¹äºä¸ä¸ªå¸¸éSIDæ¥è¡¨ç¤ºsession_id()ï¼$urlè¿å¯ä»¥åæ$url='ä¸ä¸é¡µ';
$url="ä¸ä¸é¡µ";
echo $url;
?>
s2.php
session_id($_GET['s']);
session_start();
echo "ä¼ éçsessionåévar1çå¼ä¸ºï¼www.2cto.com".$_SESSION['var1'];
?>
第ä¸ç§éå¾ï¼
login.html
请ç»å½ï¼
mylogin1.php
$name=$_POST['name'];
$pass=$_POST['pass'];
if(!$name || !$pass) {
echo "ç¨æ·åæå¯ç 为空ï¼è¯·éæ°ç»å½";
die();
}
if (!($name=="laogong" && $pass=="123")) {
echo "ç¨æ·åæå¯ç ä¸æ£ç¡®ï¼è¯·éæ°ç»å½";
die();
}
//注åç¨æ·
ob_start(); // Turn on output buffering
session_start();
$_SESSION['user']= $name;
$psid=session_id();
$fp=fopen("e:\\tmp\\phpsid.txt","w+");
fwrite($fp,$psid);
fclose($fp);
//身份éªè¯æåï¼è¿è¡ç¸å ³æä½
echo "å·²ç»å½
";
echo "ä¸ä¸é¡µ";
?>
mylogin2.php
$fp=fopen("e:\\tmp\\phpsid.txt","r");
$sid=fread($fp,1024);
fclose($fp);
session_id($sid);
session_start();
if(isset($_SESSION['user']) && $_SESSION['user']="laogong" ) {
echo "å·²ç»å½!";
}
else {
//æåç»å½è¿è¡ç¸å ³æä½
echo "æªç»å½ï¼æ æ访é®";
echo "请ç»å½åæµè§";
die();
}
?>
4ï¼å ³äºå¤æå¡å¨å ±äº«åä¸sessionç解å³åæ³
ç¨å¤§ä¸äºçç½ç«ï¼é常é½ä¼æ好å 个æå¡å¨ï¼æ¯ä¸ªæå¡å¨è¿è¡çä¸ååè½ç模åï¼ä½¿ç¨ä¸åçäºçº§ååï¼èä¸ä¸ªæ´ä½æ§å¼ºçç½ç«ï¼ç¨æ·ç³»ç»æ¯ç»ä¸çï¼å³ä¸å¥ç¨æ·åãå¯ç å¨æ´ä¸ªç½ç«çå个模åä¸é½æ¯å¯ä»¥ç»å½ä½¿ç¨çãå个æå¡å¨å ±äº«ç¨æ·æ°æ®æ¯æ¯è¾å®¹æå®ç°çï¼åªéè¦å¨å端æ¾ä¸ªæ°æ®åºæå¡å¨ï¼å个æå¡å¨éè¿ç»ä¸æ¥å£å¯¹ç¨æ·æ°æ®è¿è¡è®¿é®å³å¯ãä½è¿åå¨ä¸ä¸ªé®é¢ï¼å°±æ¯ç¨æ·å¨è¿ä¸ªæå¡å¨ç»å½ä¹åï¼è¿å ¥å¦ä¸ä¸ªæå¡å¨çå«ç模åæ¶ï¼ä»ç¶éè¦éæ°ç»å½ï¼è¿å°±æ¯ä¸æ¬¡ç»å½ï¼å ¨é¨éè¡çé®é¢ï¼æ å°å°ææ¯ä¸ï¼å ¶å®å°±æ¯å个æå¡å¨ä¹é´å¦ä½å®ç°å ±äº«SESSION æ°æ®çé®é¢ã
æ³è¦å ±äº«SESSION æ°æ®ï¼é£å°±å¿ é¡»å®ç°ä¸¤ä¸ªç®æ ï¼ä¸ä¸ªæ¯å个æå¡å¨å¯¹åä¸ä¸ªå®¢æ·ç«¯äº§ççSESSION ID å¿ é¡»ç¸åï¼å¹¶ä¸å¯éè¿åä¸ä¸ªCOOKIE è¿è¡ä¼ éï¼ä¹å°±æ¯è¯´å个æå¡å¨å¿ é¡»å¯ä»¥è¯»ååä¸ä¸ªå为PHPSESSID çCOOKIEï¼å¦ä¸ä¸ªæ¯SESSION æ°æ®çåå¨æ¹å¼/ä½ç½®å¿ é¡»ä¿è¯å个æå¡å¨é½è½å¤è®¿é®å°ãç®åå°è¯´å°±æ¯å¤æå¡å¨å ±äº«å®¢æ·ç«¯çSESSION IDï¼åæ¶è¿å¿ é¡»å ±äº«æå¡å¨ç«¯çSESSION æ°æ®ã
第ä¸ä¸ªç®æ çå®ç°å ¶å®å¾ç®åï¼åªéè¦å¯¹COOKIE çåï¼domainï¼è¿è¡ç¹æ®å°è®¾ç½®å³å¯ï¼é»è®¤æ åµä¸ï¼COOKIE çåæ¯å½åæå¡å¨çåå/IP å°åï¼èåä¸åçè¯ï¼å个æå¡å¨æ设置çCOOKIE æ¯ä¸è½ç¸äºè®¿é®çï¼å¦www.2cto.com çæå¡å¨æ¯ä¸è½è¯»åwww.bbb.com æå¡å¨è®¾ç½®çCOOKIE çãè¿éæ们æ说çåä¸ç½ç«çæå¡å¨æå ¶ç¹æ®æ§ï¼é£å°±æ¯ä»ä»¬åå±äºåä¸ä¸ªä¸çº§åï¼å¦ï¼aaa.infor96.com åwww.infor96.com é½å±äºå.infor96.comï¼é£ä¹æ们就å¯ä»¥è®¾ç½®COOKIE çå为.infor96.comï¼è¿æ ·aaa.infor96.comãwww.infor96.com ççé½å¯ä»¥è®¿é®æ¤COOKIEãPHP 代ç ä¸ç设置æ¹æ³å¦ä¸ï¼
CODE:
ini_set('session.cookie_domain', '.infor96.com');
第äºä¸ªç®æ çå®ç°å¯ä»¥ä½¿ç¨æä»¶å ±äº«æ¹å¼ï¼å¦NFS æ¹å¼ï¼ä½è®¾ç½®ãæä½ä¸æäºå¤æãæ们å¯ä»¥åèå åæ说çç»ä¸ç¨æ·ç³»ç»çæ¹å¼ï¼å³ä½¿ç¨æ°æ®åºæ¥ä¿åSESSION æ°æ®ï¼è¿æ ·å个æå¡å¨å°±å¯ä»¥æ¹ä¾¿å°è®¿é®åä¸ä¸ªæ°æ®æºï¼è·åç¸åçSESSION æ°æ®äºã
å ³äºå¦ä½å°sessionæ¾å ¥æ°æ®åºå¯ä»¥è§ãphp ç¨åºè®¾è®¡ãï¼å以ä¸ç½é¡µ
http://www.eb163.com/article.php?id=75&PHPSESSID=d226cc07cec0580ec7dad47119ee4667æèª æ²³å¤§æä¿¡çCrazy Coding人ç
上一篇: 几种RIA技术介绍