Работа с куками (cookie) в Zend Framework

Для установки куки необходимо:

$cookie = new Zend_Http_Cookie('name',
    'value',
    '.site.ru',
    time() + 7200,
    '/'
);
$this->getResponse()->setHeader('Set-Cookie', $cookie->__toString());
//Для передачи всех данных нужно использовать
$this->getResponse()->setHeader('Set-Cookie', $cookie->__toStringFull());


Веб-сервер отправляет браузеру заголовок Set-Cookie.

В Zend\Http\Cookie.php необходимо добавить метод:

public function __toStringFull()
{
	$string = '';
	if ($this->encodeValue) {
		$string .= $this->name . '=' . urlencode($this->value);
	} else {
		$string .= $this->name . '=' . $this->value;
	}
	
	if (($domain = $this->getDomain())) {
		$string .= '; Domain=' . $domain;
	}
	
	if (($path = $this->getPath())) {
		$string .= '; Path=' . $path;
	}
	
	if (($expires = $this->getExpiryTime())) {
		$string .= '; Expires=' . gmstrftime("%a, %d %b %Y %H:%M:%S", $expires );
	}

	if ($this->isSecure()) {
		$string .= '; Secure';
	}

	return $string;
}

Получение значения куки:

$this->getRequest()->getCookie('name')

Мы получаем отправленный браузером заголовок Cookie

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *