WordPress 会话管理

发布于 2024-08-05 06:11:51 字数 231 浏览 3 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

§对你不离不弃 2024-08-12 06:11:51

修改 WP Core 文件以获得使用会话的能力是一个非常糟糕的主意。我发现的最好方法是从 init 操作挂钩调用 session_start()

function kana_init_session() {
  session_start();
}

add_action('init', 'kana_init_session', 1);

您可以将其放置在主题的 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() 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/

北音执念 2024-08-12 06:11:51

WordPress 似乎没有调用 session_start() 因为它想要无状态
如果定义了 register_globals,它会自动销毁您的 $_SESSION

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

优雅的叶子 2024-08-12 06:11:51

考虑使用 WordPress Transient API

使用 Transient API 存储的值对所有用户可见,而不仅仅是当前用户,根据用于检索瞬态的唯一标识符,您可以为每个用户分配一个唯一标识符,本质上导致瞬态的行为非常类似于会话。

进一步考虑:

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:

涙—继续流 2024-08-12 06:11:51

对于我需要做的,最好的答案包括:

  1. 要允许 WordPress 的 Cookie 跨子域持续存在,请安装根 Cookie 插件
  2. sub1.domain.com 有 wordpress; sub2.domain.com 是另一个网站。从另一个站点(sub2),我读取 cookie 来识别用户是谁以及用户是否登录。

我的 cookie 如下:

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

使用 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:

  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:

冬天旳寂寞 2024-08-12 06:11:51

WordPress 似乎不使用任何会话。

最好的方法是使用它提供的操作挂钩。

Wordpress doesn't seem to use any sessions.

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

手心的海 2024-08-12 06:11:51

您是否检查过这里的解决方案,这可能适用于这里,并且它的简单方法

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/

最后的乘客 2024-08-12 06:11:51

在这种情况下,在 wp_loaded 上使用 session_start() 挂钩函数似乎可行。

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

可爱暴击 2024-08-12 06:11:51

将此代码放在 wp-config.php 的第一行:

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

将此代码放在主题的 header.php 的第一行:

session_start();

然后它将维护所有会话变量。

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.

记忆消瘦 2024-08-12 06:11:51

如果您想使用自己的会话值,Wordpress 确实支持

您需要在 wp-config.php 顶部添加以下行,

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

然后在 header.php 顶部添加以下行

session_start();

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();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文