简体   繁体   中英

how to delete/destroy/unset a specific php session

I need to delete/destroy/unset a specific session

$('#btnlogout').on('click', function(){
        $.post('pro.php', {fn: 'btn_logout'}, function(data){
            location.href = location.href;
        });
});

pro.php

function btn_logout(){
    //$_SESSION['id'] = 5; //this works
    unset($_SESSION['id']);  // doesn't work
    destroy_session();  // doesn't work
}

echo $_SESSION['id'] on main page always gives me a value

here - How do I destroy a specific session variable in PHP? unset($_SESSION['id']) is accepted answer, but I tried so many times - doesn't work

The unset method itself does the trick but until the page is refreshed it echo the last instance. you can try this

session_start();

if(isset(($_SESSION['id'])){
    unset($_SESSION['id']);
    die();
    header ('Location: index.php'); //optional line, any other page can be used
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM