$_COOKIE

$HTTP_COOKIE_VARS [застаріла]

(PHP 4 >= 4.1.0, PHP 5)

$_COOKIE -- $HTTP_COOKIE_VARS [застаріла]HTTP Cookies

Опис

Асоціативний масив (array) змінних, переданих до поточного скрипта через HTTP Cookies.

Змінна $HTTP_COOKIE_VARS містить таку ж ініціалізуючу інформацію, але не є Суперглобальною. (Зауважте, що $HTTP_COOKIE_VARS та $_COOKIE є різними змінними, а тому PHP обробляє їх як такі)

Журнал Змін

Версія Опис
4.1.0 Введено $_COOKIE для заміни застарілої $HTTP_COOKIE_VARS.

Приклади

Приклад #1 Використання $_COOKIE

<?php
echo 'Як справи, ' htmlspecialchars($_COOKIE["name"]) . '?!';
?>

Припускається, що куку з назвою "name" було встановлено раніше, а її значення містить рядок "Вася"

Наведений вище приклад виведе щось подібне до:

Як справи, Вася?!

Примітки

Зауваження:

Це є "суперглобальна", або автоматична глобальна змінна. Тобто ця змінна доступна у всіх областях видимості скрипта, та її не потрібно оголошувати як global $variable; щоб отримати доступ до неї всередині функції чи метода.

add a note add a note

User Contributed Notes 4 notes

up
62
kiril (at) atern (dot) us
7 years ago
To clarify the previously posted note:

Dots (.) and spaces ( ) in cookie names are being replaced with underscores (_).
up
63
k dot andris at gmail dot com
8 years ago
beware, dots (.) in cookie names are replaces by underscores (_)
up
0
axodjakov at gmail dot com
2 years ago
$_COOKIE returns an array if there are more than one cookie saved under the given key.
up
-16
alexander-schranz at NO_SPAM dot hotmail dot com
6 years ago
Cookies with the same name the first cookie is used. Clients will send cookies with longer path before cookies with shorter path. This comes from RFC 6265 which says "Cookies with longer paths are listed before cookies with shorter paths.". So you get the best matching cookie for your current request.
To Top