Symfony\Component\HttpFoundation\Cookie::__construct PHP Method

__construct() public method

Constructor.
public __construct ( string $name, string | null $value = null, integer | string | DateTimeInterface $expire, string $path = '/', string | null $domain = null, boolean $secure = false, boolean $httpOnly = true, boolean $raw = false, string | null $sameSite = null )
$name string The name of the cookie
$value string | null The value of the cookie
$expire integer | string | DateTimeInterface The time the cookie expires
$path string The path on the server in which the cookie will be available on
$domain string | null The domain that the cookie is available to
$secure boolean Whether the cookie should only be transmitted over a secure HTTPS connection from the client
$httpOnly boolean Whether the cookie will be made accessible only through the HTTP protocol
$raw boolean Whether the cookie value should be sent with no url encoding
$sameSite string | null Whether the cookie will be available for cross-site requests
    public function __construct($name, $value = null, $expire = 0, $path = '/', $domain = null, $secure = false, $httpOnly = true, $raw = false, $sameSite = null)
    {
        // from PHP source code
        if (preg_match("/[=,; \t\r\n\013\014]/", $name)) {
            throw new \InvalidArgumentException(sprintf('The cookie name "%s" contains invalid characters.', $name));
        }

        if (empty($name)) {
            throw new \InvalidArgumentException('The cookie name cannot be empty.');
        }

        // convert expiration time to a Unix timestamp
        if ($expire instanceof \DateTimeInterface) {
            $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->name = $name;
        $this->value = $value;
        $this->domain = $domain;
        $this->expire = $expire;
        $this->path = empty($path) ? '/' : $path;
        $this->secure = (bool) $secure;
        $this->httpOnly = (bool) $httpOnly;
        $this->raw = (bool) $raw;

        if (!in_array($sameSite, array(self::SAMESITE_LAX, self::SAMESITE_STRICT, null), true)) {
            throw new \InvalidArgumentException('The "sameSite" parameter value is not valid.');
        }

        $this->sameSite = $sameSite;
    }

Usage Example

Example #1
0
 public function __construct($name, $value, $expire, $path, $domain, $secure, $httpOnly, $raw)
 {
     self::$crypt = env("COOKIE_CRYPT", null);
     if (!is_null(self::$crypt)) {
         $_crypt = strpos(self::$crypt, ".");
         self::$cryptType = strtoupper(substr(self::$crypt, 0, $_crypt));
         self::$cryptLength = intval(substr(self::$crypt, $_crypt + 1));
     }
     $value = self::cryptValue($value);
     parent::__construct($name, $value, $expire, $path, $domain, $secure, $httpOnly, $raw);
 }
All Usage Examples Of Symfony\Component\HttpFoundation\Cookie::__construct