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 toFALSE.-
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$replacetoTRUEwill 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$replaceflag 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().




