联盟会员的ZF2 Cookies

时间:2015-10-09 12:39:38

标签: php zend-framework cookies

我正在尝试编写一个cookie类来存储推荐信息,需要一些指导。

documentation上的http://example.com/referrer_id=123似乎有点稀疏。

与任何联盟计划一样,我们的想法是从链接中获取信息:here并将其存储在用户计算机上供以后使用。

所以这就是我到目前为止:

控制器:

    $cookieObject = new CookieService();

    $cookieObject->setCookie(
        [
            'value' => [
                'name' => 'test',
                'referring_site'=>'test site'
            ]
        ]
    );

    die(var_dump($cookieObject->getCookie()));

CookieService类:

<?php
namespace Application\Library\Session;

use Zend\Http\Header\SetCookie;
use Zend\Http\Cookies;
class CookieService extends SetCookie
{
    public function __construct() {

        $this->cookie = new SetCookie();
        $this->header = new Cookies();
    }

    /**
     * Create a new cookie
     * @param $params
     */
    public function setCookie($params)
    {
        $key = (isset($params['key'])) ? $params['key'] : 'referrer';
        $value = (isset($params['value'])) ? $params['value'] : [];
        $expires = (isset($params['time'])) ? time() + $params['time'] : time() +  365 * 60 * 60 * 24;

        /**
         * Test whether the cookie exists before updating
         */
        if (!$this->exists($key))
        {
            $this->cookie->setName($key);
            $this->cookie->setValue($value);
            $this->cookie->setExpires($expires);
            $this->header->addCookie($this->cookie);
        }


    }

    /**
     * Get a cookie based on a given key
     * @param string $key
     * @return null
     */
    public function getCookie($key = 'referrer')
    {
        $cookieArray = $this->header->getAllCookies();

        foreach ($cookieArray AS $cookie)
        {
            if ($cookie->name === $key)
            {
                return $cookie;
            }
        }

        return null;

    }

    /**
     * Returns whether a cookie exists or not
     * @param $key
     * @return bool
     */
    public function exists($key)
    {
        return ( $this->getCookie($key) ) ? true : false;
    }

}

我正在努力解决以下问题:

如果我保存cookie然后关闭浏览器,则会删除cookie。我似乎无法找到任何明确涵盖此用途的文档。

任何建议,非常感谢。

ND:Cookie对象的转储:

object(Zend\Http\Header\SetCookie)[1085]   
    protected 'name' => string 'referrer' (length=8)   
    protected 'value' => 
    array (size=2)
      'name' => string 'test' (length=4)
      'referring_site' => string 'test site' (length=9)   
    protected 'version' => null   
    protected 'maxAge' => null   
    protected 'expires' => int 1475931242   
    protected 'domain' => null   
    protected 'path' => null   
    protected 'secure' => boolean false   
    protected 'quoteFieldValue' => boolean false   
    protected 'httponly' => boolean false   
    public 'type' => string 'Cookie' (length=6)

1 个答案:

答案 0 :(得分:0)

好的 - 所以我写了自己的课来处理这个问题。这很简单:

params[:ipv4address]