如何设置和使用Magento的Cookie和Session

如何设置和使用Magento的Cookie和Session,第1张

magento中cookie和session是已经封装了的

Magento的核心对象-Mage_Core_Model_Cookie &Mage_Core_Model_Session

Mage_Core_Model_Cookie,这个对象主要是用来设置cookie的,具体方法如下

01public function getDomain()

02public function getConfigDomain()

03public function getPath()

04public function getLifetime()

05public function setLifetime()

06public function getHttponly()

07public function isSecure()

08public function set()

09public function renew()

10public function get()

11public function delete()

具体设置COOKIE的方法如下:

01/**

02* Set cookie

03*

04* @param string $name The cookie name

05* @param string $value The cookie value

06* @param int $period Lifetime period

07* @param string $path

08* @param string $domain

09* @param int|bool $secure

10* @return Mage_Core_Model_Cookie

11*/

12public function set($name, $value, $period = null, $path = null, $domain = null, $secure = null, $httponly = null)

13{

14}

使用很方便,设置一个Cookie,只要设置cookie的名字和value就可以了,其他的都可以省略

1$cookieModel = Mage::getModel(‘core/cookie’)

2$cookieModel->set($name, $value, $period, $path, $domain, $secure, $httponly)

3//也可以

4Mage::getSingleton(“core/cookie”)->set(“name”,”value”)

获取cookie主要用到的是get()

01/*通过cookie名字来获取cookie

02* $name = name of the cookie

03*/

04Mage::getModel(‘core/cookie’)->get($name)

05/**

06* 得到所有的COOKIE数组

07*/

08Mage::getModel(‘core/cookie’)->get()

09/**

10* delete/remove cookie

11* $name is mandatoryother parameters are optional and cen be set as null

12*/

13Mage::getModel(‘core/cookie’)->get($name, $path, $domain, $secure, $httponly)

也可以通过下面的代码来获取cookie的expire Date, path, domain, secure, httponly

1$cookieExpires = Mage::getModel(‘core/cookie’)->getLifetime()

2$cookiePath = Mage::getModel(‘core/cookie’)->getPath()

3$cookieDomain = Mage::getModel(‘core/cookie’)->getDomain()

4$cookieSecure = Mage::getModel(‘core/cookie’)->isSecure()

5$cookieHttponly = Mage::getModel(‘core/cookie’)->getHttponly()

Session的设置

Mage_Core_Model_Session,这个对象的使用很简单

Mage::getSingleton(‘core/session’)->setXXXX(value)

wampserv安装之后需要运行才能启动php环境,等右下角图标变绿了之后,浏览器输入localhost,出现wamp的欢迎界面说明环境正常。现在使用127.0.0.1去运行magento的安装。magento的session机制不识别localhost,不要用localhost


欢迎分享,转载请注明来源:夏雨云

原文地址:https://www.xiayuyun.com/zonghe/56582.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-02-26
下一篇2023-02-26

发表评论

登录后才能评论

评论列表(0条)

    保存