PhpRiot
Become Zend Certified

Prepare for the ZCE exam using our quizzes (web or iPad/iPhone). More info...


When you're ready get 7.5% off your exam voucher using voucher CJQNOV23 at the Zend Store

Zend_Http_Cookie getter methods

Once a Zend_Http_Cookie object is instantiated, it provides several getter methods to get the different properties of the HTTP cookie:

  • getName(): Get the name of the cookie

  • getValue(): Get the real, decoded value of the cookie

  • getDomain(): Get the cookie's domain

  • getPath(): Get the cookie's path, which defaults to '/'

  • getExpiryTime(): Get the cookie's expiration time, as UNIX time stamp. If the cookie has no expiration time set, will return NULL.

Additionally, several boolean tester methods are provided:

  • isSecure(): Check whether the cookie is set to be sent over secure connections only. Generally speaking, if TRUE the cookie should only be sent over HTTPS.

  • isExpired(int $time = null): Check whether the cookie is expired or not. If the cookie has no expiration time, will always return TRUE. If $time is provided, it will override the current time stamp as the time to check the cookie against.

  • isSessionCookie(): Check whether the cookie is a "session cookie" - that is a cookie with no expiration time, which is meant to expire when the session ends.

Example 500. Using getter methods with Zend_Http_Cookie

<?php
// First, create the cookie
$cookie =
    
Zend_Http_Cookie::fromString('foo=two+words; ' +
                                 
'domain=.example.com; ' +
                                 
'path=/somedir; ' +
                                 
'secure; ' +
                                 
'expires=Wednesday, 28-Feb-05 20:41:22 UTC');

echo 
$cookie->getName();   // Will echo 'foo'
echo $cookie->getValue();  // will echo 'two words'
echo $cookie->getDomain(); // Will echo '.example.com'
echo $cookie->getPath();   // Will echo '/'

echo date('Y-m-d'$cookie->getExpiryTime());
// Will echo '2005-02-28'

echo ($cookie->isExpired() ? 'Yes' 'No');
// Will echo 'Yes'

echo ($cookie->isExpired(strtotime('2005-01-01') ? 'Yes' 'No');
// Will echo 'No'

echo ($cookie->isSessionCookie() ? 'Yes' 'No');
// Will echo 'No'


Zend Framework