PHP Simple implementation of shopping cart

Source: Internet
Author: User

First, determine if you are logged in,
if (Isset ($_session[' user_id ')) {
Exist
Add the user ID and product ID to the shopping cart table
}else{
Do not exist; Use cookies to put the commodity ID and host IP into an array
, the array is stored in a cookie, and the browser is allowed to store 300 cookies,
Each cookie is 4KB large enough to meet the requirements of the shopping cart and
Reduced load on the server
}
If the browser disables the cookie, use Memcache to implement

Another way to determine whether to log in,
if (Isset ($memcache->get (' user_id '))) {
Exist
Add the user ID and product ID to the shopping cart table
}else{
does not exist;
$memcache = new Memcache (); Instantiate Memchche
$data =[' goods_id ' = $goods _id, ' IP ' =>$_server
[' REMOTE_ADDR '];
$memcache->set (' key key ', serialize ($data));
}

There is no big difference between REDIS implementations and Memcache!


Both of these methods can be implemented, if you use the first, we have to
Set the expiration time of the cookie, if the second type is used, the value
You need to deserialize the unserialize (), and then you can get the
The Data!

PHP Simple implementation of shopping cart

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.