douganmo1121 2012-03-02 18:49
浏览 58
已采纳

为什么需要session_ destroy()?

What does session_destroy() function do? To delete all session variables in global array $_SESSION I have to use session_unset(). To remove session from the client browser (delete session id and name) I have to unset cookies:

unset($_COOKIES[session_id()]);
unset($_COOKIES[session_name()]);

Why session_destroy() function is needed?

  • 写回答

1条回答 默认 最新

  • douhuiqi3855 2012-03-02 18:51
    关注

    session_destroy() ends the whole session, meaning it will be removed from PHP's session storage and can't ever be used again. If you'd only unset the session variables and cookies, the session would still be active server-side and could potentially be recycled if some session variables are set again and the cookie with the original session ID is sent to the client again.

    To put it in another way: a session basically consists of a secret ID stored somewhere on the web server, together with session variables registered to that session. The session ID is sent to the client (usually as a cookie) so the client can be identified as 'owner' of the session on later requests. Assuming a session has already been created and has variables registered to it, here's an overview of what the functions do:

    • session_start() imports all session variables belonging to the session ID that the client sent from the session registry to the $_SESSION array
    • session_unset() or calling unset() on $_SESSION variables will clear all variables registered to the current session, but it will not clear the session itself
    • Unsetting the session cookie of the client will signal to the client that the session is over, but this will not remove the session from the session registry on the server either
    • session_destroy() is the only function that will actually purge the session from the session registry, thus literally 'destroying' the session

    While session_destroy() will unregister all session variables, it won't clear the $_SESSION array in the script that's currently executing, so it's still a good idea to unset the session variables to prevent bugs and security issues.

    On a related note, the PHP manual recommends not to use session_unset() but rather unset the keys from $_SESSION:

    If $_SESSION (or $HTTP_SESSION_VARS for PHP 4.0.6 or less) is used, use unset() to unregister a session variable, i.e. unset($_SESSION['varname']);.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)