1

I am including a pure PHP library (nothing to do with WordPress) into a few of my page templates. This library calls PHP session_start() function and references the $_SESSION["cart"] global variable. Because of the session_start I include the library at the top of my template.

When I am logged in the library functions exactly as it is supposed to, but when I log out, the $_SESSION variables appear not to store data. Any ideas to fix this?

  • do you have debugging enabled? – Milo Jul 15 '15 at 17:08
  • if you are talking about ini_set('display_errors', 'On'); I am not. – Vial Jul 15 '15 at 17:23
  • 1
    I suggest enabling debugging to see if there's a warning or error being generated that may give some indication as to why the session isn't starting. – Milo Jul 15 '15 at 17:26
  • "This library calls PHP session_start() ..." -- where/how is this library loaded? – s_ha_dum Jul 15 '15 at 18:48
5

WordPress doesn't use PHP sessions, so WordPress itself can not be related with your sessions working or not regarding if you are logged in or not (I think).

Try to call session_start() on init action instead of doing it in a template file and be sure it is called before your custom library is loaded. Also, it can be interesting to end the PHP session on user log in and user log out to start a fresh one:

add_action('init', 'cyb_start_session', 1);
add_action('wp_logout', 'cyb_end_session');
add_action('wp_login', 'cyb_end_session');

function cyb_start_session() {
    if( ! session_id() ) {
        session_start();
        // now you can load your library that use $_SESSION
    }
}

function cyb_end_session() {
    session_destroy();
}

Because you library calls session_start(), con avoid to call it yourself but you still needs to load your library on init and destroy sessions on log in/log out:

add_action('init', 'cyb_start_session', 1);
add_action('wp_logout', 'cyb_end_session');
add_action('wp_login', 'cyb_end_session');

function cyb_start_session() {
      // load your libreary here (assuming it calls session_start())
}

function cyb_end_session() {
    session_destroy();
}

The key is to start PHP sessions as earlier as possible, before output anything. On init with priority 1 is the earliest it can be done in WP enviroment. If you do before, session can be destroyed by wp_unregister_GLOBALS().

A basic and silly but working example:

add_action('init', 'cyb_start_session', 1);
add_action('wp_logout', 'cyb_end_session');
add_action('wp_login', 'cyb_end_session');

function cyb_start_session() {
    if( ! session_id() ) {
        session_start();
        // now you can use $_SESSION
        $_SESSION['test'] = "test";
    }
}

function cyb_end_session() {
    session_destroy();
}


// This will print array(1) { ["test"]=> string(4) "test" }
// above the content of any post
add_filter( 'the_content', function( $content ) {

        var_dump($_SESSION);

        return $content;

} );
  • So, apparently the session isn't getting started inside cyb_start_session function. I test with if (!session_id())echo "session not created", and the template page shows "session not created". – Vial Jul 16 '15 at 15:09
  • How do I test to see if cyb_start_session is being called? – Vial Jul 16 '15 at 15:10
  • Have you ever tried my code as it is? I use it every time I need to use PHP sessions within WordPress and it works perfectly. As I say in the answer, be sure to call session_start() on init and use PHP sessions on later events. – cybmeta Jul 16 '15 at 15:26
  • I'm not sure what you mean with "to a T". – cybmeta Jul 16 '15 at 15:29
  • I am following it exactly... I think that the root of the problem has got to do with the fact that it works when I am signed in, and not when I am signed out. – Vial Jul 16 '15 at 15:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.