HTTP Cookies
是一塊網站會使用到的資料,這些資料會存在使用者的電腦上,並被瀏覽器使用。
It is a small piece of data sent from a website and stored on the user's computer by the user's web browser while the user is browsing.
運作流程
- Client向server請求資料時,會將符合的cookie傳至server
- 當client取得返回的資料時,會將返回的cookie存入電腦中
- 通常會紀錄該cookie 的隸屬網域(Domain)、網頁目錄(Path)、過期時間(Expires) 等資訊
特色
- cookie有容量與數量限制。cookie總大小最多4KB。
- cookie無法跨域(a.com不能設定b.com的cookie)
註:跨域可用token或參數傳送資料
Usage
for websites to remember STATEFUL information
(such as items added in the shopping cart in an online store)
RECORD the user's browsing activity
(including clicking particular buttons, logging in, or recording which pages were visited in the past)
Cookie vs HTTP Storage
Cookie在每個HTTP request送出時都會被送到Server端,在某種程度上會拖慢執行的效能與浪費不必要的網路頻寬
Cookie | Local Storage | Session Storage | |
---|---|---|---|
HTTP Request | 會傳送到server | 不會 | 不會 |
容量 | 4KB | 5MB | 5MB |
生命週期 | 總和4KB | 長 | 分頁關閉即消失 |
有效範圍 | 總和4KB | 無法跨分頁 |