2009年1月19日 星期一

cookies 設定

cookies api
# :value - The cookie‘s value or list of values (as an array).
# :path - The path for which this cookie applies. Defaults to the root of the application.
# :domain - The domain for which this cookie applies.
# :expires - The time at which this cookie expires, as a Time object.
# :secure - Whether this cookie is a only transmitted to HTTPS servers. Default is false.
# :http_only - Whether this cookie is accessible via scripting or only HTTP. Defaults to false.

cookies 的一些觀念
1.要在這個domain 跟 path之下的才能拿, 例如如果把domain設成
ilake.demo.com, demo.demo.com 這樣彼此是不能拿的, 可是設成 demo.com 便可以

2. cookies 誰都可以寫, 寫就是寫了, https 寫的 http 也可以拿, 但是可以設成只有 https 才可以拿, :secure 就是此選項, 而http_only 便是javascript 不能拿

3.不能把cookie domain 設成你現在server domain 以外的 domain

4.navigator.cookieEnabled 可以檢查cookie 功能是否開啟

5.cookie 不能包含分號 逗號 空白, 所以有時妳會需要用encodeURIComponent, 讀取時就要使用相對應的decodeURIComponent

6.刪除cookie, 就是把他直接設過去的時間即可

7.cookie 只能有4K, cookie 在妳request 其符合規則網頁時(same domain, path etc...)
就算妳沒用它, 它也會上傳到server


另外 rails cookies 裡有 subdomain_fu 在localhost 這種 domain 之下, 有點問題, 把他弄成demo.com 這樣正常的domain, 便可以正常拿到該domain 下的cookies

沒有留言: