php 如何在会话代码点火器中存储变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23078716/
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
How to store variable in session codeigniter
提问by Piyush
I would like to store the variable in the session currently i am tryin like this but not working.
我想将变量存储在会话中,目前我正在尝试这样但不起作用。
In Controller
在控制器中
<?php
session_start();
$_SESSION['myvar']='myvalue';
?>
回答by Rahil Wazir
You don't have to set the session the PHP way.
您不必以 PHP 方式设置会话。
CodeIgnter has its on Session Class
CodeIgnter 有它的会话类
Load the session library:
加载会话库:
$this->load->library('session');
To set session data:
设置会话数据:
$this->session->set_userdata('some_name', 'some_value');
To retrieve session data:
检索会话数据:
$this->session->userdata('some_name');
To remove session data:
删除会话数据:
$this->session->unset_userdata('some_name');
回答by maxime1992
Simple :
简单的 :
First, load this library
首先,加载这个库
$this->load->library('session');
Then, to add some informations in session :
然后,在 session 中添加一些信息:
$newdata = array(
'username' => 'johndoe',
'email' => '[email protected]',
'logged_in' => TRUE
);
$this->session->set_userdata($newdata);
Next, if you want to get values :
接下来,如果您想获取值:
$session_id = $this->session->userdata('session_id');
And to remove :
并删除:
$this->session->unset_userdata('some_name');
A simple search "codeigniter session" could have help you ...
一个简单的搜索“codeigniter session”可以帮助你......
https://www.codeigniter.com/user_guide/libraries/sessions.html
https://www.codeigniter.com/user_guide/libraries/sessions.html
Don't forget to upvote and mark as solved if you find this useful :)
如果您觉得这有用,请不要忘记投票并标记为已解决:)
回答by David Martin
I consider storing my session data in a controller. So this is how you should go about storing and retrieving session data
我考虑将我的会话数据存储在控制器中。所以这就是你应该如何存储和检索会话数据
1.Use autoload in your config.php files. It saves you time and load time
1.在 config.php 文件中使用自动加载。它可以节省您的时间和加载时间
autoload['libraries'] = ('session);
autoload['libraries'] = ('session);
2.In your controller.php create an array to store your session data.
2.在您的controller.php 中创建一个数组来存储您的会话数据。
$new_data = array( 'username' => 'martin', 'email' => '[email protected]', 'user_logged => TRUE );
$this->session->set_userdata($new_data);
$new_data = array( 'username' => 'martin', 'email' => '[email protected]', 'user_logged => TRUE );
$this->session->set_userdata($new_data);
- Then this is how to call your session data(create a variable and assign it the value of one of the session data you need):
- 然后这是如何调用您的会话数据(创建一个变量并将其分配给您需要的会话数据之一的值):
$username = $this->session->userdata('username');
$username = $this->session->userdata('username');
For further reference visit codeigniter user_guide/sessions
如需进一步参考,请访问 codeigniter user_guide/sessions
回答by user3470953
Codeigniter
代码点火器
set in session
在会话中设置
$newdata = array(
'username' => 'johndoe',
'email' => '[email protected]',
'logged_in' => TRUE
);
$this->session->set_userdata($newdata);
retrieve from session
从会话中检索
$this->session->userdata('username');