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!
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()
frominit
action hook.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/
WordPress doesn’t appear to call
session_start()
because it wants to be statelessand if
register_globals
is defined, it automatically destroys your$_SESSION
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:
Depending on a users setup with object cache, etc., transients may
not always be stored in the DB (e.g. memcached), using transients for
sessions could mean that the data can get bulky and fill memory
quickly (in the use of memcached).
Also, it seems that WP does not do auto garbage collection for
transients:
https://wordpress.stackexchange.com/questions/6602/are-transients-garbage-collected
For what I need to do, the best answer involves:
My cookies are as follows:
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 aslogged_in
. The expiry of the cookie is1255298821
.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 querySELECT * FROM `wp_usermeta` WHERE user_id = '$user_id' ...
grab lots of other data from wpWith 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:
WordPress doesn’t seem to use any sessions.
The best way to go about it is to use the action hooks it provides.
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/
Hooking a function with
session_start()
onwp_loaded
seems to work in this case.Put this code in wp-config.php at first line:
Put this code in theme’s header.php at first line:
Then it will maintain all session variables.
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
Then add following line at the top of
header.php