Rails 如何使用 cookies

紅寶鐵軌客
Join to follow...
Follow/Unfollow Writer: 紅寶鐵軌客
By following, you’ll receive notifications when this author publishes new articles.
Don't wait! Sign up to follow this writer.
WriterShelf is a privacy-oriented writing platform. Unleash the power of your voice. It's free!
Sign up. Join WriterShelf now! Already a member. Login to WriterShelf.
寫程式中、折磨中、享受中 ......
959   0  
·
2019/04/17
·
4 mins read


在 Rails 中,使用 Cookies 真是非常的簡單,基本上,就給 cookies 取個名,就可以讀取了,例如:

# 最基本的設定
cookies[:name] = "david" 
# 這樣存的資料是公開的,使用者可以自由的讀取更改,讀的時候就讀 cookies[:name]
# cookie 的名稱就是“name"

# 要讓 cookie 一直存在,又懶得設定有效期: 
cookies.permanent[:login] = "XJ-122"
# 這等於是設定 expires: 20.year.from_now,

cookies.delete :name
# 刪除 cookies[:name] 


# 詳細的 cookie 設定,一般不需要這樣設定,只有當你真的有特別需求時
cookies[:name] = {
  value: 'david, the user name',
  path: '/foo',
  # 指定儲存路徑,一般別用,除非你確定這個路徑是你能存取的,瀏覽器可能會拒絕存取
  expires: 1.year.from_now,
  # cookie 有效期
  domain: 'domain.com',
  # or domain: nil (這是預設值,沒有網域限定), 
  # domain: :all (限定 domain.com 跟 sub.domain.com 使用), 
  # domain: %w(.example.com .example.org) (給兩個網域使用)
  tld_length: 2,
  # 一般不用設定,只有在 domain: :all 時,需要限定頂級域長度時使用,例如在 example.com 這個網域中,頂級域是.com,tld_length: 要設成 2
  secure: false,
  # 限定這 cookie 只給 HTTPS 使用時就要設成 true,預設是 false,也就是 https 跟 http 都可以用。
  httponly: false 
  # 限定這 cookie 只給 HTTP 程序使用時就要設成 true,預設是 false,也就是不限制使用。
}

很簡單吧,以下是一些常用的 rails cookies 使用方式:

保護 cookie 的內容:

有很多時候,我們不希望 cookies 的內容被使用者看到,甚至更改,這時,就要使用:

cookies.signed[:name] = current_user.name 
# 這樣存的資料是被用 ActiveSupport::MessageVerifier 使用 “secrets.secret_key_base” 以 base64 轉換,瀏覽器的使用者可以讀,看到的是亂碼,但是值是被保護不能改變的。

cookies.encrypted[:site_name_sc_amt] = shoping_cart.amount 
# 這樣存的資料是被用 ActiveSupport::MessageEncryptor 使用 “secrets.secret_key_base” 鎖碼的,瀏覽器的使用者可以讀,看到的是亂碼,值也是被保護不能改變的。

signed 與 encrypted 的差異不大,一般使用上,如果只是不想要 cookies 的值被改,就用 signed,連值都不想被看到,就用 encrypted。

一次存取兩個以上的值

cookies 就只能存“一個”字串,要存一堆“值”的時候怎麼辦?很簡單,把“值”變成 JSON 存進去!

# 存一對數字
cookies[:a_pair] = JSON.generate([47.68, -122.37])
# 讀回第一個數字
JSON.parse(cookies[:a_pair])[0].to_i
其他常用方法:

signed 與 encrypted 可以跟 permanent 混用,這也是大部分 rails 使用者的用法,

# 將 permanent 與 sign/encrypted 連用:
cookies.permanent.signed[:login] = "XJ-122"
cookies.permanent.encrypted[:login] = "XJ-122"

# 讀取時,可以省略 .permanent,如:
cookies.signed[:login]
cookies.encrypted[:login]

例外處理:Cookie 的存取是瀏覽器決定的,我的習慣是測試完後,將 cookies 的程式部分用 begin rescue end 包起來,畢竟,瀏覽器會怎麼報錯,很難說。

Cookie 管理:用瀏覽器就可以管理 cookie,在 Chrome 上,叫出「開發人員工具」, 選 application / Storage / Cookies 就可以更改刪除這一個 cookie 了, 實務上,我開發程式時都先不用 signed 或是 encrypted,這樣可以很容易的更改 cookie,開發測試完後,再改成 signed 或是 encrypted。

歐盟 GDPR:雖然歐盟對 cookies 的使用告知規定是制約網站由歐盟成員所有,但是 GDPR 也對網站有歐盟人士使用保護,如果你的網站是針對歐盟市場,你就必須要完備與加註 GDPR 警語,這裡有個 rails gem,也許對你網站需要 GDPR 警語時有幫助:

infinum/cookies_eu — Gem to add cookie consent to Rails application. Contribute to infinum/cookies_eu development by creating an account on GitHub.
GitHub

參考:


WriterShelf™ is a unique multiple pen name blogging and forum platform. Protect relationships and your privacy. Take your writing in new directions. ** Join WriterShelf**
WriterShelf™ is an open writing platform. The views, information and opinions in this article are those of the author.


Article info

This article is part of:
Categories:
Tags:
Total: 939 words


Share this article:
About the Author

很久以前就是個「寫程式的」,其實,什麼程式都不熟⋯⋯
就,這會一點點,那會一點點⋯⋯




Join the discussion now!
Don't wait! Sign up to join the discussion.
WriterShelf is a privacy-oriented writing platform. Unleash the power of your voice. It's free!
Sign up. Join WriterShelf now! Already a member. Login to WriterShelf.