Bluz\Response\Response::setCookie PHP Method

setCookie() public method

Set Cookie
public setCookie ( string $name, string $value = '', integer | string | DateTime $expire, string $path = '/', string $domain = '', boolean $secure = false, boolean $httpOnly = false ) : void
$name string
$value string
$expire integer | string | DateTime
$path string
$domain string
$secure boolean
$httpOnly boolean
return void
    public function setCookie($name, $value = '', $expire = 0, $path = '/', $domain = '', $secure = false, $httpOnly = false)
    {
        // from PHP source code
        if (preg_match("/[=,; \t\r\n\v\f]/", $name)) {
            throw new \InvalidArgumentException('The cookie name contains invalid characters.');
        }
        if (empty($name)) {
            throw new \InvalidArgumentException('The cookie name cannot be empty.');
        }
        // convert expiration time to a Unix timestamp
        if ($expire instanceof \DateTime) {
            $expire = $expire->format('U');
        } elseif (!is_numeric($expire)) {
            $expire = strtotime($expire);
            if (false === $expire || -1 === $expire) {
                throw new \InvalidArgumentException('The cookie expiration time is not valid.');
            }
        }
        $this->cookies[$name] = ['name' => $name, 'value' => $value, 'expire' => $expire, 'path' => $path, 'domain' => $domain, 'secure' => (bool) $secure, 'httpOnly' => (bool) $httpOnly];
    }

Usage Example

Example #1
0
 /**
  * @expectedException \InvalidArgumentException
  */
 public function testSetCookieWithWrongDateNameThrowException()
 {
     $this->response->setCookie('foo', 'bar', 'the day before sunday');
 }