Magento怎么设置和使用Cookie和Session

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

Magento的核心对象-Mage_Core_Model_Cookie & Mage_Core_Model_Session
Mage_Core_Model_Cookie,这个对象主要是用来设置cookie的,具体方法如下:

01    public function getDomain();
02    public function getConfigDomain();
03    public function getPath();
04    public function getLifetime();
05    public function setLifetime();
06    public function getHttponly();
07    public function isSecure();
08    public function set();
09    public function renew();
10    public function get();
11    public 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    */
12    public 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    //也可以
4    Mage::getSingleton(“core/cookie”)->set(“name”,”value”);

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

01    /*通过cookie名字来获取cookie
02    * $name = name of the cookie
03    */
04    Mage::getModel(‘core/cookie’)->get($name);
05    /**
06    * 得到所有的COOKIE数组
07    */
08    Mage::getModel(‘core/cookie’)->get();
09    /**
10    * delete/remove cookie
11    * $name is mandatory; other parameters are optional and cen be set as null
12    */
13    Mage::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);

source: http://zhidao.baidu.com/link?url=76CrtnKHlOniKFEJgdaeyAM5CkxBlJTlQ5YazbBVdx_LSg705DZd9Smzy3_6bIYBYgRjAvBJOz54vRTgs3j_uoL2cK8kFGkWSqsa4Mn4Ofq

你可能感兴趣的:(Magento)