Wordpress 会话管理

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1441240/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 18:32:18  来源:igfitidea点击:

Wordpress session management

wordpresssession-management

提问by dkretz

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?

我正在使用 Wordpress 建立一个网站,我想利用它的会话。但我没有找到任何插件,甚至没有找到文档。在我开始破解它之前有什么建议或参考吗?

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.

注意:我问的是 WP 是否以及如何使用标准 PHP 会话本身,而不是如何添加 PHP 会话,例如使用 session_start()。显然,WP 维护的任何状态都是通过其他方式实现的。所以如果我想使用 PHP 会话,我需要完全自己添加和维护它,使用类似线程中的技术。

Thanks all!

谢谢大家!

采纳答案by Steve

WordPress doesn't appear to call session_start()because it wants to be stateless and if register_globalsis defined, it automatically destroys your $_SESSION

WordPress 似乎不会调用,session_start()因为它希望是无状态的,如果register_globals已定义,它会自动破坏您的$_SESSION

回答by Andrey Rudenko

It's a very bad idea to modify WP Corefiles for the ability to use sessions. The best way I've found is to call the session_start()from initaction hook.

为了使用会话的能力而修改WP Core文件是一个非常糟糕的主意。我发现的最好方法是调用session_start()frominit动作挂钩。

function kana_init_session() {
  session_start();
}

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

You can place it in functions.phpfile of your theme.

你可以把它放在functions.php你的主题文件中。

Detailed article can be found here: http://www.kanasolution.com/2011/01/session-variable-in-wordpress/

详细文章可以在这里找到:http: //www.kanasolution.com/2011/01/session-variable-in-wordpress/

回答by mshaffer

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.comhas wordpress; sub2.domain.comis another site. From the other site (sub2), I read the cookies to identify who the user is and if the user is logged in.
  1. 要允许 wordpress 的 cookie 跨子域持续存在,请安装 Root Cookie插件
  2. sub1.domain.com有 wordpress;sub2.domain.com是另一个站点。我从另一个站点 ( sub2) 读取 cookie 以识别用户是谁以及用户是否已登录。

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=>valuepairs. 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.

使用 PHP,我可以遍历 cookie,解析key=>value对。这些 cookie 让我知道[mshaffer]有一个 cookie 存储在 wordpress 上,并且还被认证为logged_in. cookie 的有效期是1255298821.

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

sub2 中,我可以查询 wordpress 的数据库并获取用户信息:

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

SELECT * FROM `wp_users` WHERE user_login = 'mshaffer' ...从这个查询中获取user_id, user_email

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

SELECT * FROM `wp_usermeta` WHERE user_id = '$user_id' ...从 wp 获取大量其他数据

With this info, I can add to my sub2session 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.comand redirect accordingly.

有了这些信息,我就可以添加到我的sub2会话变量/cookie 中,并对数据做我想做的事情。我可以识别我是否已登录,以及我的用户名……这让我可以获取许多不同的数据。我现在可以在sub2.domain.com 中使用 WordPress 身份验证并相应地重定向。

monte

蒙特

{x:

{X:

回答by farinspace

Consider using WordPress Transient API

考虑使用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.

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

Further considerations:

进一步的考虑:

回答by Anraiki

Wordpress doesn't seem to use any sessions.

Wordpress 似乎不使用任何会话。

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

最好的方法是使用它提供的动作钩子。

回答by Chirag Kalani

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/

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

回答by vmassuchetto

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

在这种情况下,使用session_start()on挂钩函数wp_loaded似乎有效。

回答by meddyman

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

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

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

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

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

session_start();

Then it will maintain all session variables.

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

回答by Alvin

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

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

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

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

Then add following line at the top of header.php

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

session_start();