1. Apa itu kue?
A: Cookie digunakan untuk menyimpan informasi sesi pada klien.
2. Komponen cookie?
① Nama: Nama unik untuk cookie. Disarankan untuk menjadi peka huruf besar -kecil. Nama cookie harus dikodekan URL.
② Nilai: Nilai string yang disimpan dalam cookie. Nilai harus dikodekan dengan URL.
③Domain: Domain mana yang berlaku untuk cookie. Semua permintaan yang dikirim ke domain ini akan berisi informasi cookie ini. Nilai ini dapat berisi subdomain (subdomain, seperti www.wrox.com) atau tidak (subdomain, seperti .wrox.com, yang berlaku untuk semua subdomain dari Wrox.com). Jika tidak diatur secara eksplisit, maka domain ini akan dipertimbangkan dari domain tempat cookie diatur.
④Path: Untuk jalur itu di domain yang ditentukan, cookie harus dikirim ke server. Misalnya, Anda dapat menentukan bahwa cookie hanya dapat diakses dari http://www.wrox.com/books/, jadi halaman http://www.wrox.com tidak akan mengirim informasi cookie, bahkan jika permintaan tersebut berasal dari domain yang sama.
⑤ Waktu Pengecualian: Cap waktu yang menunjukkan kapan cookie harus dihapus. Secara default, semua cookie akan dihapus di akhir sesi browser; Namun, Anda juga dapat mengatur waktu penghapusan sendiri. Nilai ini adalah tanggal dalam format GMT (WDY, DD-MON-YYY HH: MM: SS GMT), yang menentukan waktu yang tepat ketika cookie harus dihapus. Oleh karena itu, cookie
Itu masih dapat disimpan di mesin pengguna setelah browser ditutup. Jika tanggal kedaluwarsa yang Anda tetapkan adalah waktu sebelumnya, cookie akan segera dihapus.
⑥ Bendera Keamanan: Setelah menentukan, cookie dikirim ke server hanya saat menggunakan koneksi SSL. Misalnya, informasi cookie hanya dapat dikirim ke https://www.wrox.com, sementara permintaan dari http://www.wrox.com tidak dapat mengirim cookie.
3. Daftar contoh cookie?
Berikut adalah contoh cookie lengkap yang akan kita hancurkan:
Set-Cookie: name = value; kedaluwarsa = Senin, 22-Jan-07 07:10:24 GMT; domain = .wrox.com; jalur =/; aman
① Nama: nama representasi string
② Nilai: Nilai Representasi String
③ Waktu Pengecualian: Mon, 22-Jan-07 07:10:24 GMT
④ Nama Domain: .wrox.com
⑤Path: Direktori saat ini/
⑥ Tanda Keselamatan: Aman
4. Bagaimana cara mengoperasikan cookie?
var cookieutil = {// Setel cookie set: function (nama, nilai, kedaluwarsa, domain, path, aman) {var cookietext = ""; cookietext + = encodeuricomponent (name) + "=" + encodeuricomponent (value); if (kedaluwarsa tanggal dari tanggal) {cookietext + = "; kedaluwarsa =" + Expires.togmtString (); } if (path) {cookietext + = "; path =" + path; } if (domain) {cookietext + = "; domain =" + domain; } if (aman) {cookietext += "; aman"; } document.cookie = cookietext; }, // name = value; kedaluwarsa = kedaluwarsa_time; path = domain_path; domain = domain_name; aman // Dapatkan cookie get: function (name) {var cookiename = encodeuricomponent (name) + "=", cookiestart = document.cookie.indexof (cookiename), cookievalue = ""; if (cookiestArt> -1) {var cookieend = document.cookie.indexof (";", cookiestArt); if (cookieEnd = -1) {cookieEnd = document.cookie.length; } cookievalue = decodeuricomponent (document.cookie.substring (cookiestart + cookiename.length, cookieend)); } return cookievalue; }, // hapus cookie unset: function (nama, domain, path, aman) {this.set (name, "", date (0), domain, path, aman); }}; // uji cookieutil.set ("name", "zhang"); var name = cookieutil.get ("name"); peringatan (nama); // zhang cookieutil.unset ("name"); peringatan (cookieutil.get ("name")); // kosong5. Pembatasan Cookie
① Jumlah data yang disimpan terbatas
② Informasi yang disimpan dalam cookie hanya dapat diakses oleh penerima yang disetujui, tetapi tidak dapat diakses oleh domain lain.
③ Keamanan Terbatas
6. Menanggapi masalah sejumlah kecil penyimpanan data cookie, kami telah mengusulkan konsep sub-cookies. Artinya, beberapa bagian data disimpan dalam nilai masing -masing cookie, dipisahkan oleh "&".
var SubCookieUtil = { /** Set a complete cookie * param name : indicates the name of the cookie, required * param subCookies : indicates the value of the cookie, for an object, required * param expires : indicates the expiration time of the cookie, you can not fill in * param domain : indicates the domain name of the cookie, you can not fill in * param path : indicates the path of the cookie, you can not fill in * param secure : menunjukkan bendera keamanan cookie, Anda tidak dapat mengisi * misalnya: subcookieutil.setall ("info", {name: "zhang", usia: 23}); **/ setall: function (name, subcookies, Expires, Domain, Path, Secure) {var cookietext = "", subname, cookieparts = []; cookietext + = encodeuricomponent (name) + "="; untuk (subname dalam subkookies) {cookieparts.push (encodeuricomponent (subname) + "=" + encodeuricomponent (subkookies [subname]))); } if (cookieparts.length> 0) {cookietext += cookieparts.join ("&"); if (kedaluwarsa tanggal dari tanggal) {cookietext + = "; kedaluwarsa =" + Expires.togmtString (); } if (path) {cookietext + = "; path =" + path; } if (domain) {cookietext + = "; domain =" + domain; } if (aman) {cookietext += "; aman"; }} else {cookietext + = "; Expires =" + date (0) .togmtString (); } document.cookie = cookietext; }, /** Set a subcookie * param name: Indicates the name of the cookie, required * param subName: Indicates the name of the child cookie, Required * param value : Indicates the value of the child cookie, Required * param expires : Indicates the expiration time of the cookie, you can not fill in * param domain : Indicates the domain name of the cookie, you can not fill in * param path : Indicates the path of the cookie, you can not Isi * param aman: Menunjukkan bendera keamanan cookie, Anda tidak dapat mengisi * misalnya: subcookieutil.set ("info", "sex", "boy"); **/ set: function (nama, subname, nilai, kedaluwarsa, domain, path, aman) {var cookies = this.getall (name) || {}; cookie [subname] = nilai; this.setall (nama, cookie, kedaluwarsa, domain, jalur, aman); }, /** Baca cookie lengkap * Param Name: Menunjukkan nama cookie, wajib * Kembalikan: objek cookie * misalnya: subcookieutil.getall ("info"); **/ getAll: function (name) {var cookiename = encodeuricomponent (name) + "=", cookiestart = document.cookie.indexof (cookiename), cookievalue = "", i, len, subkoki, bagian, hasil = {}; if (cookiestArt> -1) {var cookieend = document.cookie.indexof (";", cookiestArt); if (cookieEnd == -1) {cookieEnd = document.cookie.length; } cookievalue = decodeuricomponent (document.cookie.substring (cookiestart + cookiename.length, cookieend)); if (cookievalue.length> 0) {subcookies = cookievalue.split ("&"); untuk (i = 0, len = subcookies.length; i <len; i ++) {Parts = subkookies [i] .split ("="); hasil [decodeuricomponent (bagian [0])] = decodeuricomponent (bagian [1]); } hasil pengembalian; }} return null; }, /** Dapatkan nilai cookie anak* Param Nama: mewakili nama cookie, diperlukan* param subname: mewakili nama cookie anak* kembali: nilai cookie anak* misalnya: subcookieutil.get ("info", "name"); **/ get: function (name, subname) {var cookies = this.getall (name); if (cookies) {return cookies [subname]; } else {return null; } }, /** Delete a complete cookie * param name : indicates the name of the cookie, required * param domain : indicates the domain name of the cookie, you can not fill in * param path : indicates the path of the cookie, you can not fill in * param secure : indicates the security flag of the cookie, you can not fill in * eg : SubCookieUtil.unsetAll("info"); **/ unsetall: function (nama, domain, path, aman) {this.setall (name, "", date (0) .togmtString (), domain, path, aman); }, /** Delete a subcookie * param name : indicates the name of the cookie, required * param subName : indicates the name of the child cookie, required * param domain : indicates the domain name of the cookie, you can not fill in * param path : indicates the path of the cookie, you can not fill in * param secure : indicates the security flag of the cookie, you can not fill in * eg : Subcookieutil.unset ("info", "name"); **/ unset: function (name, subname, domain, path, secure) {var cookies = this.getAll (name); if (cookie) {hapus cookie [subname]; this.setall (nama, cookie, null, domain, jalur, aman); }}}}; // tes: var zhang = {name: "zhang", usia: 23, tinggi: "178cm", berat: "66kg"} // atur cookie lengkap subcookieutil.setall ("zhang", zhang); // Dapatkan cookie lengkap var zhang = subcookieutil.getall ("zhang"); peringatan (zhang.weight); // 66kg // Tambahkan kue anak ke Zhang Subcookieutil.set ("Zhang", "Sport", "Basketball"); // dapatkan Subcookie Alert (subcookieutil.get ("Zhang", "Sport")); // Basketball // Hapus subcookie subcookieutil.unset ("Zhang", "Age"); peringatan (subcookieutil.get ("zhang", "usia")); // Undefined // Hapus cookie lengkap subcookieutil.unsetall ("zhang"); peringatan (subcookieutil.getall ("zhang")); // Kesalahan telah dihapusDi atas adalah semua tentang artikel ini, saya harap ini akan membantu untuk pembelajaran semua orang.