PHPチュートリアル

PHPホーム PHPイントロ PHPインストール PHP構文 PHPコメント PHP変数 PHPエコー/印刷 PHPデータ型 PHP文字列 PHP番号 PHP数学 PHP定数 PHP演算子 PHP If ... Else ... Elseif PHPスイッチ PHPループ PHP関数 PHP配列 PHPスーパーグローバル PHP正規表現

PHPフォーム

PHPフォーム処理 PHPフォームの検証 PHPフォームが必要 PHPフォームのURL / Eメール PHPフォームの完了

PHP Advanced

PHPの日付と時刻 PHPインクルード PHPファイルの処理 PHPファイルのオープン/読み取り PHPファイルの作成/書き込み PHPファイルのアップロード PHPクッキー PHPセッション PHPフィルター PHPフィルターアドバンスト PHPコールバック関数 PHP JSON PHPの例外

PHPOOP _

PHPOOPとは PHPクラス/オブジェクト PHPコンストラクター PHPデストラクタ PHPアクセス修飾子 PHPの継承 PHP定数 PHP抽象クラス PHPインターフェース PHPの特性 PHP静的メソッド PHPの静的プロパティ PHP名前空間 PHPIterables

MySQLデータベース

MySQLデータベース MySQLコネクト MySQL Create DB MySQLテーブルの作成 MySQLの挿入データ MySQLは最後のIDを取得します MySQL Insert Multiple MySQLを準備しました MySQL Select Data MySQL Where MySQL Order By MySQLデータの削除 MySQLアップデートデータ MySQL制限データ

PHP XML

PHPXMLパーサー PHPSimpleXMLパーサー PHPSimpleXML-取得 PHP XMLExpat PHP XML DOM

PHP -AJAX

AJAXイントロ AJAX PHP AJAXデータベース AJAX XML AJAXライブ検索 AJAXポール

PHPの

PHPの例 PHPコンパイラ PHPクイズ PHP演習 PHP証明書

PHPリファレンス

PHPの概要 PHP配列 PHPカレンダー PHPの日付 PHPディレクトリ PHPエラー PHP例外 PHPファイルシステム PHPフィルター PHP FTP PHP JSON PHPキーワード PHP Libxml PHPメール PHP数学 PHPその他 PHP MySQLi PHPネットワーク PHP出力制御 PHP正規表現 PHP SimpleXML PHPストリーム PHP文字列 PHP変数の処理 PHPXMLパーサー PHP Zip PHPタイムゾーン

PHP setrawcookie()関数

❮PHPネットワークリファレンス

次の例では、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ネットワークリファレンス