WordPress 会话管理
我正在使用 Wordpress 建立一个网站,并且我想利用它的会话。但我没有找到任何插件,甚至文档。在我开始破解之前有什么建议或参考吗?
注意:我问的是 WP 是否以及如何使用标准 PHP 会话本身,而不是如何添加 PHP 会话,例如使用 session_start()。显然,WP 所维护的任何状态都是通过其他方式来完成的。因此,如果我想使用 PHP 会话,我需要使用线程中的技术完全自己添加和维护它。
谢谢大家!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
修改 WP Core 文件以获得使用会话的能力是一个非常糟糕的主意。我发现的最好方法是从
init
操作挂钩调用session_start()
。您可以将其放置在主题的
functions.php
文件中。详细文章可以在这里找到:http://www.kanasolution。 com/2011/01/session-variable-in-wordpress/
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 似乎没有调用
session_start()
因为它想要无状态如果定义了
register_globals
,它会自动销毁您的$_SESSION
WordPress doesn't appear to call
session_start()
because it wants to be statelessand if
register_globals
is defined, it automatically destroys your$_SESSION
考虑使用 WordPress Transient API
使用 Transient API 存储的值对所有用户可见,而不仅仅是当前用户,根据用于检索瞬态的唯一标识符,您可以为每个用户分配一个唯一标识符,本质上导致瞬态的行为非常类似于会话。
进一步考虑:
根据用户设置的对象缓存等,瞬态可能会
并不总是存储在数据库中(例如memcached),使用瞬态
会话可能意味着数据会变得庞大并填满内存
很快(在使用 memcached 时)。
此外,WP 似乎没有进行自动垃圾收集
瞬变:
https://wordpress.stackexchange.com/questions/6602/are-transients-garbage-collected< /a>
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
对于我需要做的,最好的答案包括:
我的 cookie 如下:
使用 PHP,我可以循环 cookie,解析
key=>value
对。这些 cookie 让我知道[mshaffer]
在 wordpress 上存储了一个 cookie,并且也被验证为logged_in
。 Cookie 的有效期为1255298821
。在sub2中,我可以查询wordpress的数据库并获取用户信息:
SELECT * FROM `wp_users` WHERE user_login = 'mshaffer' ...
grab user_id , user_email 来自此查询SELECT * FROM `wp_usermeta` WHERE user_id = '$user_id' ...
从 wp 获取大量其他数据有了这些信息,我可以添加到我的 sub2 会话变量/cookie 中,并对数据执行我想要的操作。我可以识别我是否已登录,以及我的用户名……这让我可以获取大量不同的数据。我现在可以在 sub2.domain.com 中使用 WordPress 身份验证并进行相应的重定向。
蒙特
{x:
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 似乎不使用任何会话。
最好的方法是使用它提供的操作挂钩。
Wordpress doesn't seem to use any sessions.
The best way to go about it is to use the action hooks it provides.
您是否检查过这里的解决方案,这可能适用于这里,并且它的简单方法
http://thedigilife.com/wordpress-how-to-set-session-custom-variable-while-login/
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/
在这种情况下,在
wp_loaded
上使用session_start()
挂钩函数似乎可行。Hooking a function with
session_start()
onwp_loaded
seems to work in this case.将此代码放在 wp-config.php 的第一行:
将此代码放在主题的 header.php 的第一行:
然后它将维护所有会话变量。
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.
如果您想使用自己的会话值,Wordpress 确实支持。
您需要在
wp-config.php
顶部添加以下行,然后在
header.php
顶部添加以下行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