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?
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?
Share Improve this question edited Jul 15, 2015 at 20:02 fuxia♦ 107k38 gold badges255 silver badges459 bronze badges asked Jul 15, 2015 at 17:00 VialVial 1331 silver badge5 bronze badges 4- do you have debugging enabled? – Milo Commented Jul 15, 2015 at 17:08
- if you are talking about ini_set('display_errors', 'On'); I am not. – Vial Commented Jul 15, 2015 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 Commented Jul 15, 2015 at 17:26
- "This library calls PHP session_start() ..." -- where/how is this library loaded? – s_ha_dum Commented Jul 15, 2015 at 18:48
2 Answers
Reset to default 6WordPress 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;
} );
I have been thinking about using Wordpress' meta keys as session variables. Something like this:
add_user_meta(
int $user_id,
string $meta_key,
mixed $meta_value,
bool $unique = false
);
Wordpress will keep the meta values, link them to a user, and you can delete those values later on.