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()?

4
  • Just some food for thought regarding sessions. Some say you should add it into config.
    – Howdy_McGee
    Aug 24, 2016 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.
    – user9447
    Aug 24, 2016 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, 2016 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.
    – user9447
    Aug 24, 2016 at 19:32

3 Answers 3

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.

2
  • 2
    Ok, if session is bad I suppose I will look at PHP's 'set_cookie()'. I would assume the same approach would apply as you have here, to use 'template_redirect' or 'wp_loaded'?
    – user9447
    Aug 24, 2016 at 22:52
  • I found this little document insightful at least as far as issues with sessions in WP: pressjitsu.com/blog/wordpress-sessions-performance
    – MikeiLL
    Mar 25, 2017 at 4:57
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.

0

We can use the easy way by init hook. Write this function in the function.php

function register_my_session() {
  if( !session_id() )
  session_start();
}
add_action('init', 'register_my_session');

after adding this function in function.php you can Set values in sessions like

$_SESSION['user_country'] = 'india';

Get session value

echo $_SESSION['user_country'];

for destroying session value

unset($_SESSION["user_country"]);
1
  • You should escape session values before you print them out.
    – fuxia
    Jul 8, 2021 at 15:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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