<?php
session_start();
?>
<?php
// 存儲會話數據
$_SESSION['username'] = 'John Doe';
$_SESSION['email'] = 'john@example.com';
?>
<?php
// 訪問會話數據
echo $_SESSION['username']; // 輸出: John Doe
echo $_SESSION['email']; // 輸出: john@example.com
?>
<?php
// 刪除某項會話數據
unset($_SESSION['email']);
?>
<?php
// 註銷整個會話
session_destroy();
?>
<?php
// 設置會話有效期為1小時
$expire_time = 3600;
session_set_cookie_params($expire_time);
session_start();
?>
啟用HTTPS,避免會話數據在網絡中明文傳輸。
不直接存儲敏感信息如密碼,可存儲令牌或ID引用。
使用session_regenerate_id()在登錄等關鍵操作時更新會話ID,防止固定會話攻擊。
定期清除過期會話,減少數據洩露風險。
<?php
session_start();
// 存儲會話數據
$_SESSION['username'] = 'John Doe';
$_SESSION['email'] = 'john@example.com';
// 訪問會話數據
echo $_SESSION['username']; // 輸出: John Doe
echo $_SESSION['email']; // 輸出: john@example.com
// 刪除會話數據
unset($_SESSION['email']);
// 註銷會話
session_destroy();
// 设置会话失效时间为一小時
$expire_time = 3600;
session_set_cookie_params($expire_time);
session_start();
?>