You can unset session variable using:

  1. session_unset - Frees all session variables (It is equal to using: $_SESSION = array(); for older deprecated code)
  2. unset($_SESSION['Products']); - Unset only Products index in session variable. (Remember: You have to use like a function, not as you used)
  3. session_destroy โ€” Destroys all data registered to a session

To know the difference between using session_unset and session_destroy, read this SO answer. That helps.

Answer from Thamilhan on Stack Overflow
๐ŸŒ
PHP
php.net โ€บ manual โ€บ en โ€บ function.session-unset.php
PHP: session_unset - Manual
If $_SESSION is used, use unset() to unregister a session variable, i.e.
๐ŸŒ
W3Schools
w3schools.com โ€บ php โ€บ php_sessions.asp
PHP Sessions
On the next page load, the server gets the session ID from the cookie and uses it to load the session data into the PHP superglobal $_SESSION. The session data is then available to the current script in all scopes. Another way to show all the session variable values for a user session is by using print_r($_SESSION):
๐ŸŒ
Tutorialspoint
tutorialspoint.com โ€บ home โ€บ php โ€บ php sessions management
PHP Sessions Management
May 26, 2007 - This function does not need any argument and a single call can destroy all the session variables. If you want to destroy a single session variable then you can use unset() function to unset a session variable.
๐ŸŒ
EITCA
eitca.org โ€บ home โ€บ how can we delete a session variable in php?
How can we delete a session variable in PHP? - EITCA Academy
August 8, 2023 - To delete a session variable in PHP, you can use the unset() function or the session_unset() function. Both methods allow you to remove a specific session variable, clearing its value from the current session. The unset() function is a built-in PHP function that destroys a given variable.
๐ŸŒ
sebhastian
sebhastian.com โ€บ php-unset-session-variable
PHP how to unset a session variable | sebhastian
September 28, 2022 - <?php session_start(); $_SESSION["User"] = "Nathan"; $_SESSION["lang"] = "en"; // ๐Ÿ‘‡ unset User session unset($_SESSION["User"]); // ๐Ÿ‘‡ Array ( [lang] => en ) print_r($_SESSION); ?> Although unset is a construct, you need to surround the parameters with round brackets. The unset construct allows you to unset multiple variables with one call as well:
๐ŸŒ
SitePoint
sitepoint.com โ€บ php
Unset() and session_destroy(); - PHP - SitePoint Forums | Web Development & Design Community
December 2, 2009 - <?php session_start(); if(isset($_SESSION["user_name"])) if($_GET["destroy"]=="yes") { unset($_SESSION["user_name"]); session_destroy(); } if(!isset($_SESSION["user_name"]) && $_GET["user"]!="") $_SESSION["user_name"] โ€ฆ
Find elsewhere
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ php โ€บ php-unset-session-variable
PHP | Unset Session Variable - GeeksforGeeks
July 20, 2020 - It is mostly used for destroying a single session variable. ... <!DOCTYPE html> <html> <head> <style> body { width: 450px; height: 300px; margin: 10px; float: left; } .height { height: 10px; } </style> </head> <body> <h1 style="color:green"...
๐ŸŒ
Hoststud
hoststud.com โ€บ home โ€บ resources โ€บ languages โ€บ php
How to Unset Session Variable in PHP? | Web Hosting Forum - Review - Community & Resources
October 13, 2020 - session_destroy(); But for clearing ... session_unset(); Therefore the PHP Unset session variable is use to clear the session that was created by the PHP engine....
Top answer
1 of 2
2

Try using wp_doing_ajax() like so:

function unset_filter_session() {
    if ( ! wp_doing_ajax() ) {
        //Reset sessions on refresh page
        unset( $_SESSION['expenditure_filter'] );
    }
}

UPDATE

You can check the answer's revision for this update part..

UPDATE #2

Sorry, I didn't realize that you're loading a page fragment (#content-area) using the jQuery.load() method. Or that you're not using the admin-ajax.php to handle the AJAX request.

So if you're not using the wp_ajax_{action} or wp_ajax_nopriv_{action} action, or with the way you do the AJAX request, you can check whether the X-Requested-With header is set and that its value is XMLHttpRequest, and if so, you can cancel the session unsetting, like so:

function unset_filter_session() {
    if ( empty( $_SERVER['HTTP_X_REQUESTED_WITH'] ) ||
        'XMLHttpRequest' !== $_SERVER['HTTP_X_REQUESTED_WITH'] ) {
        //Reset sessions on refresh page, if not doing AJAX request
        unset( $_SESSION['expenditure_filter'] );
    }
}

That should work because jQuery always sets that header, unless you explicitly remove it โ€” or change its value.

See the headers option on the jQuery.ajax() reference:

The header X-Requested-With: XMLHttpRequest is always added, but its default XMLHttpRequest value can be changed

Note: The header name is X-Requested-With, but in the superglobal $_SERVER, its key is HTTP_X_REQUESTED_WITH. I.e. Don't use $_SERVER['X-Requested-With'].

UPDATE #3

As suggested by Lawrence Johnson, you (ahem, we) should use filter_input():

function unset_filter_session() {
    if ( 'XMLHttpRequest' !== filter_input( INPUT_SERVER, 'HTTP_X_REQUESTED_WITH' ) ) {
        //Reset sessions on refresh page, if not doing AJAX request
        unset( $_SESSION['expenditure_filter'] );
    }
}

(but I kept the previous code for reference)

2 of 2
0

One thing you could consider doing is adding a header variable to your AJAX requests. Even a GET or POST param would likely to the trick. Perhaps something like this:

/**
 * Unset filter session if page get's reloaded
 */
add_action( 'wp', 'unset_filter_session' );
function unset_filter_session() {
    //Reset sessions on refresh page
    if (!filter_input(INPUT_POST, 'isContentLoader', FILTER_SANITIZE_NUMBER_INT)) {
        unset( $_SESSION['expenditure_filter'] );
    }
}

And then

jQuery('#content-area').load(location.href + ' #content-area>*', { isContentLoader: 1 });
๐ŸŒ
Edureka Community
edureka.co โ€บ home โ€บ community โ€บ categories โ€บ web development โ€บ php โ€บ php unset session variable
PHP Unset Session Variable | Edureka Community
June 8, 2022 - 181641/php-unset-session-variable ยท Home ยท Community ยท Categories ยท Web Development ยท PHP ยท PHP Unset Session Variable ยท How to create a service that wraps browser APIs for consistent usage across platforms? Apr 17, 2025 ยท what is CURL ? Mar 19, 2025 ยท
๐ŸŒ
Sarathlal
sarathlal.com โ€บ basics-of-php-sessions-start-store-values-destroy-or-unset-them
Basics of PHP sessions โ€“ start, store values & destroy or unset them - Sarathlal N
We want to start PHP session using a PHP function & then we can store some information for that particular session only. We use $_SESSION array to store session data & its values are available on all web pages in that application until we quit the browser. The important things are the session variable ...
๐ŸŒ
Reddit
reddit.com โ€บ r/phphelp โ€บ unset instead of session_destroy() - long-term issues?
r/PHPhelp on Reddit: Unset instead of session_destroy() - long-term issues?
February 16, 2020 -

I have a web app that has a simple login using PHP sessions, along with a data processing component that retrieves and passes data from the web page back to server using PHP sessions to maintain the "conversation" or prior messages between requests.

Both technically worked fine in tandem when I first added, it's just that a user got kicked out following each completion of the AJAX session workflow. The login session was destroyed each time I called session_destroy().

I now just unset the specific session variables that were used in the AJAX workflow rather than calling session_destroy() - this is however still called in the login workflow when someone logs out. Although it's now all working as I want, are there any potential issues with this approach?

๐ŸŒ
Quora
quora.com โ€บ How-should-I-unset-session-in-PHP
How should I unset session in PHP? - Quora
Answer (1 of 3): This is my code to unset session when user logs out. connect_error) { die("Co...