6

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.

4

5 回答 5

13

会话值未在文件中设置。如果要设置会话变量,请使用

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

并检索您刚刚访问的值

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

例如,要回显它,请使用

echo $this->session->data['variable_name_here'];
于 2011-12-12T11:40:05.317 回答
2

没有保存会话变量的文件。打开购物车会话是使用“system/library/Session.php”创建的。您可以在打开的购物车中创建这样的会话。

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

现在您可以像这样在打开的购物车中的任何位置调用此会话。

<?php
     echo $this->session->data['session_name'];
?>
于 2015-04-14T11:02:27.967 回答
2

在这里,我会将变量保存到会话中:

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

现在在catalog/controller/checkout/guest.php方法中index检查该会话变量,如果设置,将值存储在$this->data数组中以呈现给模板:

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'];
}

之后,您可以简单地在您的模板中回显这些值(仍在检查是否存在):

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

现在你应该在避免任何undefined variable通知的同时完成......

于 2014-03-13T04:03:57.547 回答
1

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

于 2012-10-07T09:41:00.080 回答
0

/system/library/customer.php 确实包含 $this->session->data['customer_id'];

于 2014-03-13T03:31:27.693 回答