Wordpress session management
Asked Answered
F

9

23

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!

Flamenco answered 17/9, 2009 at 20:45 Comment(1)
Better solution here: wordpress.stackexchange.com/a/72744/20261Insectile
D
15

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

Deuterium answered 25/3, 2010 at 8:43 Comment(2)
Thanks - from that I learned that you can call session_start() yourself in wp_config, which won't be overwritten by updates. But better than that, I'm using wp_config to instantiate a singleton instance of a "Context" class I'm using to hold a bunch of stuff I'm adding (including e.g. extended user auth tables etc.), and it can call session_start(). Your reference clarified it for me. (Now I need to maintain wp_settings with his patch to retain $_SESSION, or else use the consolidated "$input" array. Or let "Context" maintain and update the session variables.) I hate it when they do that.Flamenco
Link is dead, but it's in the wayback machine web.archive.org/web/20161021035452/http://…Doglike
L
18

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/

Lagomorph answered 22/1, 2011 at 17:42 Comment(0)
D
15

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

Deuterium answered 25/3, 2010 at 8:43 Comment(2)
Thanks - from that I learned that you can call session_start() yourself in wp_config, which won't be overwritten by updates. But better than that, I'm using wp_config to instantiate a singleton instance of a "Context" class I'm using to hold a bunch of stuff I'm adding (including e.g. extended user auth tables etc.), and it can call session_start(). Your reference clarified it for me. (Now I need to maintain wp_settings with his patch to retain $_SESSION, or else use the consolidated "$input" array. Or let "Context" maintain and update the session variables.) I hate it when they do that.Flamenco
Link is dead, but it's in the wayback machine web.archive.org/web/20161021035452/http://…Doglike
A
5

For what I need to do, the best answer involves:

  1. To allow the cookie for wordpress to persist across subdomains, install the Root Cookie plugin.
  2. sub1.domain.com has wordpress; sub2.domain.com is another site. From the other site (sub2), I read the cookies to identify who the user is and if the user is logged in.

My cookies are as follows:

[wordpress_909bb230b32f5f0473202684d863b2e0] => mshaffer|1255298821|d0249fced9c323835c5bf7e84ad3ffea
[wordpress_logged_in_909bb230b32f5f0473202684d863b2e0] => mshaffer|1255298821|56e9c19541ecb596a1fa0995da935700

Using PHP, I can loop over the cookies, parse the key=>value pairs. These cookies let me know that [mshaffer] has a cookie stored on wordpress, and also is authenticated as logged_in. The expiry of the cookie is 1255298821.

In sub2, I can query the database of wordpress and grab the user info:

SELECT * FROM `wp_users` WHERE user_login = 'mshaffer' ... grab user_id, user_email from this query

SELECT * FROM `wp_usermeta` WHERE user_id = '$user_id' ... grab lots of other data from wp

With this info, I can add to my sub2 session variable / cookie and do what I want with the data. I can identify if I am logged in, and my username ... which let's me grab lots of different data. I can now use WordPress authentication in my sub2.domain.com and redirect accordingly.

monte

{x:

Allman answered 9/10, 2009 at 22:22 Comment(2)
Bear in mind that you can't rely on the cookie being genuine. I could send your site a cookie with any username I wish. The long hex string is (I think) a cryptographic checksum which you need to parse to ensure the cookie was set by WordPress.Tamaratamarack
Certainly Tamlyn you can pass additional salt/pepper in the cookie and additional checks on comparison.Allman
P
5

Consider using WordPress Transient API

Values stored using the Transient API are visible to all users, not just the current user, depending on the unique identifier used to retrieve the transient, you could assign each user a unique identifier essentially causing a transient to behave very much like a session.

Further considerations:

Pentalpha answered 17/3, 2011 at 0:23 Comment(0)
B
3

Wordpress doesn't seem to use any sessions.

The best way to go about it is to use the action hooks it provides.

Betook answered 4/10, 2009 at 4:16 Comment(3)
You are completely incorrect, Anraiki. Wordpress surely does use Sessions. And.. additionally, if you want to use your own custom session values, follow the following way: You need to add following lines at the top of wp-config.php if (!session_id()) { session_start(); } Then add following line at the top of header.php session_start();Cyprian
WordPress uses cookies, not sessionsSubaxillary
WordPress by default does not use sessions.Bedaub
U
1

Put this code in wp-config.php at first line:

if (!session_id()) {
    session_start();
}

Put this code in theme's header.php at first line:

session_start();

Then it will maintain all session variables.

Unmerciful answered 13/4, 2011 at 12:8 Comment(1)
This is nice, but don't do both. Putting it in wp-config.php is a problem as it will always create a session, and that uses resources. Even bot hits would create a session - one per hit as they don't do cookies. That's a lot of sessions!Bedaub
J
1

Have you checked the solution here this may work for here and its on easy way

http://thedigilife.com/wordpress-how-to-set-session-custom-variable-while-login/

Jenson answered 9/8, 2012 at 11:59 Comment(0)
A
1

Hooking a function with session_start() on wp_loaded seems to work in this case.

Acrobatic answered 10/8, 2012 at 14:48 Comment(0)
C
-1

If you wanna use your own session values, Wordpress does support it.

You need to add following lines at the top of wp-config.php

if (!session_id()) {
    session_start();
}

Then add following line at the top of header.php

session_start();
Cyprian answered 2/11, 2011 at 6:45 Comment(1)
Editing wo-config.php isn't real good if you are writing a plugin. If a plugin needs it, hook session_start() into init or wp_loaded. Worth noting that the fact that you can edit wp-config.php like this doesn't mean that WordPress actually supports it as such - after all, you are doing all the code. There is a serious problem here as well - putting session_start() means that every hit, even bots, will create a new session in your session folder. If you get a lot of traffic this can fill up and cause various problems, so best to only create a session when/where it is needed.Bedaub

© 2022 - 2024 — McMap. All rights reserved.