PHP (setcookie) vs header (header)

4

I see some frameworks, CMS and the like, write cookies directly in PHP with setcookie and others stock to emit in headers at the end of execution. I would like to know how to create a pattern for creating cookies.

  

PHP

     

The setcookie () function sets a cookie to be sent along with the rest of the HTTP headers

Should we create by sending by header?

header( "Set-Cookie: {$name}={$value};" )

Or is it PHP?

setcookie( $name , $value )

    
asked by anonymous 30.07.2014 / 09:39

1 answer

4

Both functions get the same result.

But what sets setcookie() apart is its ease of creation of more complex cookies, with expiration date, domain of the application that the cookie will be used (www.algumacoisa.com.br), among others.

See all the parameters that the function accepts:

setcookie ( string $name [, string $value [, int $expire = 0 [, string $path [, string $domain [, bool $secure = false [, bool $httponly = false ]]]]]] )

More information on the php documentation .

    
30.07.2014 / 12:12