So $token here is an array of data from the database, I would like to retrieve it in the view. I know that the data is already there by using print_r($this->session->all_userdata()); but I would like to retrieve the values from the $token array and use it.
Controller:
$token['answer']=$this->Qmodel->get_answers();
$data= array(
'username' => $this->input->post('username'),
'is_logged'=> 1,
$token
);
$this->session->set_userdata($data);
You can try with this one,I hope it will help you
$token['answer']=$this->Qmodel->get_answers();
$data= array(
'username' => $this->input->post('username'),
'is_logged'=> 1,
'token' => $token['answer'] // assign $token['answer'] array into token
);
$this->session->set_userdata($data);
get $token['answer'] array data from session:
$token_from_session = $this->session->userdata('token'); //return $token['answer'] array
Related
I have a controller that is used to create a user login session with the following code:
public function account(){
$data = array();
if($this->session->userdata('isUserLoggedIn')){
$data['user'] = $this->user->getRows(array('id'=>$this->session->userdata('userId')));
//Create session
$newdata = array(
'username' => $user['name'],
'email' => $user['email'],
);
$this->session->set_userdata($newdata);
//load the view
$this->load->view('templates/header', $data);
$this->load->view('pages/home', $data);
$this->load->view('templates/footer', $data);
//redirect('pages/view/');
}else{
redirect('users/login');
}
}
Problem occurs when I try to check from this or any other of my controllers the session data and it always returns false. Not sure what it is that Im doing wrong. If I pass this
$data['user']
to a View as data, Im able to access the name and email, but I cant access it on the controller
If I understand correctly I believe your problem is here.
$newdata = array(
'username' => $user['name'],
'email' => $user['email'],
);
The variable $user has not been set in the controller, at least not in the code you show. I think this is what you need
$newdata = array(
'username' => $data['user']['name'],
'email' => $data['user']['email'],
);
A suggestion, not related to your problem, regarding getting session data. With CI >= v3.0.0 you can obtain an item of session data like this.
$this->session->isUserLoggedIn;
The code behind this method is smaller and you type less to get the same return that $this->session->userdata('isUserLoggedIn') provides.
userdata() is a legacy method kept only for backwards compatibility with older applications.
First, you need store session data
`
$sedata = array(
'user' => 1,
'username' => $username,
'userid' => $dbrow->id,
'mobile_phone' => $dbrow->mobile_phone,
'success' => 'Successfully loggedin!',
'logged_in' => TRUE
);
$this->session->set_userdata($sedata);
**Now you get session data like :**
$this->session->userdata('user');
$this->session->userdata('userid');
`
I'm using Phalcon PHP and I want to add another item to my session after it's created. I have this :
private function _registerSession($user, $account) {
$this->session->set('auth', array(
'user_id' => $user->id,
'username' => $user->name
)); }
In another controller I want to edit this session for example :
$auth = $this->session->get('auth');
$auth->add('account_id', '10');
And this session will content the 3 variables as :
$this->session->set('auth', array(
'user_id' => $user->id,
'username' => $user->name,
'account_id' => 10
)); }
But I don't know how I can dot that.
Yo need to do it in following manner:-
$auth = $this->session->get('auth'); // get auth array from Session
$auth['account_id']= '10'; // add new index value pair to it
$this->session->set('auth',$auth); // reassign it to auth index of Session
This should work:
$auth = $this->session->get("auth");
$this->session->set("auth", array_merge($auth, array('account_id'=>'10')));
i think you can use like this :-
$auth = $this->session->get('auth');
$auth['account_id']= 10;
$this->session->set('auth',$auth);
private function _registerSession($user, $account) {
$this->session->set_userdata('auth', array(
'user_id' => $user->id,
'username' => $user->name
)); }
// You should use the following code to set one more parameter in sesssion:
$this->session->set_userdata('auth', array(
'user_id' => $this->session_userdata('user_id'),
'username' => $this->session_userdata('username'),
'account_id' => 10
));
Phalcon's session code is simply a wrapper around $_SESSION. The simplest solution is to avoid using Phalcon functions:
$_SESSION['auth']->add('account_id',10);
I wanna to know how to catch session id from device/client.
I have a Login API, the logic is like this. When client login to the apps they are also get an session id. the code is below:
public function user_post()
{
$data = array (
'username' => $this->input->get_post('username'),
'password' => sha1($this->input->get_post('password'))
);
$result = $this->login_m->user_check($data);
if ($result ) {
foreach ($result as $row ) {
$sess_array = array(
'username' => $row->username,
'email' => $row->email,
'uniqueID' => $row->uniqueID,
'userid' => $row->userid,
'session_id' => session_id()
);
$this->session->set_userdata('logged', $sess_array);
$this->response(array('success' => $this->session->userdata('logged') ));
}
} else {
$this->response(array(404 => 'missing parameter'));
}
}
it also has an array init. and the json response would like this below:
{
"success": {
"username": "johndoe123",
"email": "myemail#gmail.com",
"uniqueID": "1q2w3e4r5t6y",
"userid": "97",
"session_id": "1bbfdj07as68bvp3ctrboj8gs7"
}
}
the question is, how i to catch the session id that is store in client apps?
so, when they are trying to get some information like news, or their profile. The client apps send me back the session id.
I presume that session id has array data just like when they are try to login.
so maybe i will get the array, array value will be the condition to get the news or profile.
CMIIW
Try this
$this->session->userdata('session_id');
$session_data=$this->session->userdata('logged');
use this to get all the session data and you can get the particular data by iterating that array
like this $session_data['session_id']
I would set a session value in codeigniter
$sess_array = array(
'id' => $row->Login_Id,
'username' => $row->Login_Name,
'postid'=> $row->Fk_Post_Id,
);
$CI->session->set_userdata('logged_in', $sess_array);
Then how to retrieve a particular (say - id) session value . I tried these
$createdby=$this->session->userdata('id');
$createdby=$this->session->userdata($logged_in['id']);
but fails.
Use the array syntax for this. Like,
$this->session->userdata['logged_in']['id'];
set it like this
$sess_array = array(
'id' => $row->Login_Id,
'username' => $row->Login_Name,
'postid' => $row->Fk_Post_Id,
);
$CI->session->set_userdata($sess_array);
To retrieve
$createdby = $this->session->userdata('id');
$createdby = $this->session->userdata(logged_in['id']);
or
$createdby = echo $_SESSION[logged_in['id']];
$sess_array = array(
'id' => $row->Login_Id,
'username' => $row->Login_Name,
'postid'=> $row->Fk_Post_Id,
);
$CI->session->set_userdata('logged_in', $sess_array);
then
$logged_in = $this->session->userdata('logged_in');
$createdby = $logged_in['id'];
Session write process. session content could be dynamic value
$logged_in = [
'id'=> 1,
'name'=> 'test'
];
Particular Session retrieve process.
$this->session->userdata['logged_in']['id'];
$pdata = array(
'id' => $row->Login_Id,
'username' => $row->Login_Name,
); // pass ur data in the array
$this->session->set_userdata('session_data', $pdata); //Sets the session
$pdata = $this->session->userdata('session_data'); //Retrive ur session
$pdata['id'] will give u the corresponding id
In newer version, you can simply use
$this->session->username
$this->session->id
$this->session->postid
I am using Code igniter for my application. I have three controllers and three models in may application homepage,dashboard and settings.I have started the session in homepage model and wanted destroy it in dashboard controller. here is my code
foreach($query->result(as $row) {
$info = array(
'loginid' => $row->loginid,
'firstname' => $row->firstname,
'emailid' => $row->emailid,
'logged_in' => TRUE
);
}
$this->session->set_userdata($info);
but my session array is not getting displyed in dashboard controller.
How I should destroy the session?
Try this:
$this->session->sess_destroy();
You destroy the session with:
$this->session->sess_destroy();
as per the codeigniter session documentation.
That said, I'm not sure how it's related to the code you posted and I'm pretty sure it won't work.
foreach($query->result(as $row) Wrong
Please use
foreach($query->result() as $row)
{
$info = array(
'loginid' => $row->loginid ,
'emailid' => row->emailid,
);
}
To display in dashboard controller. Try this
$this->session->set_userdata($info);
Then you can destroy session by
$this->session->sess_destroy();
$this->session->set_userdata([
'logged_in' => TRUE
'loginid' => $row->loginid,
'firstname' => $row->firstname,
'emailid' => $row->emailid,
]);
And in your conntroller to unset like this
$this->session->set_userdata([
'logged_in' => false,
'loginid' => null
]);