php 你如何在wordpress中使用会话变量?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23811748/
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-08-25 16:58:43  来源:igfitidea点击:

How do you use session variables in wordpress?

phpwordpresssessionvariables

提问by user1978592

I have the following plugin: http://wordpress.org/support/plugin/wp-session-manager

我有以下插件:http: //wordpress.org/support/plugin/wp-session-manager

I cannot work out how to use the session variables in WordPress. From what I understand by reading, this is how it should be used:

我不知道如何在 WordPress 中使用会话变量。根据我阅读的理解,这是应该如何使用它的:

I have the following on page one (page 1):

我在第一页(第 1 页)上有以下内容:

global $wp_session;
$wp_session['loggedIn'] = 15;

echo $wp_session['loggedIn'];

On the first page, the session variable is working but on the second page, the session variable is not working. Can anyone suggest me where I am going wrong?

在第一页上,会话变量正在工作,但在第二页上,会话变量不起作用。谁能建议我哪里出错了?

Thanks.

谢谢。

回答by Nathan Dawson

Replace:

代替:

global $wp_session;

With:

和:

$wp_session = WP_Session::get_instance();

Make sure you add $wp_session = WP_Session::get_instance();before you try to echo the variable on page 2.

确保$wp_session = WP_Session::get_instance();在尝试回显第 2 页上的变量之前添加。

回答by Anjali Parab

  1. Add below code in function.phpfile
  1. function.php文件中添加以下代码
function register_my_session(){
    if( ! session_id() ) {
        session_start();
    }
}

add_action('init', 'register_my_session');
  1. After that you can store value in session variable like
  1. 之后,您可以将值存储在会话变量中,例如
$_SESSION['something'] = $xyz

回答by Tanmoy Biswas

Introducing WP_Session:

介绍 WP_Session:

Example :

例子 :

global $wp_session;

$wp_session['user_name'] = 'User Name'; // A string

$wp_session['user_contact'] = array( 'email' => '[email protected]' );// An array

$wp_session['user_obj'] = new WP_User( 1 ); // An object

Wordpress session functions:

Wordpress 会话功能:

  1. wp_session_cache_expire() – get the session expiration time
  2. wp_session_commit() – write session data out to the transient
  3. wp_session_decode() – load data into the session from a serialized string
  4. wp_session_encode() – write session data out to a serialized string
  5. wp_session_regenerate_id() – change the ID of the current session to a new, random one
  6. wp_session_start() – start the session and load data based on the user's cookie
  7. wp_session_status() – check the status of the current session
  8. wp_session_unset() – clear out all variables in the current session
  9. wp_session_write_close() – write session data and end session.
  1. wp_session_cache_expire() – 获取会话过期时间
  2. wp_session_commit() – 将会话数据写入瞬态
  3. wp_session_decode() – 从序列化字符串将数据加载到会话中
  4. wp_session_encode() – 将会话数据写入序列化字符串
  5. wp_session_regenerate_id() – 将当前会话的 ID 更改为新的随机 ID
  6. wp_session_start() – 根据用户的 cookie 启动会话并加载数据
  7. wp_session_status() – 检查当前会话的状态
  8. wp_session_unset() – 清除当前会话中的所有变量
  9. wp_session_write_close() – 写入会话数据并结束会话。