0

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

1

1 Answer 1

1

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
}
8
  • tried - doesn't work - btw - why if (isset... - echo session gives a value - meaning - it is set ?
    – provance
    Commented May 10, 2022 at 19:50
  • Yes its optional to use isset but not a good practice because if session is not set what the use of unsetting it. Also on which page you are echoing the session?
    – user17133804
    Commented May 10, 2022 at 19:53
  • using ajax call. Changing tha value of the session using the same way - works. But unset the session - doesn't
    – provance
    Commented May 10, 2022 at 19:59
  • is this your full unset session page ?? or there is something else on page?
    – user17133804
    Commented May 10, 2022 at 20:04
  • see my update, pls
    – provance
    Commented May 10, 2022 at 20:07

Not the answer you're looking for? Browse other questions tagged or ask your own question.