PHP setrawcookie()関数
例
次の例では、PHPを使用してCookieを作成します。Cookieの名前は「user」で、値は「JohnDoe」になります。Cookieの値はURLエンコードされません。Cookieは30日後に期限切れになります(86400 * 30)。「/」を使用すると、CookieがWebサイト全体で利用可能になることを意味します(それ以外の場合は、希望するディレクトリを選択します)。
<?php
$cookie_name = "user";
$cookie_value = "John";
setrawcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/");
// 86400 = 1 day
?>
<html>
<body>
<?php
echo "Cookie is set.";
?>
</body>
</html>
?>
定義と使用法
setrawcookie()関数は、残りのHTTPヘッダーと一緒に送信されるCookie(URLエンコードなし)を定義します。
Cookieは、ユーザーを識別するためによく使用されます。Cookieは、サーバーがユーザーのコンピューターに埋め込む小さなファイルです。同じコンピュータがブラウザでページを要求するたびに、Cookieも送信します。PHPを使用すると、Cookie値の作成と取得の両方を行うことができます。
Cookieの名前は、同じ名前の変数に自動的に割り当てられます。たとえば、Cookieが「user」という名前で送信された場合、Cookie値を含む$ userという変数が自動的に作成されます。
注: setrawcookie()関数は、<html>タグの前に表示する必要があります。
注:送信時にCookie値を自動的にURLエンコードし、受信時に自動的にデコードするには、代わりにsetcookie()関数を使用します。
構文
setrawcookie(name, value, expire, path, domain, secure);
パラメータ値
Parameter | Description |
---|---|
name | Required. Specifies the name of the cookie |
value | Optional. Specifies the value of the cookie |
expire | Optional. Specifies when the cookie expires. The value: time()+86400*30, will set the cookie to expire in 30 days. If this parameter is not set, the cookie will expire at the end of the session (when the browser closes) |
path | Optional. Specifies the server path of the cookie. If set to "/", the cookie will be available within the entire domain. If set to "/php/", the cookie will only be available within the php directory and all sub-directories of php. The default value is the current directory that the cookie is being set in |
domain | Optional. Specifies the domain name of the cookie. To make the cookie available on all subdomains of example.com, set domain to ".example.com". Setting it to www.example.com will make the cookie only available in the www subdomain |
secure | Optional. Specifies whether or not the cookie should only be transmitted over a secure HTTPS connection. TRUE indicates that the cookie will only be set if a secure connection exists. Default is FALSE. |
技術的な詳細
戻り値: | 成功するとTRUE。失敗するとFALSE |
---|---|
PHPバージョン: | 5歳以上 |
その他の例
例
「user」という名前のCookieの値を取得します(グローバル変数$ _COOKIEを使用)。また、isset()関数を使用して、Cookieが存在するかどうかを確認します。
<html>
<body>
<?php
$cookie_name = "user";
if(!isset($_COOKIE[$cookie_name])) {
echo "Cookie named '" . $cookie_name . "' does not exist!";
} else {
echo "Cookie is named: " . $cookie_name . "<br>Value is: " . $_COOKIE[$cookie_name];
}
?>
</body>
</html>
例
Cookieを変更するには、setrawcookie()関数を使用してCookieを(再度)設定するだけです。
<?php
$cookie_name = "user";
$cookie_value = "Alex";
setrawcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/");
?>
<html>
<body>
<?php
$cookie_name = "user";
if(!isset($_COOKIE[$cookie_name])) {
echo "Cookie named '" . $cookie_name . "' does not exist!";
} else {
echo "Cookie is named: " . $cookie_name . "<br>Value is: " . $_COOKIE[$cookie_name];
}
?>
</body>
</html>
例
Cookieを削除するには、有効期限が過去のsetrawcookie()関数を使用します。
<?php
$cookie_name = "user";
unset($_COOKIE[$cookie_name]);
// empty value and expiration one hour before
$res = setrawcookie($cookie_name, '', time() - 3600);
?>
<html>
<body>
<?php
echo "Cookie 'user' is deleted.";
?>
</body>
</html>
例
Cookieが有効になっているかどうかを確認する小さなスクリプトを作成します。まず、setrawcookie()関数を使用してテストCookieを作成してから、$ _ COOKIE配列変数をカウントします。
<?php
setrawcookie("test_cookie", "test", time() + 3600, '/');
?>
<html>
<body>
<?php
if(count($_COOKIE) > 0) {
echo "Cookies are enabled";
} else {
echo "Cookies are disabled";
}
?>
</body>
</html>
❮PHPネットワークリファレンス