php Opencart 中的会话变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8472027/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Session Variables in Opencart
提问by user984689
Can someone explain where session variables are held?
有人可以解释会话变量的保存位置吗?
I have added some session variables in header.php in the controller, for example:
我在控制器的 header.php 中添加了一些会话变量,例如:
$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.
这在加载站点时有效,当我单击产品时,所有变量都消失了,除了Opencart 设置的[language]
,[currency]
和[cart]
。
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.
我想还有另一个文件或控制器文件,我在其中设置了变量,或者在哪里设置了[language]
,[currency]
和[cart]
,但我找不到它。
Thanks in advance.
提前致谢。
回答by Jay Gilford
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'];
回答by ravi patel
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:
现在在catalog/controller/checkout/guest.php
atindex
方法中检查该会话变量,如果设置,则将值存储在$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'];
}
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...
现在你应该在避免任何undefined variable
通知的情况下完成...
回答by Raja Usman Mehmood
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.
没有保存会话变量的文件。使用“system/library/Session.php”创建打开购物车会话。您可以在打开的购物车中创建这样的会话。
<?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'];
?>
回答by spiralclick
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.
我想我有点晚了,但是处理会话的主类在 system/library/session.php 中,它具有公共变量 $data 并在构造函数中处理 $_SESSION。所以无论你在 $this->session->data 中放入什么,它都会合并。
Hope it will be beneficial.
希望它会是有益的。
thanks
谢谢
回答by Khan Shahrukh
/system/library/customer.php does contain $this->session->data['customer_id'];
/system/library/customer.php 确实包含 $this->session->data['customer_id'];