Wordpress session management

前端 未结 4 1530
清酒与你
清酒与你 2020-12-01 05:49

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 bef

相关标签:
4条回答
  • 2020-12-01 06:04

    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

    0 讨论(0)
  • 2020-12-01 06:07

    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/

    0 讨论(0)
  • 2020-12-01 06:12

    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

    0 讨论(0)
  • 2020-12-01 06:16

    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:

    | improve this answer | |
    0 讨论(0)
提交回复
热议问题