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
Related
//setting session data
$loginData = array(
'name' => 'Rajeev Singh',
'email' => 'rajeev#gmail.com',
'age' => '21',
);
$this->session->set_userdata('loginData',$loginData);
//accessing session data
$name = $this->session->userdata('loginData')['name'];
suppose if my users want to update his/her details then I need to change in session
but I'm unable to update session value
currently what I'm doing is creating a whole new session which changes my session ID
I wanted to update one value of my session without changing session ID
//accessing session ID
$sessionID = $this->session->session_id;
This will update your session variable 'loginData' using Codeigniter without destroying the session.
I'm using $data_update where your custom variable would go.
$this->session->set_userdata('loginData', $data_update);
Codeigniter Sessions page
[1] http://codeigniter.com/user_guide/libraries/sessions.html
Just overwrite the sessions
//initial set
loginData = array(
'name' => 'Rajeev Singh',
'email' => 'rajeev#gmail.com',
'age' => '21',
);
$this->session->set_userdata('loginData', $loginData);
//overwritting
$loginData = array(
'name' => 'new name',
'email' => 'new email',
'age' => 'new age',
);
$this->session->set_userdata('loginData', $loginData);
Take look at here.
print_r($_SESSION["logindata"]);
$_SESSION["logindata"]["name"]="updated name";
print_r($_SESSION["logindata"]);
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);
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
I have a form with 8 input fields. Now I don't want to update a field in the database if it's left empty.
This are the fields that I like to check. if empty do not update them and leave the original value. How to do this?
This is my function in my controller
function update_profile(){
$data = array(
'name' => $this->input->post('name'),
'email' => $this->input->post('email'),
'telefoon' => $this->input->post('telefoon'),
'gsm' => $this->input->post('gsm'),
'facebook' => $this->input->post('facebook'),
'twitter' => $this->input->post('twitter'),
'portfolio' => $this->input->post('portfolio'),
'profilefoto' => $this->input->post('browse')
);
$this->kdg_model->update_profile($data);
}
My model
function update_profile($data) {
$session_id = $this->session->userdata('user');
$this->db->where('user', $session_id);
$this->db->update('user', $data);
}
Just remove the field from your main array and check it in a different way.
Let's assume that this is your $data array:
$data = array(
'naam' => $this->input->post('naam'),
'email' => $this->input->post('email'),
'telefoon' => $this->input->post('telefoon'),
'gsm' => $this->input->post('gsm'),
'facebook' => $this->input->post('facebook'),
'twitter' => $this->input->post('twitter'),
'portfolio' => $this->input->post('portfolio'),
'profielfoto' => $this->input->post('browse')
);
and about not_update_if_blank, all you need to do is check it after the $data array:
if( $this->input->post('not_update_if_blank') != "" )
{
$data['not_update_if_blank'] = $this->input->post('not_update_if_blank');
}
now you can pass $data to your model.
EDIT:
$post_array = $this->input->post();
foreach( $post_array as $key=>$value )
{
if(trim($value)!= "")
{
$data[$key] = $value;
}
}
now pass $data to your model.
NB: test the code because I haven't tested it!
First of all the best solution near me is To Validate the form, but if you still want to avoid validation than go like this: Its the simplest way, not good but simplest:
E.g
if($name!='' && $email!='' && $facebook!='') //place all your Post variables here same like these 3 variables.
{
//Perform your Updation process here.
}