php 如何获取会话数组数据 CodeIgniter
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35449191/
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 get session array data CodeIgniter
提问by metaphor
I'm not quite sure why the username didn't appear on view page if i change the session data into array (i'm new in CodeIgniter). I have auth
controllerto make login process:
如果我将会话数据更改为数组(我是 CodeIgniter 中的新手),我不太确定为什么用户名没有出现在视图页面上。我有auth
控制器来进行登录过程:
function index() {
$this->load->library('form_validation');
$this->form_validation->set_rules('username', 'username', 'required|trim|xss_clean');
$this->form_validation->set_rules('password', 'password', 'required|trim|xss_clean');
if ($this->form_validation->run() == FALSE) {
$this->load->view('Login');
# code...
}
else {
$username = $this->input->post('username');
$password = $this->input->post('password');
$check = $this->M_login->check($username, $password);
// session data
if ($check->num_rows() == TRUE) {
foreach ($check->result() as $value) {
$sess_data['id'] = $value->id;
$sess_data['name'] = $value->name;
$sess_data['username'] = $value->username;
$sess_data['password'] = $value->password;
$sess_data['description'] = $value->description;
$this->session->set_userdata($sess_data);
}
redirect('Dashboard');
}
else {
$this->session->set_flashdata('result_login', '<br>Invalid username or password, try again.');
redirect('Login');
}
And here is my dashboard controller:
这是我的仪表板控制器:
public function __construct() {
parent::__construct();
if(!$this->session->userdata('id')){
redirect('login');
}
}
public function index() {
// Dashboard view
$username = $this->session->userdata('username');
$data['username']=$username;
$this->load->view('Dashboard', $data);
}
function logout() {
$this->session->sess_destroy('id');
redirect('login');
}
With above code, i can get the username on my dashboard view by echo $username
. But when i change the session data like this:
使用上面的代码,我可以通过echo $username
. 但是当我像这样更改会话数据时:
if ($check->num_rows() == TRUE) {
foreach ($check->result() as $value) {
// session data
$sess_data = array(
'id' => $value->id,
'username' => $value->username,
'password' => $value->password,
'name' => $value->name,
'description' => $value->description
);
$this->session->set_userdata('log',$sess_data);
}
}
And Dashboard controllerchanged like this:
而仪表盘控制器改变这样的:
if(!$this->session->userdata('id')) {
$getdata= $this->session->userdata('log');
$data['username'] = $getdata['username'];
}
$this->load->view('Dashboard', $data);
Then the username disappeared from view page. How can i store username in session array and call it in view page. Please share your better suggestions or your experience guys.
然后用户名从视图页面中消失了。如何将用户名存储在会话数组中并在视图页面中调用它。请分享您更好的建议或您的经验。
Thanks.
谢谢。
回答by Ajay
Adding data in session :-
在会话中添加数据:-
$newdata = array(
'username' => 'johndoe',
'email' => '[email protected]',
'logged_in' => TRUE
);
$this->session->set_userdata($newdata);
Retrieving Data from session :-
从会话中检索数据:-
$this->session->all_userdata()
Retrieving single data :-
检索单个数据:-
$session_id = $this->session->userdata('username');
回答by Angel
Youcan refer this code
你可以参考这个代码
public function loginaction()
{
$this->load->library('session');
$a = $this->input->post('email');
$b = trim($this->input->post('password'));
$b1 = md5($b);
$c = 1;
$data = $this->userdata->userlogin($a,$b1,$c);
if($data)
{
echo true;
foreach ($data as $login)
{
$uid=$this->session->set_userdata('logId',$login->usr_id);
}
}
else
{
echo "Invalid username or password..!!!!";
$a=$this->input->post('email');
}
}
回答by Praveen Kumar
You can do it like
你可以这样做
$username = $this->input->post('username');
$password = $this->input->post('password');
$check = $this->M_login->check($username, $password);
if ($check->num_rows() > 0)
{
$value = $check->row();
$sess_data['id'] = $value->id;
$sess_data['name'] = $value->name;
$sess_data['username'] = $value->username;
$sess_data['password'] = $value->password;
$sess_data['description'] = $value->description;
$this->session->set_userdata($sess_data);
redirect('Dashboard');
}
else
{
$this->session->set_flashdata('result_login', '<br>Invalid username or password, try again.');
redirect('Login');
}
And in your view
在你看来
$username = $this->session->userdata('username');