0Pricing
Flask Academy · Lezione

Impostare cookie personalizzati in una risposta

Scriva cookie con set_cookie e le relative opzioni.

Impostare cookie personalizzati in una risposta è una lezione Flask Academy gratuita su CoddyKit. Questa è la lezione 3 di 4. Puoi leggere la lezione completa qui gratuitamente — poi esercitati direttamente nel browser con un editor di codice integrato e un tutor IA disponibile 24/7. Fa parte del percorso di apprendimento Flask Academy, e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso Flask Academy include 4 lezioni in totale.

Parti di questa lezione non sono ancora state tradotte e vengono mostrate in inglese.

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. 🛡️

Domande Frequenti

La lezione «Impostare cookie personalizzati in una risposta» è gratuita?

Sì — il testo completo di «Impostare cookie personalizzati in una risposta» è gratuito qui sul web. Per esercitarvi in modo interattivo (un editor di codice integrato e un tutor IA 24/7) e sbloccare il resto del corso Flask Academy, passa a CoddyKit PRO. Il corso Flask Academy include 4 lezioni in totale.

Cosa imparerò in «Impostare cookie personalizzati in una risposta»?

Scriva cookie con set_cookie e le relative opzioni. Eserciti Flask Academy con codice pratico che esegui direttamente nel browser, e un tutor IA 24/7 risponde alle tue domande mentre lavori sulla lezione.

Ho bisogno di esperienza per iniziare Flask Academy?

Non è richiesta alcuna esperienza precedente. Flask Academy su CoddyKit è strutturato per principianti e studenti avanzati, quindi puoi iniziare da qui o dall'inizio e procedere al tuo ritmo. Questa è la lezione 3 di 4.

Quanto tempo richiede la lezione «Impostare cookie personalizzati in una risposta»?

La maggior parte delle lezioni CoddyKit richiede circa 5–10 minuti. Ogni lezione è breve e interattiva, quindi fai progressi costanti e riprendi esattamente da dove hai lasciato su web e app.

Posso scrivere ed eseguire codice in questa lezione Flask Academy?

Sì. Ogni lezione Flask Academy include un editor di codice integrato, quindi scrivi ed esegui codice reale direttamente nel tuo browser e ricevi feedback istantaneo dall'IA — nessuna configurazione locale necessaria.

Tutte le lezioni di questo corso

  1. Impostare e leggere il dizionario session
  2. SECRET_KEY e cookie firmati
  3. Impostare cookie personalizzati in una risposta
  4. Messaggi flash tra le richieste
← Torna a Flask Academy