//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"]);
Related
I was planing to save the search inputs as arrays in session.
if ($this->input->post()) {
$recent_search = array("Country" => $this->input->post('country'),
"State" => $this->input->post('state'),
"Building" => $this->input->post('building'),
);
I have saved the array in session as
$this->session->set_userdata('recentSearch',$recent_search);
The thing is after every form submission I'm taking in new array values. I want to store those input arrays into the session without erasing the old one. Is there any method that I could use?
I suggest you use localstorage or cookies rather than sessions.
Example:
$search_cache = [
'Country' => $this->input->post('country'),
'State' => $this->input->post('state'),
'Building' => $this->input->post('building')
];
setcookie( 'recentSearch', $search_cache , time() + 3600 , '/');
var_export($_COOKIE);
I am tring to get the session information in home page on my localhost but I am not getting it. But getting this information on my server.
In Localhost:-
enter image description here
In Server:-
enter image description here
I am trying to get with following code:-
print_r($this->session);
You can set data to session simply like this in Codeigniter:
$this->load->library('session');
$this->session->set_userdata(array(
'user_id' => $user->uid,
'username' => $user->username,
'groupid' => $user->groupid,
'date' => $user->date_cr,
'serial' => $user->serial,
'rec_id' => $user->rec_id,
'status' => TRUE
));
and you can get it like this:
$u_rec_id = $this->session->userdata('rec_id');
$serial = $this->session->userdata('serial');
We are building a webshop based on Codeigniter. Products are saved in a shopping cart with help of Codeigniters' Cart Class.
When a visitor logs in with it's account, all products which are already in their cart are removed because a new session started when the user logged in.
How can we keep the products in the cart at this point?
if($query->num_rows() == 1)
{
$user = $query->row_array();
$data = array(
'userid' => $user['id'],
'email' => $user['email'],
'validated' => true
);
$this->session->set_userdata($data);
return true;
}
You have to create new table in base, like shoppingcarts, and pass all items from that Chart class in that table, and user ID, then when user log in just query that table with his ID and ID of row now you got all products.But when user bye just delete that rows in base and unset cart class
$data = array(
'id' => 'sku_123ABC',
'qty' => 1,
'price' => 39.95,
'name' => 'T-Shirt',
'options' => array('Size' => 'L', 'Color' => 'Red'
'user_id => '45')
);
$this->cart->insert($data);
$chart = $this->cart->contents();
$this->db->insert('shoppingcarts',$chart);
This is just simple example, how to do that :)
If users have account, you need to save data in DB.
If users don't have an account and you want to keep his chart (based on browser), then you don't need to destroy session when user close the window. You need to keep it on session until he clear his chart. You can do it on config.php file.
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 trying to use user_save() in Drupal 7 to add new accounts to the system.
The function is defined as user_save($account, $edit = array(), $category = 'account').
According to the documentation, the variable $account is a user object. How do I put together a user object that the function can process?
Here is a comment that may help you out a little regarding the user object: User Object. Another way to see what the user object holds is to do:
<?php
global $user;
print_r($user);
?>
Here is a very basic example of creating a new user with user_save():
<?php
$edit = array(
'name' => 'New User',
'pass' => 'password',
'mail' => 'myemail#example.com',
'status' => 1,
'language' => 'en',
'init' => 'myemail#example.com',
'roles' => array(2 => 'authenticated user'),
);
user_save(NULL, $edit);
?>