首頁  >  文章  >  後端開發  >  11 Session和數據保持

11 Session和數據保持

WBOY
WBOY原創
2016-07-29 08:55:241147瀏覽

1 透過SessionID維護網站中的使用者資訊

<code><span><span><?php</span>
session_start();
<span>$_SESSION</span>[<span>'visits'</span>]++;
<span>print</span><span>'You have visited here '</span>.<span>$_SESSION</span>[<span>'visits'</span>].<span>' times.<br>'</span>;
<span>echo</span><span>'session id = '</span>.<span>$_COOKIE</span>[<span>'PHPSESSID'</span>];
<span>echo</span><span>"<br>"</span>;
<span>echo</span><span>"session name = "</span>.session_name().<span>"<br>"</span>;
<span>?></span></span></code>

SessionID記錄在全域變數_COOKIE中,SessionID的名字是PHPSESSID,PHPSESSID也可以透過session_name()取得。

2 預防Session劫持

<code><span><span><span><?php</span>
ini_set(<span>'sessio.use_only_cookies'</span>, <span>true</span>);
session_start();
<span>$salt</span> = <span>'YourSpecialValueHere'</span>;
<span>$tokenstr</span> = date(<span>'W'</span>).<span>$salt</span>;
<span>$token</span> = md5(<span>$tokenstr</span>);
<span>echo</span><span>'token = '</span>.<span>$token</span>.<span>'<br>'</span>;

<span>if</span>(!<span>isset</span>(<span>$_REQUEST</span>[<span>'token'</span>]) || <span>$_REQUEST</span>[<span>'token'</span>] != <span>$token</span>)
{
    <span>exit</span>;
}
<span>$_SESSION</span>[<span>'token'</span>] = <span>$token</span>;
output_add_rewrite_var(<span>'token'</span>, <span>$token</span>);
<span>echo</span><span>'<a href="test.php">link</a>'</span>;
ob_flush();
output_reset_rewrite_vars();
<span>?></span></span></span></code>
<code><span><span><span><?php</span>
session_start();
output_add_rewrite_var(<span>'var'</span>, <span>'value'</span>);

<span>echo</span><span>'<a href="file.php">link</a>'</span>;
ob_flush();

output_reset_rewrite_vars();
<span>echo</span><span>'<a href="file.php">link</a>'</span>;
<span>?></span></span>以上例程会输出:

<span><<span>a</span><span>href</span>=<span>"file.php?PHPSESSID=xxx&var=value"</span>></span>link<span></<span>a</span>></span><span><<span>a</span><span>href</span>=<span>"file.php"</span>></span>link<span></<span>a</span>></span></span></code>

3 預防Session定制

  • 不會把session標誌符附加到URL上的session cookie.
  • 頻繁的生成新的sessionID
  • '>Class
(). numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i ').text(i)); }; $numbering.fadeIn(1700); }); }); 以上就介紹了11 Session和資料保持,包括了方面的內容,希望對PHP教程有興趣的朋友有所幫助。

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn