I have encountered an interesting question and don't know how to say it. let's take a look at the code first. let's talk about it: $ catid & nbsp ;=& nbsp ;$ _ GET ['id']; if (& nbsp ;! Isset (& nbsp; $ _ COOKIE [& nbsp; $ cookie_name & nbsp;] & n has encountered an interesting problem. I don't know what to say. let's take a look.
Let's look at the Code First:
$ Catid = $ _ GET ['id'];
If (! Isset ($ _ COOKIE [$ cookie_name]) {
$ Cart_ary = array ();
$ Cart_ary ['cart'] = array ();
$ Cart_ary ['item _ num'] = 0;
$ Cart_ary ['total _ price'] = 0.00;
Setcookie ($ cookie_name, json_encode ($ cart_ary), time () + (7*24*60*60 ));
}
// The value cannot be obtained for the first time, so we need to suppress the first error.
$ Cart_ary = json_decode (str_replace ("\", "", @ $ _ COOKIE [$ cookie_name]), true );
If (isset ($ cart_ary ['cart'] [$ catid]) {
$ Cart_ary ['cart'] [$ catid] ++;
} Else {
$ Cart_ary ['cart'] [$ catid] = 1;
}
$ Cart_ary ['item _ num'] = count_cart_item_num ($ cart_ary ['cart']);
$ Cart_ary ['total _ price'] = count_cart_total_price ($ cart_ary ['cart']);
Setcookie ($ cookie_name, json_encode ($ cart_ary), time () + (7*24*60*60 ));
The specific problem is as follows:
Because $ cart_ary is a two-dimensional array, I need to serialize it when saving it in the cookie. at the beginning, I use the serialize () and unserialize () functions, but I don't know what's going on. during deserialization, the error "offset xxx" was reported. later, Baidu said it was a encoding problem, but I am on the same page, and are UTF-8 code, so this problem I think is not, then the first question: how can we see this problem ??
Then I switched to the json_encode () and json_decode () functions, but at the same time, a problem was found: when the cookie is stored in the cookie after encoding, the built-in mechanism of the cookie should be to escape the serialized data, the backslash "\" is added before the double quotation marks, so I have to remove it, just like this;
str_replace( "\\","",@$_COOKIE[ $cookie_name ] )
Then the second question is:
I mentioned above that the built-in mechanism of cookie will do something to the string to be saved ??
Well, I know Baidu, but I have never found it. so I will discuss it with you! Expert Mo Xiao ~ Php json
------ Solution --------------------
Are you kidding?
At least.
$catid = $_GET['id'];
if( !isset( $_COOKIE[ $cookie_name ] ) ){
$cart_ary = array();
$cart_ary['cart'] = array();
$cart_ary['item_num'] = 0;
$cart_ary['total_price'] = 0.00;
setcookie( $cookie_name,json_encode( $cart_ary ),time()+(7*24*60*60) );
}else {
$cart_ary = json_decode( str_replace( "\\","",@$_COOKIE[ $cookie_name ] ),true );
if( isset( $cart_ary['cart'][ $catid ] ) ){
$cart_ary['cart'][ $catid ]++ ;
}else{
$cart_ary['cart'][ $catid ] = 1;
}
$cart_ary['item_num'] = count_cart_item_num( $cart_ary['cart'] );
$cart_ary['total_price'] = count_cart_total_price( $cart_ary['cart'] );
setcookie( $cookie_name,json_encode( $cart_ary ),time()+(7*24*60*60) );
}
What is count_cart_item_num?
------ Solution --------------------
1. this error is caused by the destruction of your original serialized data. how can this problem be solved? See Section 2
2. you have enabled automatic escape, that is, Magic Quotes. before the program starts, PHP has automatically helped you escape external commit data, that is, $ _ GET, $ _ POST, $ _ COOKIE. So you will see an even more \
You need to turn Off the magic quotes and modify php. ini magic_quote_gpc = Off.