æå¼Cookie使ç¨SESSIONï¼PHPä¸SESSIONä¸è½è·¨é¡µä¼ éé®é¢ç解å³åæ³
cookie|session|解å³|é®é¢
æå¼cookie使ç¨session
PHPä¸SESSIONä¸è½è·¨é¡µä¼ éé®é¢ç解å³åæ³
å¨PHPä¸ä½¿ç¨è¿SESSIONçæåå¯è½ä¼ç¢°å°è¿ä¹ä¸ä¸ªé®é¢ï¼SESSIONåéä¸è½è·¨é¡µä¼ éãè¿ä»¤æè¦æ¼äºå¥½äºæ¥åï¼æç»éè¿æ¥èµææè并解å³äºè¿ä¸ªé®é¢ãæ认为ï¼åºç°è¿ä¸ªé®é¢çåå æ以ä¸å ç¹ï¼
1ã客æ·ç«¯ç¦ç¨äºcookie
2ãæµè§å¨åºç°é®é¢ï¼ææ¶æ æ³ååcookie
3ãphp.iniä¸çsession.use_trans_sid = 0æè
ç¼è¯æ¶æ²¡ææå¼--enable-trans-sidé项
为ä»ä¹ä¼è¿æ ·å¢ï¼ä¸é¢æ解éä¸ä¸ï¼
Sessionå¨åäºæå¡å¨ç«¯ï¼é»è®¤ä»¥æ件æ¹å¼åå¨sessionï¼ï¼æ ¹æ®å®¢æ·ç«¯æä¾ç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æ件ã
PHPä¸çsessionå¨é»è®¤æ
åµä¸æ¯ä½¿ç¨å®¢æ·ç«¯çCookieæ¥ä¿åsession idç,æ以å½å®¢æ·ç«¯çcookieåºç°é®é¢çæ¶åå°±ä¼å½±åsessionäºãå¿
须注æçæ¯ï¼sessionä¸ä¸å®å¿
é¡»ä¾èµcookieï¼è¿ä¹æ¯sessionç¸æ¯cookieçé«æä¹å¤ãå½å®¢æ·ç«¯çCookie被ç¦ç¨æåºç°é®é¢æ¶ï¼PHPä¼èªå¨æsession idéçå¨URLä¸,è¿æ ·åéè¿session idå°±è½è·¨é¡µä½¿ç¨sessionåéäºãä½è¿ç§éçä¹æ¯æä¸å®æ¡ä»¶çï¼å³âphp.iniä¸çsession.use_trans_sid = 1æè
ç¼è¯æ¶æå¼æå¼äº--enable-trans-sidé项âã
æç½äºä»¥ä¸çéçï¼ç°å¨æ们æ¥æå¼cookie使ç¨session,主è¦éå¾æä¸æ¡ï¼
1ã设置php.iniä¸çsession.use_trans_sid = 1æè
ç¼è¯æ¶æå¼æå¼äº--enable-trans-sidé项ï¼è®©PHPèªå¨è·¨é¡µä¼ ésession idã
2ãæå¨éè¿URLä¼ å¼ãéè表åä¼ ésession idã
3ãç¨æ件ãæ°æ®åºçå½¢å¼ä¿åsession_id,å¨è·¨é¡µè¿ç¨ä¸æå¨è°ç¨ã
éè¿ä¾åæ¥è¯´æå§ï¼
s1.php
session_start();
$_SESSION['var1']="ä¸å人æ°å
񆆫";
$url="ä¸ä¸é¡µ";
echo $url;
?>
s2.php
session_start();
echo "ä¼ éçsessionåévar1çå¼ä¸ºï¼".$_SESSION['var1'];
?>
è¿è¡ä»¥ä¸ä»£ç ï¼å¨å®¢æ·ç«¯cookieæ£å¸¸çæ
åµä¸ï¼åºè¯¥å¯ä»¥å¨å¾å°ç»æâä¸å人æ°å
±åå½âã
ç°å¨ä½ æå¨å
³é客æ·ç«¯çcookieï¼åè¿è¡ï¼å¯è½å¾ä¸å°ç»æäºå§ãå¦æå¾ä¸å°ç»æï¼åâ设置php.iniä¸çsession.use_trans_sid = 1æè
ç¼è¯æ¶æå¼æå¼äº--enable-trans-sidé项âï¼åå¾å°ç»æâä¸å人æ°å
±åå½â
è¿ä¹å°±æ¯ä¸é¢æ说çéå¾1ã
ä¸é¢å说éå¾2ï¼
ä¿®æ¹ç代ç å¦ä¸ï¼
s1.php
session_start();
$_SESSION['var1']="ä¸å人æ°å
񆆫";
$sn = session_id();
$url="ä¸ä¸é¡µ";
echo $url;
?>
s2.php
session_id($_GET['s']);
session_start();
echo "ä¼ éçsessionåévar1çå¼ä¸ºï¼".$_SESSION['var1'];
?>
åæ³3è¿æ¯éè¿ä¾åæ¥è¯´æï¼
login.html
请ç»å½ï¼
mylogin1.php
$name=$_POST['name'];
$pass=$_POST['pass'];
if(!$name || !$pass) {
echo "ç¨æ·åæå¯ç 为空ï¼è¯·éæ°ç»å½";
die();
}
if (!($name=="laogong" && $pass=="123")) {
echo "ç¨æ·åæå¯ç ä¸æ£ç¡®ï¼è¯·éæ°ç»å½";
die();
}
//注åç¨æ·
ob_start();
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();
}
?>
åæ ·è¯·å ³écookieæµè¯ï¼ç¨æ·åï¼laogong å¯ç ï¼123 è¿æ¯éè¿æ件ä¿åsession idçï¼æ件æ¯ï¼e:\tmp\phpsid.txtï¼è¯·æ ¹æ®èªå·±çç³»ç»å³å®æ件åæè·¯å¾ã
è³äºç¨æ°æ®åºçæ¹æ³ï¼æå°±ä¸ä¸¾ä¾åäºï¼ä¸æ件çæ¹æ³ç±»ä¼¼ã
æ»ç»ä¸ä¸ï¼ä¸é¢çæ¹æ³æä¸ä¸ªå ±åç¹ï¼å°±æ¯å¨åä¸é¡µåå¾session idï¼ç¶åæ³åæ³ä¼ å°ä¸ä¸é¡µï¼å¨ä¸ä¸é¡µçsession_start();代ç ä¹åå 代ç session_id(ä¼ è¿æ¥çsession id);
注ï¼æ¬äººæµè¯ç¯å¢:Win2K Sever Aapache 1.3.31 PHP 4.3.4
å¦å¤,lidmå¨ç±»unixç³»ç»ä¸ä¹æµè¯éè¿
上一篇: 计算排列组合的php代码