Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wordpress session management

Tags:

I'm putting up a site using Wordpress and I'd like to piggyback on its sessions. But I'm not finding any plugins, or even documentation. Any suggestions or references before I start hacking it?

Note: I'm asking about if and how WP uses standard PHP sessions itself, not how to add PHP sessions e.g. using session_start(). Apparently any state WP maintains is accomplished by other means. So if I want to use PHP sessions I need to add and maintain it myself entirely, using techniques like those in the thread.

Thanks all!

like image 233
dkretz Avatar asked Sep 17 '09 20:09

dkretz


People also ask

Where are WordPress sessions stored?

When a user logs on to a WordPress website, a session is created. The details of the session are stored in the WordPress database, specifically in wp_usermeta table.

Can I use session in WordPress?

WordPress doesn't use sessions, that's why your session variables aren't working. As a matter of fact, if certain variables are defined, WordPress will actually destroy $_SESSION to keep itself stateless. But if you really want to use sessions, try adding session_start() at the beginning of your wp-config. php file.

How do I enable sessions in WordPress?

To allow sessions you simply have to insert _SESSION into the array. The final code will be: $noUnset = array('_SESSION','GLOBALS', '_GET', '_POST', '_COOKIE', '_REQUEST', '_SERVER', '_ENV', '_FILES', 'table_prefix');

How do I set session variables in WordPress?

Wordpress session functions:wp_session_encode() – write session data out to a serialized string. wp_session_regenerate_id() – change the ID of the current session to a new, random one. wp_session_start() – start the session and load data based on the user's cookie.


2 Answers

It's a very bad idea to modify WP Core files for the ability to use sessions. The best way I've found is to call the session_start() from init action hook.

function kana_init_session() {   session_start(); }  add_action('init', 'kana_init_session', 1); 

You can place it in functions.php file of your theme.

Detailed article can be found here: http://www.kanasolution.com/2011/01/session-variable-in-wordpress/

like image 97
Andrey Rudenko Avatar answered Sep 19 '22 15:09

Andrey Rudenko


WordPress doesn't appear to call session_start() because it wants to be stateless and if register_globals is defined, it automatically destroys your $_SESSION

like image 34
Steve Avatar answered Sep 20 '22 15:09

Steve