How to keep a session alive after logging in (Codeigniter) - php

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.

Related

how to set different sessions for user and admin | CodeIgniter

hello i am working on a project where user and admin have different logins
I want to set different sessions for user and admin
here is I how set admin session
$this->session->set('adminData', [
'id' => $teacher['id'],
'name' => $teacher['name'],
'email' => $teacher['email'],
'new_email' => $teacher['new_email']
]);
user's session data as follows
$this->session->set('userData', [
'id' => $teacher['id'],
'name' => $teacher['name'],
'email' => $teacher['email'],
'new_email' => $teacher['new_email']
]);
but when validating user
if ($this->session->isLoggedIn) {
return redirect()->to('admin');
}
the only problem I am facing that either I can login as admin or user not both
when I try to login as user when I am already logged as admin, it redirects me to user/account.
Is there any method that i can set isAdminLoggedIn for admin and can use isLoggedIn for user?
you can set isAdminLoggedIn before or after adminData.
$this->session->set('isAdminLoggedIn', true);
and can destroy the session when you logged out
$this->session->remove(['isAdminLoggedIn']);
or together with
$this->session->remove(['isAdminLoggedIn', 'adminData']);

How do i update CI session without destroying it

//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"]);

How to add shopping cart contents in cookie in codeigniter?

I have situation where i want to store my whole cart into cookie so that if accidently user closed browser, cart contains the same contents using that cookie.
Right now, i am using codeigniter cart class to contain all the products user added to cart.
//array for product details...
$insert_data = array(
'id' => $this->input->post('id'),
'name' => $this->input->post('name'),
'price' => $this->input->post('price'),
'image' => $this->input->post('image'),
'qty' => 1
);
//add previously defined data to cart....
$this->cart->insert($insert_data);
//assigning whole cart to cookie
$this->input->set_cookie('cart_cookie', $this->cart->contents(),36000);
But here, set_cookie method is giving error as it is expecting second parameter to be string...But i want whole cart to be stored..
Any solution with this problem?
Or is there any better solution than cookie?

A row associates with session is deleted when user logout in YII

I have a site that uses YII. One thing that it does is storing session information into a MySql database table YiiSession. Also in a separate table (users_sessions), a new row is inserted with the following information:
session id
user id
online status of a user
I create another table for session because YiiSession is part of YII. Also users_session keeps track online status of a user, whereas YiiSession doesn't.
I only make an insertion into users_session during user login.
But I don't understand that when user logs out, the session that associates with the user got deleted.
Note that the session_id in the users_session is a foreign key to the one in YiiSession. But the one in YiiSession still exists even though it has an expiration date.
What mechanism that possibly deletes the row? Please help.
You should extend CDbHttpSession and overwrite the method destroySession($id). There you append the code and delete your entry also.
Configure Yii to use your session class instead of it's own in config/main.php:
'session' => array(
'autoStart' => true,
'class' => 'application.components.<your session class name>',
'connectionID' => 'db',
'sessionTableName' => 'session',
'autoCreateSessionTable' => false,
'timeout' => 3600, // 60 min
),
However I would not do this with a separate table, just add some fields to the YiiSession table. That's what I did and it works pretty well.
EDIT:
You should create a file named MyDbHttpSession.php in protected/components.
In this file extend CDbHttpSession like so:
class MyDbHttpSession extends CDbHttpSession
{
protected function createSessionTable($db, $tableName)
{
// basically this is a copy of CDbHttpSession
$db->createCommand()->createTable($tableName, array(
'id' => 'CHAR(32) PRIMARY KEY',
'expire' => 'integer',
'data' => $blob,
// your additional fields
'idUser' => 'INT(10) NULL',
'online' => 'TINYINT(1)',
// more of your fields here if you have
));
}
// this sets the user ID by session ID
public function setIdUser($idUser)
{
$db = $this->getDbConnection();
$db->setActive(true);
$db->createCommand()->update($this->sessionTableName, array('idUser' => (int) $idUser),
'id=:id', array(':id' => $this->sessionId)
);
}
// and this sets the online status, pass true or false to $online
public function setOnline($online)
{
$db = $this->getDbConnection();
$db->setActive(true);
$db->createCommand()->update($this->sessionTableName, array('online' => (int) $online),
'id=:id', array(':id' => $this->sessionId)
);
}
}
Set Yii to use your session class instead of it's own like I wrote above.
You should already have a class WebUser. If not, create one in protected/components and extend CWebUser.
In this class add a method afterLogin:
public function afterLogin($fromCookie)
{
// store user ID and online status in session table
Yii::app()->session->setIdUser($this->id);
Yii::app()->session->setOnline(true);
return parent::afterLogin($fromCookie);
}
You can from wherever you are in Yii use Yii::app()->session->setOnline(true); or Yii::app()->session->setOnline(false);

Opencart adding a new field to the customer view of the admin section

I have added a new field to my database. I also have managed to add necessary codes and functions in catalog section. This new field is related to the customer. The data related to this new field gets added successfully to the database.
This new field belongs to Customer table.
Now, I want to know, when viewing customer's details in the admin section, how this new field should be retrieved from database? I mean which file should be edited for this purpose?
getCustomer($customer_id) and getCustomers($data = array()) are the functions used to get customer data.
Since they are SELECT * querys your field is being processed automatically.
Afterwards you need to go in the Controller section in the controller\sale folder and there you have customer.php, custom_field.php and and edit the ones that you need. For example:
$this->data['customers'][] = array(
'customer_id' => $result['customer_id'],
'name' => $result['name'],
'email' => $result['email'],
'customer_group' => $result['customer_group'],
'status' => ($result['status'] ? $this->language->get('text_enabled') : $this->language->get('text_disabled')),
'approved' => ($result['approved'] ? $this->language->get('text_yes') : $this->language->get('text_no')),
'ip' => $result['ip'],
'date_added' => date($this->language->get('date_format_short'), strtotime($result['date_added'])),
'selected' => isset($this->request->post['selected']) && in_array($result['customer_id'], $this->request->post['selected']),
'action' => $action
);
add your field in this array (this is from customer.php).
And finally edit the .tpl files that are called from the view\template\sale folder so they appear where they want them to.
Hope i was clear enough.

Categories