Optimization of cookies and test of shopping cart and test of COOKIE shopping cart

Source: Internet
Author: User

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

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.