English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Tutorial PHP Básico

Tutorial PHP Avançado

PHP & MySQL

Manual de Referência PHP

Uso e exemplo da função setcookie() do PHP

PHP HTTP  Reference Manual

A função setcookie() envia o Cookie

Sintaxe

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

Definição e uso

Usado para definir COOKIES.

 A função setcookie() envia o Cookie assim que é definido, juntamente com os outros cabeçalhos HTTP para o cliente. Como outros cabeçalhos HTTP, o Cookie deve ser enviado antes de qualquer saída ser gerada pelo script (devido aos limites do protocolo). Chame essa função antes de gerar qualquer saída (inclusive <html> e <head> ou espaços).
Após definir o Cookie, é possível usá-lo para ler $_COOKIE na próxima vez que a página for aberta. O valor do Cookie também está presente em $_REQUEST.

Retorno de valor

 If output has been generated before calling this function, setcookie() will fail and return FALSE. If setcookie() runs successfully, it returns TRUE. Of course, this does not mean that the user has accepted the Cookie.

Parameter

NumberParameters and descriptions
1

name

cookie name

2

value

Cookie value. This value is stored on the user's computer, do not store sensitive information. For example, if the name is 'cookiename', its value can be obtained through $_COOKIE['cookiename'].

3

errno

It contains information about the cookie input.

4

expire

Cookie's expiration time. This is a Unix timestamp, i.e., the number of seconds since the Unix epoch (Greenwich Mean Time 1970 Year 1 Month 1 The number of seconds from the Unix epoch (Greenwich Mean Time+60*60*24*30 is to set the Cookie 30 days after expiration. If set to zero, or omitted, the Cookie will expire at the end of the session (i.e., when the browser is closed).

5

path

Cookie's valid server path. Set to '/,' the Cookie is valid for the entire domain '/foo/,' the Cookie is only valid for the domain in ' /foo/ Directory and subdirectories are valid (for example /foo/bar/). The default value is the current directory at the time the Cookie is set.

6

domain

Cookie's valid domain/Subdomain. Setting it to a subdomain (for example, 'www.example.com') makes the Cookie valid for this subdomain and its third-level subdomains (for example, w2.www.example.com). To make the Cookie valid for the entire domain (including all its subdomains), just set it to the domain (in this example, 'example.com').

Online example

Try the following example

<?php
   $input = '它包含cookie的名称';
   
   setcookie("TestCookie", $input);
   setcookie("TestCookie", $input, time())+3600); 
   setcookie("TestCookie", $input, time())+3600, "/~rasmus/", "oldtoolbag.com", 1);
?>

PHP HTTP  Reference Manual