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

PHP Основной Учебник

PHP Уровеньный Учебник

PHP & MySQL

PHP Референс Мануал

Пример использования функции PHP setcookie() и примеры

PHP HTTP  reference manual

Функция setcookie() отправляет Cookie

Синтаксис

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

Определение и использование

Эта функция используется для установки COOKIE.

 Функция setcookie() отправляет Cookie вместе с оставшимися HTTP-заголовками клиенту. Как и другие HTTP-заголовки, Cookie должен быть отправлен до того, как скрипт произведет какой-либо вывод (из-за ограничений протокола). Позвоните в эту функцию до того, как произойдет какой-либо вывод (включая <html> и <head> или пробелы).
После того как Cookie будет установлен, при следующем открытии страницы его можно будет прочитать с помощью $_COOKIE. Значения Cookie также присутствуют в $_REQUEST.

Возвратное значение

 Если до вызова этой функции уже был произведен вывод, функция setcookie() не сработает и вернет FALSE. Если setcookie() успешно выполнится, вернет TRUE. Однако, это не означает, что пользователь уже принял Cookie.

Parameters

Serial numberParameters and descriptions
1

name

The name of the cookie.

2

value

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

3

errno

It contains information about the cookie input.

4

expire

Expiration time of the cookie. This is a Unix timestamp, which is the number of seconds since the Unix epoch (Greenwich Mean Time, January 1, 1970, 00:00:00). That is, it can be the result of the time() function plus the number of seconds you want the Cookie to expire. Or you can use mktime(). time()+60*60*24*30 sets the Cookie to expire 30 days later. 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

Server path where the cookie is valid. Setting it to '/' makes the Cookie valid for the entire domain 'domain'. If set to '/foo/', the Cookie is only valid for the '/foo/' directory and its subdirectories (e.g., /foo/bar/). The default value is the current directory at the time the Cookie is set.

6

domain

Valid domain/subdomain for cookies. Setting it to a subdomain (e.g., 'www.example.com') makes the Cookie valid for this subdomain and its third-level domains (e.g., 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 = 'It contains the name of the cookie';
   
   setcookie("TestCookie", $input);
   setcookie("TestCookie", $input, time()+3600); 
   setcookie("TestCookie", $input, time()+3600, "/~rasmus/", "oldtoolbag.com", 1);
?>

PHP HTTP  reference manual