user984689
user984689

Reputation:

Session Variables in Opencart

Can someone explain where session variables are held?

I have added some session variables in header.php in the controller, for example:

$this->session->data['day']=date("d",strtotime($row['startdate']));

This works when loading the site, and when I click on a product, all the variables are gone, except for the [language], [currency] and [cart] which are set by Opencart.

I guess there is another file or controller file where I set the variables, or where [language], [currency] and [cart] are set but I cannot find it.

Thanks in advance.

Upvotes: 6

Views: 40882

Answers (5)

Raja Usman Mehmood
Raja Usman Mehmood

Reputation: 653

There is no file that held the session variables. Open cart session are created by using "system/library/Session.php". You can create session like this in open cart.

<?php
     $this->session->data['session_name'] = 'session value';
?>

Now you can call this session in any where in open cart like this.

<?php
     echo $this->session->data['session_name'];
?>

Upvotes: 2

Ravi Patel
Ravi Patel

Reputation: 5211

Here I would save the variables into a session:

public function receive() {
    $this->session->data['guest_name'] = $this->request->post['name'];
    $this->session->data['guest_address'] = $this->request->post['address'];
}

Now in catalog/controller/checkout/guest.php at index method check for that session variables and if set, store the value in the $this->data array for presenting to the template:

if(isset($this->session->data['guest_name'])) { // it is enough to check only for one variable and only if it is set
    $this->data['guest_name'] = $this->session->data['guest_name'];
    $this->data['guest_address'] = $this->session->data['guest_address'];
}

After that You can simply echo these values in Your template (still checking whether exists):

<?php if(isset($guest_name)) { ?>
<div><?php echo $guest_name . ' - ' . $guest_address; ?></div>
<?php } ?>

Now You should be done while avoiding any undefined variable notices...

Upvotes: 3

Khan Shahrukh
Khan Shahrukh

Reputation: 6361

/system/library/customer.php does contain $this->session->data['customer_id'];

Upvotes: 0

spiralclick
spiralclick

Reputation: 56

I think i bit late but the main class which handle the sessions is in the system/library/session.php, which have public variable $data and handling the $_SESSION in the constructor. so what ever you put in the $this->session->data it merge.

Hope it will be beneficial.

thanks

Upvotes: 1

Jay Gilford
Jay Gilford

Reputation: 15151

Session values are not set in a file. If you want to set a session variable, use

$this->session->data['variable_name_here'] = 'data value here';

and to retrieve the value you just access

$this->session->data['variable_name_here']

For example, to echo it use

echo $this->session->data['variable_name_here'];

Upvotes: 13

Related Questions