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
session_unset() - Frees all session variables ยท A session is started with the session_start() function. Note: The session_start() function must be callled at the beginning of every PHP script, before any HTML output or whitespace!
๐ŸŒ
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.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ php โ€บ php-unset-session-variable
PHP | Unset Session Variable - GeeksforGeeks
July 20, 2020 - Example: The following PHP function unregisters or clears a session variable whenever $_SESSION is used in some code. 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">GeeksforGeeks</h1> <b> PHP Unset session variables </b> <div class="height"> </div> <?php // start a new session session_start(); // Check if the session name exists if( isset($_SESSION['name']) ) { echo 'Session name is set.'.'<br>'; } else { echo 'Please set the session name !'.'<br>'; } echo'<br>'; $_SESSION['name'] = 'John'; //unset($_SESSION['name']); echo "Session Name : ".$_SESSION['name'].'<br>'; ?> </body> </html>
๐ŸŒ
SitePoint
sitepoint.com โ€บ php
Unset() and session_destroy(); - PHP - SitePoint Forums | Web Development & Design Community
November 28, 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"] โ€ฆ
๐ŸŒ
sebhastian
sebhastian.com โ€บ php-unset-session-variable
PHP how to unset a session variable | sebhastian
September 28, 2022 - The unset construct allows you ... "en"; $_SESSION["theme"] = "dark"; // ๐Ÿ‘‡ unset two session variables unset($_SESSION["User"], $_SESSION["lang"]); // ๐Ÿ‘‡ Array ( [theme] => dark ) print_r($_SESSION); ?>...
Find elsewhere
๐ŸŒ
W3Resource
w3resource.com โ€บ php-exercises โ€บ cookies-sessions โ€บ php-cookies-sessions-exercise-6.php
Destroy PHP session and unset all session variables
<?php session_save_path('i:/custom/'); session_start(); // Unset all session variables $_SESSION = []; // Destroy the session session_destroy(); echo "Session destroyed. All session variables have been unset."; ?> ... Session destroyed.
๐ŸŒ
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.
๐ŸŒ
Hoststud
hoststud.com โ€บ home โ€บ resources โ€บ languages โ€บ php
How to Unset Session Variable in PHP? | Web Hosting Forum - Review - Community & Resources
October 13, 2020 - session_unset(); Therefore the PHP Unset session variable is use to clear the session that was created by the PHP engine. ... A brief overview of Docker. A brief overview of Docker.
๐ŸŒ
PHPpot
phppot.com โ€บ php โ€บ php-session-destroy
PHP Session Destroy after 30 Minutes - PHPpot
For an older PHP version, use session_unset(). Letโ€™s create a login example code to use PHP session, session_destroy and all. It allows users to login and logout from the current session. Use this code if you are looking for a complete user registration and login in PHP script.
๐ŸŒ
Jobtensor
jobtensor.com โ€บ Tutorial โ€บ PHP โ€บ en โ€บ Sessions
PHP Sessions - session_start(), $_SESSION, session_unset(), session_destroy() | jobtensor
<?php session_start(); // starts the session // Storing session data $_SESSION["user"] = "admin"; $_SESSION["password"] = "password"; echo 'Username: ' . $_SESSION["user"]; echo '<br>'; echo 'Password: ' . $_SESSION["password"]; session_unset(); // removes session variables session_destroy(); // destroys the session // try to access the session variables after session is destroyed echo '<br>' .
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 });
๐ŸŒ
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...