Optimization of cookies and test of shopping cart and test of COOKIE shopping cart
1. Cookie Optimization
1.1 In general, we set cookies in php.
For example:
<? Phpsetcookie ('testkey1', 'Hello world', 0, '/'); // # When expires = 0, this Cookie becomes invalid when the browser is closed,?>
During verification, we usually use the following methods:
<?phpif(isset($_COOKIE['testKey2'])) echo "The New COOKIE is : testKey2 = ".$_COOKIE['testKey2'];else echo "The new COOKIE is setting failed";?>
All are performed on the server. Optimization:
1.2 verify the cookie on the front-end page
If the cookie is stored on the client, you can verify it on the client side. According to the above Code, the front-end obtains the Code as follows:
<Script language = "JavaScript" type = "text/javascript"> var key1 = document. cookie. match (new RegExp ("(^ |) testKey1 = ([^;] *) (; | $ )")); // obtain the cookie value of testKey in regular expressions. try {if (key1 [2]! = '') Document. write (" testKey1 = "+ key1 [2]);} catch (e) {document. write (" testKey1 = NULL ");};
So can we set cookies on the front end?
1.3 set cookie on the front-end page [shopping cart principle]
Function setCookie () {var expire = new Date (); expire. setTime (expire. getTime () + 86400000); document. cookie = "testKey2 = This the second Cookie; expires =" + expire. toGMTString () + "; path =/"; alert ('finish settings'); location. href = 'test2. php '}
This reduces the pressure on servers.
We should note that there are limitations in this way, and the data that the browser itself can store is limited:
The above is obtained from the Internet for reference only. If we want to store more data. You can use:
1.4 local storage
In Google's browser, f12 can see:
This can be seen as a small browser database that can store more data.
Example ]:
Settings page:
<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
Effect:
If you have settings, you can view them:
<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
Effect:
Display in string format
Show Details