5

Learning more about sessions I've gathered that a session_start() should come directly after <?php per Where exactly do I put a SESSION_START? and I wanted to play around and create a plugin that would add a session_start() to header.php after it's <?php but after searching I've been inclusive with trying to figure out the appropriate procedure to do this.

I did search for session but I've seen a variety of Q&As that mostly seem to use:

So when authoring a plugin that relies on session to be added to header.php what is the appropriate hook for adding session_start()?

  • Just some food for thought regarding sessions. Some say you should add it into config. – Howdy_McGee Aug 24 '16 at 19:10
  • really? I've never heard of that before. Isn't it bad for a plugin to modify the config? That may lead to another question after searching. – DᴀʀᴛʜVᴀᴅᴇʀ Aug 24 '16 at 19:11
  • actually, you would only start a session if it's not started already. if ( !session_id() ) { session_start(); } so I don't think it's really important but it should definitely be checked before you need to use the $_SESSION. But I'm curious if someone has a better explanation – bynicolas Aug 24 '16 at 19:28
  • @bynicolas yes I am aware of that but I was more interested in where to target the session for it's placement. – DᴀʀᴛʜVᴀᴅᴇʀ Aug 24 '16 at 19:32
4

There is no regular output and hence no header sent before template_redirect on the front end. If you need sessions on the back end too, use the action wp_loaded to cover both.

Example:

add_action( 'template_redirect', function() {

    $status = session_status();

    if ( PHP_SESSION_DISABLED === $status ) {
        // That's why you cannot rely on sessions!
        return;
    }

    if ( PHP_SESSION_NONE === $status ) {
        session_start();
    }

    $_SESSION[ 'foo' ] = 'bar';
});

Keep in mind that using sessions adds a whole set of very complex problems to your code, including security, scalability (load balancers), and following time consuming support issues. I don't recommend it.

| improve this answer | |
1

Here is also another solution for all kind of PHP versions what you can find inside CF Geo Plugin:

if (version_compare(PHP_VERSION, '7.0.0') >= 0) {
    if(function_exists('session_status') && session_status() == PHP_SESSION_NONE) {
        session_start(array(
          'cache_limiter' => 'private_no_expire',
          'read_and_close' => false,
       ));
    }
}
else if (version_compare(PHP_VERSION, '5.4.0') >= 0)
{
    if (function_exists('session_status') && session_status() == PHP_SESSION_NONE) {
        session_cache_limiter('private_no_expire');
        session_start();
    }
}
else
{
    if(session_id() == '') {
        if(version_compare(PHP_VERSION, '4.0.0') >= 0){
            session_cache_limiter('private_no_expire');
        }
        session_start();
    }
}

With this solution you can prevent any problem with session and start it anytime you need.

| improve this answer | |

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.