WordPress session problems

I have found a bunch of articles talking about using sessions.
From what I have been able to find the best way is to add an init action.
I have been following this article
http://devondev.com/2012/02/03/using-the-php-session-in-wordpress/

But I must be missing something. every time I call the page with my plugin there is no session id.

Read More

Am I missing something?

thanks for any help

add_action('init', 'my_GB_StartSession', 1);
add_action('wp_logout', 'my_GB_myEndSession');
add_action('wp_login', 'my_GB_myEndSession');
if (!function_exists('my_GB_StartSession')) {
    function my_GB_StartSession() {
        if(!session_id()) {
            errorLog("session startingn");
            session_start();
        }
    }
}
function UnitNet_GB_myEndSession() {
    session_destroy ();
}

Related posts

Leave a Reply

1 comment

  1. try this and add to your theme’s function.php :

    add_action('init', 'myStartSession', 1);
    add_action('wp_logout', 'myEndSession');
    add_action('wp_login', 'myEndSession');
    
    function myStartSession() {
        if(!session_id()) {
            session_start();
        }
    }
    
    function myEndSession() {
        session_destroy ();
    }
    

    Now the session is yours to use as you wish in your code:

    $_SESSION['myKey'] = "Some data I need later";
    
    if(isset($_SESSION['myKey'])) {
        $value = $_SESSION['myKey'];
    } else {
        $value = '';
    }
    

    Or add this to wp-config.php before the wp-settings (e.g top of the wp-config.php)

    if (!session_id())
        session_start();