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

Manipulating Headers

As stated previously, one aspect of the response object's duties is to collect and emit HTTP response headers. A variety of methods exist for this:

  • canSendHeaders() is used to determine if headers have already been sent. It takes an optional flag indicating whether or not to throw an exception if headers have already been sent. This can be overridden by setting the property headersSentThrowsException to FALSE.

  • setHeader($name, $value, $replace = false) is used to set an individual header. By default, it does not replace existing headers of the same name in the object; however, setting $replace to TRUE will force it to do so.

    Before setting the header, it checks with canSendHeaders() to see if this operation is allowed at this point, and requests that an exception be thrown.

  • setRedirect($url, $code = 302) sets an HTTP Location header for a redirect. If an HTTP status code has been provided, it will use that status code.

    Internally, it calls setHeader() with the $replace flag on to ensure only one such header is ever sent.

  • getHeaders() returns an array of all headers. Each array element is an array with the keys 'name' and 'value'.

  • clearHeaders() clears all registered headers.

  • setRawHeader() can be used to set headers that are not key and value pairs, such as an HTTP status header.

  • getRawHeaders() returns any registered raw headers.

  • clearRawHeaders() clears any registered raw headers.

  • clearAllHeaders() clears both regular key and value headers as well as raw headers.

In addition to the above methods, there are accessors for setting and retrieving the HTTP response code for the current request, setHttpResponseCode() and getHttpResponseCode().

Setting Cookie Headers

You can inject HTTP Set-Cookie headers into the response object of an action controller by using the provided header class Zend_Http_Header_SetCookie

Constructor Arguments

The following table lists all constructor arguments of Zend_Http_Header_SetCookie in the order they are accepted. All arguments are optional, but name and value must be supplied via their setters if not passed in via the constructor or the resulting Set-Cookie header be invalid.

  • $name: The name of the cookie

  • $value: The value of the cookie

  • $expires: The time the cookie expires

  • $path: The path on the server in which the cookie will be available

  • $domain: The domain to restrict cookie to

  • $secure: boolean indicating whether cookie should be sent over an unencrypted connection (false) or via HTTPS only (true)

  • $httpOnly: boolean indicating whether cookie should be transmitted only via the HTTP protocol

  • $maxAge: The maximum age of the cookie in seconds

  • $version: The cookie specification version

Example 153. Populate Zend_Http_Header_SetCookie via constructor and add to response

->getResponse()->setRawHeader(new Zend_Http_Header_SetCookie(

Example 154. Populate Zend_Http_Header_SetCookie via setters and add to response

= new Zend_Http_Header_SetCookie();

Zend Framework