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

0
Posted at

結論

  • セッション:サーバー側でユーザーの状態を管理する仕組み
  • Cookie:ブラウザに保存しておく小さなデータ

詳細

なぜ必要か

例えば、あるWebサイトにログインするとする:
image.png

ログインしたあとは、

  • マイページを開く
  • 商品一覧を開く
  • 注文履歴を開く
    等の操作がある
    →毎回ユーザー名とパスワードを送ってログインし直すのは不便
    →ログイン済みであることを覚えておく仕組みが必要になる

セッション

  • サーバー側でユーザーごとの状態を管理する仕組み
  • 管理例:

image.png

Cookie

  • Cookieとは、サーバーが作ってブラウザに渡し、ブラウザが自身に保存する小さなデータのこと
  • 例えば、サーバーから次のようなCookieが渡されるとする
    • Cookie名:sessionId
    • 値:abc123
  • ブラウザはこれを保存しておき、次に同じWebサイトへリクエストするとき、Cookieを自動的に送信する

image.png

リクエスト例:このように、CookieはHTTPリクエストに含まれてサーバーに送られる

GET /mypage HTTP/1.1
Host: example.com
Cookie: sessionId=abc123

なお、CookieはJavaScriptから操作することもできるが、HttpOnly 属性が付いたCookieはJavaScriptから読み取れない

ログイン~マイページ表示までのセッション・Cookieの流れ

image.png

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?