cookie的優(yōu)化與購(gòu)物車(chē)實(shí)例
一 Cookie 的優(yōu)化
1.1 一般而言,我們?cè)O(shè)置cookie是在php中設(shè)置
例如:
<?php setcookie('testKey1','hello world',0,'/'); //# 當(dāng) expires = 0 時(shí),此Cookie隨瀏覽器關(guān)閉而失效,?>
而在驗(yàn)證的時(shí)候,我們通常是:
<?php if(isset($_COOKIE['testKey2'])) echo "The New COOKIE is : testKey2 = ".$_COOKIE['testKey2']; else echo "The new COOKIE is setting failed"; ?>
都是在服務(wù)端進(jìn)行。優(yōu)化:
1.2 在前端頁(yè)面進(jìn)行驗(yàn)證cookie
cookie保存在客戶(hù)端,那么可以在客戶(hù)端那邊進(jìn)行驗(yàn)證,根據(jù)上面的代碼,前端獲取代碼為:
<script language="JavaScript" type="text/javascript"> var key1 = document.cookie.match(new RegExp("(^| )testKey1=([^;]*)(;|$)")); //正則找出testKey的cookie值 try{ if(key1[2] != '') document.write("testKey1 = "+key1[2]); }catch(e){ document.write("testKey1 = NULL"); };
那么我們能否在前端設(shè)置cookie 呢 ?
1.3 在前端頁(yè)面設(shè)置cookie【購(gòu)物車(chē)原理】
function setCookie(){ var expire = new Date(); expire.setTime(expire.getTime() + 86400000); document.cookie = "testKey2=This the second Cookie;expires=" + expire.toGMTString() + ";path=/"; alert('完成設(shè)置'); location.href='test2.php' }
這樣子能夠減輕服務(wù)器的壓力
我們要注意,這樣子是有限制的,瀏覽器本身能夠存儲(chǔ)的數(shù)據(jù)有限:
上述是從網(wǎng)上找來(lái),僅供參考,如果我們要存儲(chǔ)更多的數(shù)據(jù)??梢允褂茫?/p>
1.4 local storage
在谷歌瀏覽器下,f12可以看到:
這個(gè)可以看成是瀏覽器的小型數(shù)據(jù)庫(kù),可以存儲(chǔ)更多的數(shù)據(jù)。
示例【購(gòu)物車(chē)小試】:
設(shè)置頁(yè)面:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Demo2</title> <script language="JavaScript" type="text/javascript"> var cartLSName = 'abc'; //gdsInfo=[ID,NAME,AVATAR,PRICE,NUMBER] function addToLS(gdsInfo){ if(!window.localStorage){ alert('您的瀏覽器不支持Local Storage!'); //如果不支持,可以采用第1.3中的方法 return false; } try{ if(gdsInfo.length != 5){ alert('參數(shù)錯(cuò)誤!'); return false; } }catch(e){alert('參數(shù)錯(cuò)誤!');return false} var gName=gdsInfo[1]; gdsInfo[1]=encodeURI(gdsInfo[1]); gdsInfo[4]=parseInt(gdsInfo[4]); if(isNaN(gdsInfo[4])) gdsInfo[4] = 1; //由JSON字符串轉(zhuǎn)換為JSON對(duì)象 var cartLS = JSON.parse(localStorage.getItem(cartLSName)); if(cartLS == null){ cartLS=[gdsInfo]; }else{ var existInCart=false; for(var i=0;i<cartLS.length;i++){ if(cartLS[i][0] == gdsInfo[0]){ cartLS[i][4] += gdsInfo[4]; existInCart = true; break; } } if(!existInCart) cartLS.splice(0,0,gdsInfo); } //將JSON對(duì)象轉(zhuǎn)化為JSON字符,并存入LocalStorage localStorage.setItem(cartLSName,JSON.stringify(cartLS)); return true; } </script> </head> <body> <a href="javascript:addToLS([3,'華為Mate8','ico.jpg',3888.00,2]);" rel="external nofollow" >存儲(chǔ)一</a><br /> </body> </html>
效果:
有設(shè)置,就有查看:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Show LocalStorage Info</title> <script language="JavaScript" type="text/javascript"> if(!window.localStorage){ alert('您的瀏覽器不支持Local Storage!'); } var cartLSName = 'abc'; var cartStr = localStorage.getItem(cartLSName) //gdsInfo=[ID,NAME,AVATAR,PRICE,NUMBER] function showStr(){ str = decodeURIComponent(cartStr); alert(str); document.getElementById('show').innerHTML=str; } function showInfo(){ var cartLS = JSON.parse(cartStr); if(cartLS == null){ alert(NULL); }else{ var str = ''; for(var i=0;i<cartLS.length;i++){ str += "ID:"+cartLS[i][0] + "\n"; str += "Name:"+cartLS[i][1] + "\n"; str += "Logo:"+cartLS[i][2] + "\n"; str += "Price:"+cartLS[i][3] + "\n"; str += "Num:"+cartLS[i][4] + "\n"; } str = decodeURIComponent(str); alert(str); document.getElementById('show').innerHTML=str.replace(/\n/g,"<br />"); } } function clearLS(){ localStorage.clear(); } </script> </head> <body> <a href="javascript:showStr();" rel="external nofollow" >以字符串形式顯示</a><br /> <a href="javascript:showInfo();" rel="external nofollow" >顯示詳細(xì)</a><br /> <a href="javascript:clearLS();" rel="external nofollow" >清空</a><br /> <a href="./" rel="external nofollow" >返回設(shè)置頁(yè)面</a><br /> <div id="show"></div> </body> </html>
效果:
以字符串形式顯示
顯示詳細(xì)
以上這篇cookie的優(yōu)化與購(gòu)物車(chē)實(shí)例就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
php基于CodeIgniter實(shí)現(xiàn)圖片上傳、剪切功能
這篇文章主要為大家詳細(xì)介紹了php基于CodeIgniter實(shí)現(xiàn)圖片上傳、剪切功能,具有參考價(jià)值,感興趣的朋友可以參考一下2016-05-05Laravel實(shí)現(xiàn)數(shù)據(jù)庫(kù)遷移與支持中文的填充
最近在學(xué)習(xí)Laravel數(shù)據(jù)庫(kù)方面的內(nèi)容,發(fā)現(xiàn)了一些資料不錯(cuò)整理出來(lái)分享給大家,下面這篇文章主要給大家介紹了關(guān)于Laravel實(shí)現(xiàn)數(shù)據(jù)庫(kù)遷移與支持中文填充的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考下。2017-11-11php微信開(kāi)發(fā)之帶參數(shù)二維碼的使用
這篇文章主要為大家詳細(xì)介紹了php微信開(kāi)發(fā)之帶參數(shù)二維碼的使用,感興趣的小伙伴們可以參考一下2016-08-08淺談PHP調(diào)用Webservice思路及源碼分享
NuSoap是PHP環(huán)境下的WebService編程工具,用于創(chuàng)建或調(diào)用WebService。它是一個(gè)開(kāi)源軟件,是完全采用PHP語(yǔ)言編寫(xiě)的、通過(guò)HTTP收發(fā)SOAP消息的一系列PHP類(lèi)。NuSOAP的一個(gè)優(yōu)勢(shì)是不需要擴(kuò)展庫(kù)的支持,這種特性使得NuSoap可以用于所有的PHP環(huán)境,不受服務(wù)器安全設(shè)置的影響。2014-06-06Laravel5.6框架使用CKEditor5相關(guān)配置詳解
這篇文章主要介紹了Laravel5.6框架使用CKEditor5相關(guān)配置,結(jié)合實(shí)例形式詳細(xì)分析了Laravel5.6框架整合CKEditor5編輯器相關(guān)操作技巧與注意事項(xiàng),需要的朋友可以參考下2019-07-07php nginx 實(shí)時(shí)輸出的簡(jiǎn)單實(shí)現(xiàn)方法
本文通過(guò)實(shí)例代碼給大家介紹了php nginx 實(shí)時(shí)輸出的簡(jiǎn)單實(shí)現(xiàn)方法,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下吧2018-01-01Yii框架結(jié)合sphinx,Ajax實(shí)現(xiàn)搜索分頁(yè)功能示例
這篇文章主要介紹了Yii框架結(jié)合sphinx,Ajax實(shí)現(xiàn)搜索分頁(yè)功能,結(jié)合實(shí)例形式分析了Yii框架中使用sphinx與Ajax實(shí)現(xiàn)搜索結(jié)果的分頁(yè)展示效果,需要的朋友可以參考下2016-10-10golang實(shí)現(xiàn)php里的serialize()和unserialize()序列和反序列方法詳解
這篇文章主要介紹了golang實(shí)現(xiàn)php里的serialize()和unserialize()序列和反序列方法詳解,需要的朋友可以參考下2018-10-10