How to clear a PHP session using Jquery/Javascript?

后端 未结 3 492
借酒劲吻你
借酒劲吻你 2021-01-02 13:32

Just want to ask if is it possible to clear a PHP session using a jquery or javascript process? Because what I want to do is to clear the PHP session using a dialog box. If

3条回答
  •  滥情空心
    2021-01-02 14:27

    There is a way you can do it directly from javascript... first you have to declare a function that clears a cookie by its key

    function removeCookie(cookieName)
    {
        cookieValue = "";
        cookieLifetime = -1;
        var date = new Date();
        date.setTime(date.getTime()+(cookieLifetime*24*60*60*1000));
        var expires = "; expires="+date.toGMTString();
        document.cookie = cookieName+"="+JSON.stringify(cookieValue)+expires+"; path=/";
    }
    

    Now all you have to do is to remove a cookie with the key "PHPSESSID" like this

    removeCookie("PHPSESSID");
    

    Now when you var_dump($_SESSION) you find it empty array

提交回复
热议问题