この内容は古いバージョンです。最新バージョンを表示するには、戻るボタンを押してください。
バージョン:3
ページ更新者:guest
更新日時:2026-06-10 17:35:42

タイトル: $_COOKIE

この本文には実行可能なタグ等が含まれるため、安全のためHTMLソースとして表示しています。本文データは変更していません。

<p>クライアントに情報を保持する機能をクッキーと呼ぶ。</p>

<p>PHPでは$_COOKIE変数を使用してクッキーの制御を行う。</p>

<p><br />
変数の参照方法は以下の通り。</p>

<p>&nbsp;&nbsp; &nbsp;<span style="color:#e74c3c"><strong>$_COOKIE[キー名]</strong></span></p>

<p><br />
変数の設定方法は以下の通り。</p>

<p>&nbsp;&nbsp; &nbsp;<span style="color:#e74c3c"><strong>setcookie(キー名, 値, 保存時間)</strong></span></p>

<p><br />
実際の使い方は以下の例を参照。</p>

<p>【index.php】</p>

<p style="margin-left:40px"><img alt="" src="/uploadImage/20170809015924_5064069.jpg" />&nbsp; &nbsp;&nbsp;</p>

<p style="margin-left:40px"><html></p>

<p style="margin-left:40px">&nbsp; &nbsp; <body></p>

<p style="margin-left:40px">&nbsp; &nbsp; &nbsp; &nbsp; <form method="GET" action="testCookie.php"></p>

<p style="margin-left:40px">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#3498db"><!-- テキストボックス --></span></p>

<p style="margin-left:40px">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <input type="text" name="save"/></p>

<p style="margin-left:40px">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#3498db"><!-- 保存ボタン --></span></p>

<p style="margin-left:40px">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <input type="submit" value="保存する"/></p>

<p style="margin-left:40px"></form><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <?php&nbsp; &nbsp; &nbsp; &nbsp; <!--?php</p--></p>

<p style="margin-left:40px">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color:#3498db">//クッキーに保存した情報を表示する</span></p>

<p style="margin-left:40px">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(@is_null(<span style="color:#e74c3c">$_COOKIE['save']</span>)) { <span style="color:#3498db">//クッキーに情報が無い場合</span></p>

<p style="margin-left:40px">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print "クッキーには何も保存されていません";</p>

<p style="margin-left:40px">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else { //クッキーに情報がある場合</p>

<p style="margin-left:40px">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print "クッキーには";</p>

<p style="margin-left:40px">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print <span style="color:#e74c3c">$_COOKIE['save']</span>;</p>

<p style="margin-left:40px">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print "が保存されています。";</p>

<p style="margin-left:40px">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }</p>

<p style="margin-left:40px">&nbsp; &nbsp; &nbsp; &nbsp; ?></p>

<p style="margin-left:40px">&nbsp; &nbsp;&nbsp; &nbsp; </body></p>

<p style="margin-left:40px"></html></p>

<p style="margin-left:40px">試しに何かクッキーに保存してみる。</p>

<p style="margin-left:40px"><img alt="" src="/uploadImage/20170809020107_8295657.jpg" /></p>

<p style="margin-left:40px">テキストボックスに文字列を入れて「保存する」ボタンを押す。</p>

<p style="margin-left:40px">&nbsp;</p>

<p>【testCookie.php】</p>

<p style="margin-left:40px"><img alt="" src="/uploadImage/20170809020610_5786485.jpg" /></p>

<p style="margin-left:40px"><?php</p>

<p style="margin-left:40px">&nbsp; &nbsp; <span style="color:#3498db">//クッキーに保存</span></p>

<p style="margin-left:40px">&nbsp; &nbsp; <strong><span style="color:#e74c3c">setcookie('save', $_GET['save'], time() + 100)</span></strong>;<br />
&nbsp; &nbsp;&nbsp;<br />
&nbsp; &nbsp; print "クッキーを保存しました。";<br />
&nbsp; &nbsp;&nbsp;</p>

<p style="margin-left:40px">?></p>

<p style="margin-left:40px">もう一度、index.phpを開くと・・・。</p>

<p style="margin-left:40px"><img alt="" src="/uploadImage/20170809020117_5763084.jpg" /></p>

<p style="margin-left:40px">クッキーに保存されていることが確認できる。</p>

<p>&nbsp;</p>

<p><strong><span style="color:#e74c3c">※クッキーはクライアント側で情報を保持するのでデータ改竄が容易である。</span></strong></p>

<p><strong><span style="color:#e74c3c"> 従って扱うデータによってはセキュリティ的な観点であまり使用すべきではない。</span></strong><br />
&nbsp;</p>

<p>&nbsp;</p>