Destroy PHP session on page leaving

前端 未结 7 1203
轻奢々
轻奢々 2020-12-06 02:52

I need to destroy a session when user leave from a particular page. I use session_destroy() on the end of the page but its not feasible for me because my page h

7条回答
  •  佛祖请我去吃肉
    2020-12-06 02:59

    I solve the problem.First take the current url then chk the page stay on current url.if page is not in the current url then destroy the session.

    $url = "http" . ((!empty($_SERVER['HTTPS'])) ? "s" : "") . "://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
    $page_name="abc.php";      
    if (!preg_match("/$page_name/",$url)) 
     {
      session_destroy();
     } 
    

    But this code should be used on another pages.Because http is a stateless processes so no way to find when a user leave the page.

提交回复
热议问题