0Pricing
Flask Academy · บทเรียน

กำหนดคุกกี้แบบกำหนดเองในการตอบกลับ

เขียนคุกกี้ด้วย set_cookie และตัวเลือกต่าง ๆ

กำหนดคุกกี้แบบกำหนดเองในการตอบกลับ เป็นบทเรียน Flask Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Flask Academy และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Flask Academy มีบทเรียนทั้งหมด 4 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

Cookies Beyond Sessions

Sometimes you want your own cookie, separate from the session, to remember a theme or a preference for a visitor. 🍪

You Need a Response

Cookies are written onto a response object. So first build one with make_response instead of returning a plain string.

from flask import make_response
resp = make_response('Hi there')

Call set_cookie

Add the cookie with set_cookie, passing a name and a value. Then return that response so the header reaches the browser.

resp.set_cookie('theme', 'dark')
return resp

Read It Next Time

On the next request the browser sends it back. Read your cookie from request.cookies, which works like a dictionary.

from flask import request
theme = request.cookies.get('theme')

Control the Lifetime

Set max_age in seconds to control how long a cookie survives. Leave it out and the cookie dies when the browser closes.

resp.set_cookie('theme', 'dark', max_age=60*60*24)

HTTPS Only

The secure flag tells the browser to send the cookie only over HTTPS, keeping it off plain unencrypted connections.

resp.set_cookie('token', 'abc', secure=True)

Hide from JavaScript

Turn on httponly so client-side scripts cannot read the cookie. This blocks a common cross-site scripting theft path.

resp.set_cookie('token', 'abc', httponly=True)

Limit Cross-Site Sending

The samesite option controls when the cookie travels on cross-site requests. Set it to Lax or Strict to curb CSRF risk.

resp.set_cookie('id', '7', samesite='Lax')

Deleting a Cookie

To remove one, call delete_cookie with its name on the response. The browser then drops it on the next visit.

resp.delete_cookie('theme')

Values Are Strings

Cookie values are always strings. To store a number or flag, convert it on the way out and parse it on the way back in.

resp.set_cookie('count', str(5))

Mind the Size

Browsers cap each cookie near four kilobytes. Keep them tiny and store large data on the server, referenced by an id.

Quick Check

You want a cookie that JavaScript on the page cannot read.

Recap

You learned to build a response, write cookies with set_cookie, read them from request.cookies, and harden them with secure flags. 🛡️

คำถามที่พบบ่อย

บทเรียน “กำหนดคุกกี้แบบกำหนดเองในการตอบกลับ” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “กำหนดคุกกี้แบบกำหนดเองในการตอบกลับ” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Flask Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Flask Academy มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “กำหนดคุกกี้แบบกำหนดเองในการตอบกลับ”

เขียนคุกกี้ด้วย set_cookie และตัวเลือกต่าง ๆ คุณปฏิบัติ Flask Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Flask Academy หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Flask Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน

บทเรียน “กำหนดคุกกี้แบบกำหนดเองในการตอบกลับ” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน Flask Academy นี้ได้ไหม

ได้ บทเรียน Flask Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. กำหนดและอ่านพจนานุกรม session
  2. SECRET_KEY และคุกกี้ที่ลงลายมือชื่อ
  3. กำหนดคุกกี้แบบกำหนดเองในการตอบกลับ
  4. ข้อความแฟลชระหว่างคำขอ
← กลับไปที่ Flask Academy