0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Cookieの使い方

Last updated at Posted at 2025-09-17

Cookie

  • ウェブサイトがパソコンやスマホに一時的に保存するテキストファイルのこと

Cookieの役割

  • ログイン状態の維持
  • サイト設定の保存
  • ショッピングカートの情報保持

Laravelでの使い方

例:

ユーザーが初めてアクセスした時

use Illuminate\Http\Request;
use Illuminate\Http\Response;

Route::get('/welcome', function (Request $request) {
    return new Response('ようこそ!初めての訪問ですね。')
        ->cookie('visited', 'true', 10);
});
  • visited=trueというCookieを発行し10分間保持している
     
     
    再度(10分以内)にアクセスした時
use Illuminate\Http\Request;

Route::get('/greeting', function (Request $request) {
    $visited = $request->cookie('visited');

    if ($visited) {
        return 'おかえりなさい!また会えて嬉しいです。';
    }

    return 'こんにちは!';
});
  • ブラウザが持っているCookieをRequestオブジェクトから読み込み
  • Cookieを持っていると特別なメッセージを表示
  • 持っていなければ一般的なメッセージ
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?