OpenCart – Sessions

Every time that a user goes to your website and begins to shop there are many processes running in the background that are critical in your users shopping experience. For example, when someone adds an item to their cart that item has to be stored somewhere. Without it being stored in the Database (yet), it is stored rather in what is called a Session Variable. If it wasn’t stored then when they would go to a different page the item would be gone. PHP has a function called session_start() which is executed immediately. You can count on this being YOUR PERSONAL SESSION for as long as you are surfing the website or until it is killed. Some administrators have a timer set up for inactive users where the session will be destroyed if inactivity passes a certain limit. From the session_start() there are many many other session variables that are executed in a session. For example let’s take the shopping cart for example. In the file system/library/cart.php you can find this line of code:

if (!isset($this->session->data['cart']) || !is_array($this->session->data['cart'])) {
      		$this->session->data['cart'] = array();
    	}
This basically is saying that if there is no session present called 'cart', then create one. Rather, this is an array stored in a session variable with the name of 'cart'. This cart session array can now be added to and can store a large amount of data including all option data for a specific item(s).  Think of the session as a holding container for the array. The array is what is manipulated by adding and removing store items and the session is what allows it to be accessed again and again from page to page.

In the same cart.php file you can see here how we iterate through the cart session array to gather all product data:
<pre class="brush: actionscript3; gutter: true">public function getProducts() {
		if (!$this-&gt;data) {
			foreach ($this-&gt;session-&gt;data['cart'] as $key =&gt; $quantity) {
				$product = explode(':', $key);
				$product_id = $product[0];
				$stock = true;
// Options
if (!empty($product[1])) {
$options = unserialize(base64_decode($product[1]));
} else {
$options = array();
}

// Profile

if (!empty($product[2])) {
$profile_id = $product[2];
} else {
$profile_id = 0;
}

$product_query = $this->db->query("SELECT * FROM " . DB_PREFIX . "product p LEFT JOIN " . DB_PREFIX . "product_description pd ON (p.product_id = pd.product_id) WHERE p.product_id = '" . (int)$product_id . "' AND pd.language_id = '" . (int)$this->config->get('config_language_id') . "' AND p.date_available <= NOW() AND p.status = '1'");

if ($product_query->num_rows) {
$option_price = 0;
$option_points = 0;
$option_weight = 0;

$option_data = array();

foreach ($options as $product_option_id => $option_value) {
$option_query = $this->db->query("SELECT po.product_option_id, po.option_id, od.name, o.type FROM " . DB_PREFIX . "product_option po LEFT JOIN `" . DB_PREFIX . "option` o ON (po.option_id = o.option_id) LEFT JOIN " . DB_PREFIX . "option_description od ON (o.option_id = od.option_id) WHERE po.product_option_id = '" . (int)$product_option_id . "' AND po.product_id = '" . (int)$product_id . "' AND od.language_id = '" . (int)$this->config->get('config_language_id') . "'");

if ($option_query->num_rows) {
if ($option_query->row['type'] == 'select' || $option_query->row['type'] == 'radio' || $option_query->row['type'] == 'image') {
$option_value_query = $this->db->query("SELECT pov.option_value_id, ovd.name, pov.quantity, pov.subtract, pov.price, pov.price_prefix, pov.points, pov.points_prefix, pov.weight, pov.weight_prefix FROM " . DB_PREFIX . "product_option_value pov LEFT JOIN " . DB_PREFIX . "option_value ov ON (pov.option_value_id = ov.option_value_id) LEFT JOIN " . DB_PREFIX . "option_value_description ovd ON (ov.option_value_id = ovd.option_value_id) WHERE pov.product_option_value_id = '" . (int)$option_value . "' AND pov.product_option_id = '" . (int)$product_option_id . "' AND ovd.language_id = '" . (int)$this->config->get('config_language_id') . "'");

if ($option_value_query->num_rows) {
if ($option_value_query->row['price_prefix'] == '+') {
$option_price += $option_value_query->row['price'];
} elseif ($option_value_query->row['price_prefix'] == '-') {
$option_price -= $option_value_query->row['price'];
}

if ($option_value_query->row['points_prefix'] == '+') {
$option_points += $option_value_query->row['points'];
} elseif ($option_value_query->row['points_prefix'] == '-') {
$option_points -= $option_value_query->row['points'];
}

if ($option_value_query->row['weight_prefix'] == '+') {
$option_weight += $option_value_query->row['weight'];
} elseif ($option_value_query->row['weight_prefix'] == '-') {
$option_weight -= $option_value_query->row['weight'];
}

if ($option_value_query->row['subtract'] && (!$option_value_query->row['quantity'] || ($option_value_query->row['quantity'] < $quantity))) {
$stock = false;
}

$option_data[] = array(
'product_option_id' => $product_option_id,
'product_option_value_id' => $option_value,
'option_id' => $option_query->row['option_id'],
'option_value_id' => $option_value_query->row['option_value_id'],
'name' => $option_query->row['name'],
'option_value' => $option_value_query->row['name'],
'type' => $option_query->row['type'],
'quantity' => $option_value_query->row['quantity'],
'subtract' => $option_value_query->row['subtract'],
'price' => $option_value_query->row['price'],
'price_prefix' => $option_value_query->row['price_prefix'],
'points' => $option_value_query->row['points'],
'points_prefix' => $option_value_query->row['points_prefix'],
'weight' => $option_value_query->row['weight'],
'weight_prefix' => $option_value_query->row['weight_prefix']
);
}
} elseif ($option_query->row['type'] == 'checkbox' && is_array($option_value)) {
foreach ($option_value as $product_option_value_id) {
$option_value_query = $this->db->query("SELECT pov.option_value_id, ovd.name, pov.quantity, pov.subtract, pov.price, pov.price_prefix, pov.points, pov.points_prefix, pov.weight, pov.weight_prefix FROM " . DB_PREFIX . "product_option_value pov LEFT JOIN " . DB_PREFIX . "option_value ov ON (pov.option_value_id = ov.option_value_id) LEFT JOIN " . DB_PREFIX . "option_value_description ovd ON (ov.option_value_id = ovd.option_value_id) WHERE pov.product_option_value_id = '" . (int)$product_option_value_id . "' AND pov.product_option_id = '" . (int)$product_option_id . "' AND ovd.language_id = '" . (int)$this->config->get('config_language_id') . "'");

if ($option_value_query->num_rows) {
if ($option_value_query->row['price_prefix'] == '+') {
$option_price += $option_value_query->row['price'];
} elseif ($option_value_query->row['price_prefix'] == '-') {
$option_price -= $option_value_query->row['price'];
}

if ($option_value_query->row['points_prefix'] == '+') {
$option_points += $option_value_query->row['points'];
} elseif ($option_value_query->row['points_prefix'] == '-') {
$option_points -= $option_value_query->row['points'];
}

if ($option_value_query->row['weight_prefix'] == '+') {
$option_weight += $option_value_query->row['weight'];
} elseif ($option_value_query->row['weight_prefix'] == '-') {
$option_weight -= $option_value_query->row['weight'];
}

if ($option_value_query->row['subtract'] && (!$option_value_query->row['quantity'] || ($option_value_query->row['quantity'] < $quantity))) {
$stock = false;
}

$option_data[] = array(
'product_option_id' => $product_option_id,
'product_option_value_id' => $product_option_value_id,
'option_id' => $option_query->row['option_id'],
'option_value_id' => $option_value_query->row['option_value_id'],
'name' => $option_query->row['name'],
'option_value' => $option_value_query->row['name'],
'type' => $option_query->row['type'],
'quantity' => $option_value_query->row['quantity'],
'subtract' => $option_value_query->row['subtract'],
'price' => $option_value_query->row['price'],
'price_prefix' => $option_value_query->row['price_prefix'],
'points' => $option_value_query->row['points'],
'points_prefix' => $option_value_query->row['points_prefix'],
'weight' => $option_value_query->row['weight'],
'weight_prefix' => $option_value_query->row['weight_prefix']
);
}
}
} elseif ($option_query->row['type'] == 'text' || $option_query->row['type'] == 'textarea' || $option_query->row['type'] == 'file' || $option_query->row['type'] == 'date' || $option_query->row['type'] == 'datetime' || $option_query->row['type'] == 'time') {
$option_data[] = array(
'product_option_id' => $product_option_id,
'product_option_value_id' => '',
'option_id' => $option_query->row['option_id'],
'option_value_id' => '',
'name' => $option_query->row['name'],
'option_value' => $option_value,
'type' => $option_query->row['type'],
'quantity' => '',
'subtract' => '',
'price' => '',
'price_prefix' => '',
'points' => '',
'points_prefix' => '',
'weight' => '',
'weight_prefix' => ''
);
}
}
}

if ($this->customer->isLogged()) {
$customer_group_id = $this->customer->getCustomerGroupId();
} else {
$customer_group_id = $this->config->get('config_customer_group_id');
}

$price = $product_query->row['price'];

// Product Discounts
$discount_quantity = 0;

foreach ($this->session->data['cart'] as $key_2 => $quantity_2) {
$product_2 = explode(':', $key_2);

if ($product_2[0] == $product_id) {
$discount_quantity += $quantity_2;
}
}

$product_discount_query = $this->db->query("SELECT price FROM " . DB_PREFIX . "product_discount WHERE product_id = '" . (int)$product_id . "' AND customer_group_id = '" . (int)$customer_group_id . "' AND quantity <= '" . (int)$discount_quantity . "' AND ((date_start = '0000-00-00' OR date_start < NOW()) AND (date_end = '0000-00-00' OR date_end > NOW())) ORDER BY quantity DESC, priority ASC, price ASC LIMIT 1");

if ($product_discount_query->num_rows) {
$price = $product_discount_query->row['price'];
}

// Product Specials
$product_special_query = $this->db->query("SELECT price FROM " . DB_PREFIX . "product_special WHERE product_id = '" . (int)$product_id . "' AND customer_group_id = '" . (int)$customer_group_id . "' AND ((date_start = '0000-00-00' OR date_start < NOW()) AND (date_end = '0000-00-00' OR date_end > NOW())) ORDER BY priority ASC, price ASC LIMIT 1");

if ($product_special_query->num_rows) {
$price = $product_special_query->row['price'];
}

// Reward Points
$product_reward_query = $this->db->query("SELECT points FROM " . DB_PREFIX . "product_reward WHERE product_id = '" . (int)$product_id . "' AND customer_group_id = '" . (int)$customer_group_id . "'");

if ($product_reward_query->num_rows) {
$reward = $product_reward_query->row['points'];
} else {
$reward = 0;
}

// Downloads
$download_data = array();

$download_query = $this->db->query("SELECT * FROM " . DB_PREFIX . "product_to_download p2d LEFT JOIN " . DB_PREFIX . "download d ON (p2d.download_id = d.download_id) LEFT JOIN " . DB_PREFIX . "download_description dd ON (d.download_id = dd.download_id) WHERE p2d.product_id = '" . (int)$product_id . "' AND dd.language_id = '" . (int)$this->config->get('config_language_id') . "'");

foreach ($download_query->rows as $download) {
$download_data[] = array(
'download_id' => $download['download_id'],
'name' => $download['name'],
'filename' => $download['filename'],
'mask' => $download['mask'],
'remaining' => $download['remaining']
);
}

// Stock
if (!$product_query->row['quantity'] || ($product_query->row['quantity'] < $quantity)) {
$stock = false;
}

$recurring = false;
$recurring_frequency = 0;
$recurring_price = 0;
$recurring_cycle = 0;
$recurring_duration = 0;
$recurring_trial_status = 0;
$recurring_trial_price = 0;
$recurring_trial_cycle = 0;
$recurring_trial_duration = 0;
$recurring_trial_frequency = 0;
$profile_name = '';

if ($profile_id) {
$profile_info = $this->db->query("SELECT * FROM `" . DB_PREFIX . "profile` `p` JOIN `" . DB_PREFIX . "product_profile` `pp` ON `pp`.`profile_id` = `p`.`profile_id` AND `pp`.`product_id` = " . (int) $product_query->row['product_id'] . " JOIN `" . DB_PREFIX . "profile_description` `pd` ON `pd`.`profile_id` = `p`.`profile_id` AND `pd`.`language_id` = " . (int) $this->config->get('config_language_id') . " WHERE `pp`.`profile_id` = " . (int) $profile_id . " AND `status` = 1 AND `pp`.`customer_group_id` = " . (int) $customer_group_id)->row;

if ($profile_info) {
$profile_name = $profile_info['name'];

$recurring = true;
$recurring_frequency = $profile_info['frequency'];
$recurring_price = $profile_info['price'];
$recurring_cycle = $profile_info['cycle'];
$recurring_duration = $profile_info['duration'];
$recurring_trial_frequency = $profile_info['trial_frequency'];
$recurring_trial_status = $profile_info['trial_status'];
$recurring_trial_price = $profile_info['trial_price'];
$recurring_trial_cycle = $profile_info['trial_cycle'];
$recurring_trial_duration = $profile_info['trial_duration'];
}
}

$this->data[$key] = array(
'key' => $key,
'product_id' => $product_query->row['product_id'],
'name' => $product_query->row['name'],
'model' => $product_query->row['model'],
'shipping' => $product_query->row['shipping'],
'image' => $product_query->row['image'],
'option' => $option_data,
'download' => $download_data,
'quantity' => $quantity,
'minimum' => $product_query->row['minimum'],
'subtract' => $product_query->row['subtract'],
'stock' => $stock,
'price' => ($price + $option_price),
'total' => ($price + $option_price) * $quantity,
'reward' => $reward * $quantity,
'points' => ($product_query->row['points'] ? ($product_query->row['points'] + $option_points) * $quantity : 0),
'tax_class_id' => $product_query->row['tax_class_id'],
'weight' => ($product_query->row['weight'] + $option_weight) * $quantity,
'weight_class_id' => $product_query->row['weight_class_id'],
'length' => $product_query->row['length'],
'width' => $product_query->row['width'],
'height' => $product_query->row['height'],
'length_class_id' => $product_query->row['length_class_id'],
'profile_id' => $profile_id,
'profile_name' => $profile_name,
'recurring' => $recurring,
'recurring_frequency' => $recurring_frequency,
'recurring_price' => $recurring_price,
'recurring_cycle' => $recurring_cycle,
'recurring_duration' => $recurring_duration,
'recurring_trial' => $recurring_trial_status,
'recurring_trial_frequency' => $recurring_trial_frequency,
'recurring_trial_price' => $recurring_trial_price,
'recurring_trial_cycle' => $recurring_trial_cycle,
'recurring_trial_duration' => $recurring_trial_duration,
);
} else {
$this->remove($key);
}
}
}

return $this->data;
}

You can kill a session just as fast as you created it by declaring the following:

unset($this->session->data['cart']); 

Or if you want to destroy ALL data related to the browser session then do this:

session_destroy();

Bonus Trick

If you are not happy with how long an administrator or user is allowed to be in the current session then you can adjust the timing by going to the following file: system/library/session.php and find the following line:

ini_set('session.use_trans_sid', 'Off');

Below that add this line (adjusting the number to your liking):

@ini_set('session.gc_maxlifetime',1440);

Looking for quality OpenCart Hosting? Look no further than Arvixe Web Hosting!

Tags: , , , , , , , , | Posted under OpenCart | RSS 2.0

Author Spotlight

Joe Stenhouse

Joe Stenhouse

I am a web application developer that specializes in PHP, JAVASCRIPT, MYSQL, HTML, and CSS. We manifest exciting potentials within the world wide web through means of innovation.

Leave a Reply

Your email address will not be published. Required fields are marked *


7 × 7 =

You may use these HTML tags and attributes: <a href="" title="" rel=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>