Skip to content
Advertisement

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

Advertisement

Answer

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
}
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement