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 startingwp_head
: Hook into wp_head(); in a pluginwp_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 startingwp_head
: Hook into wp_head(); in a pluginwp_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()
?
3 Answers
Reset to default 4There 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
版权声明:本文标题:How should you hook a session_start() when authoring a plugin? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741457221a2379825.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
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