PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, May 17, 2022

[FIXED] how to delete/destroy/unset a specific php session

 May 17, 2022     php, session     No comments   

Issue

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


Solution

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
}


Answered By - Sameer
Answer Checked By - Senaida (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing