admin管理员组

文章数量:1289834

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:

  • init: Session is not starting
  • wp_head: Hook into wp_head(); in a plugin
  • wp_loaded: Getting headers already sent error from plugin

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

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:

  • init: Session is not starting
  • wp_head: Hook into wp_head(); in a plugin
  • wp_loaded: Getting headers already sent error from plugin

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

Share Improve this question edited May 23, 2017 at 12:40 CommunityBot 1 asked Aug 24, 2016 at 19:03 user9447user9447 1,7927 gold badges30 silver badges55 bronze badges 4
  • Just some food for thought regarding sessions. Some say you should add it into config. – Howdy_McGee Commented 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 Commented 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 Commented 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 Commented Aug 24, 2016 at 19:32
Add a comment  | 

3 Answers 3

Reset to default 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.

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.

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"]);

本文标签: How should you hook a sessionstart() when authoring a plugin