$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>plugin development - $_SESSION inside php function executed by AJAX|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

plugin development - $_SESSION inside php function executed by AJAX

matteradmin9PV0评论

I am defining $_SESSION['session_test'] from $_POST['reason'] I get from AJAX call.

Inside of this function, $_SESSION['session_test'] does get defined, and I get desired echo result.

add_action('wp_ajax_return_reason', array( __CLASS__ , 'select_return_reason'));
static function select_return_reason() {

        $_SESSION['session_test'] = $_POST['reason'];
        echo "REASON: " . $_SESSION['session_test'];
        echo "<br>";

        wp_die();
    }

But, when I refresh page and try to echo $_SESSION['session_test'] in some other function below this one, it's empty - I get NULL.

add_action('woocommerce_after_cart_table', array( __CLASS__ , 'display_returned_items'));
static function display_returned_items() {
        echo "Testing: " .  $_SESSION['session_test'];
    }

Why $_SESSION['session_test'] value is available/defined only inside function which is executed by AJAX?

My session_start(); is on the top of the php file, right after the <?php opening tag.

Please let me know if I should provide more info. Thanks.

I am defining $_SESSION['session_test'] from $_POST['reason'] I get from AJAX call.

Inside of this function, $_SESSION['session_test'] does get defined, and I get desired echo result.

add_action('wp_ajax_return_reason', array( __CLASS__ , 'select_return_reason'));
static function select_return_reason() {

        $_SESSION['session_test'] = $_POST['reason'];
        echo "REASON: " . $_SESSION['session_test'];
        echo "<br>";

        wp_die();
    }

But, when I refresh page and try to echo $_SESSION['session_test'] in some other function below this one, it's empty - I get NULL.

add_action('woocommerce_after_cart_table', array( __CLASS__ , 'display_returned_items'));
static function display_returned_items() {
        echo "Testing: " .  $_SESSION['session_test'];
    }

Why $_SESSION['session_test'] value is available/defined only inside function which is executed by AJAX?

My session_start(); is on the top of the php file, right after the <?php opening tag.

Please let me know if I should provide more info. Thanks.

Share Improve this question asked Nov 12, 2018 at 10:51 Tahi ReuTahi Reu 3081 silver badge14 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

Wrapping this line inside if statement solved the problem:

if(!isset($_SESSION['session_test'])) {
    $_SESSION['session_test'] = $_POST['reason'];
}

I thought select_return_reason(){...} function which is holding this line was executing only by AJAX, not on each page load.

Post a comment

comment list (0)

  1. No comments so far