if(isset($_POST["cartDelete"])){
$item_ID = $_POST["hidden_id"];
$cookie_data = stripslashes($_COOKIE['shopping_cart']);
$cart_data = json_decode($cookie_data, true);
unset($cart_data[$item_ID]);
$item_data = json_encode($cart_data);
setcookie('shopping_cart', $item_data, time() + (86400 * 30));
header("Location: cart");
print_r($cart_data);
}
if(isset($_POST["QTYedited"])){
$item_ID = $_POST["hidden_id"];
$item_QTY = $_POST["QTYedited"];
$cookie_data = stripslashes($_COOKIE['shopping_cart']);
$cart_data = json_decode($cookie_data, true);
$cart_data[$item_ID]["item_QTY"] = $item_QTY;
$item_data = json_encode($cart_data);
setcookie('shopping_cart', $item_data, time() + (86400 * 30));
header("Location: cart");
}
I'm writing an webshop in php, but in this part the cookie somehow won't be saved. The whole fun is, with one item in the cart everything is working. The problem beginning with at least two items.
I've tried everything I knew. You can test the page with webshop.abrisx.nhely.hu
First, make sure that the cookie is enabled in your browser and note that some illegal characters will cause the cookie not to be stored,
Try calling urlencode() on your string before sending it to the cookie.
ex:
setcookie('shopping_cart', urlencode($item_data), time() + (86400 * 30), '/');
I think this will solve your problem.
Related
I have a shopping cart which I store in a cookie. The cookie 'shopping_cart' is set once an item is added onto the cart.
function shopping_cart_add(){
if(isset($_COOKIE['shopping_cart'])){
$cookie_data = $_COOKIE['shopping_cart'];
$cart_data = json_decode($cookie_data, true);
}else{
$cart_data = array();
}
$data = array(
"cart_id" => 1,
"product_id" => $this->input->post('product_id'),
"product_name" => $this->input->post('product_name'),
"product_price" => $this->input->post('product_price'),
"quantity" => $this->input->post('quantity'),
"options" => $this->input->post('options'),
"description" => $this->input->post('description'),
"image" => $this->input->post('image')
);
$cart_data[] = $data;
$item_data = json_encode($cart_data);
setcookie('shopping_cart',$item_data,time() + (86400 * 30));
$_COOKIE['shopping_cart'] = $item_data;
}
I am having a problem with when the user wants to remove an item from cart. I am using codeigniter framework but not its inbuilt cookies.
The following is the code in my view for deleting an item:
<td>Delete</td>
The following is the code in my controller for deleting an item:
function delete($delete_id){
$cookie_data = $_COOKIE['shopping_cart'];
$cart_data = json_decode($cookie_data, true);
foreach ($cart_data as $key => $value) {
if ($cart_data[$key]['product_id'] == $delete_id) {
unset($cart_data[$key]);
$item_data = json_encode($cart_data);
setcookie('shopping_cart',$item_data,time() + (86400 * 30));
header("location:/category/cart_summary.php?remove=1");
}
}
}
However I keep getting an error when I click the delete button.
Undefined variable $_COOKIE['shopping_cart']
Yet when I check my browser it show the cookie exists. What could be the problem?? I have already checked similar questions on stack pertaining to this issue but none has helped.
This includes:
undefined index for cookie in some browsers
PHP Undefined Index When Checking Cookie Value And Cookie Exists
This probably happens because you didn't explicitly mentions on what path the shopping_cart cookies will be saved. Try to add a 4th parameter (path) at each setcookie :
setcookie('shopping_cart',$item_data,time() + (86400 * 30), '/');
In a webshop product page, i am sending the product id with ajax to this php file:
<?php
include_once("connect.php");
if(isset($_POST['product_id']))
{
$product_id = mysqli_real_escape_string($kapcs, $_POST['product_id']);
$cookie_name = "kedvenc_termek";
$cookie_value = $product_id;
setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/");
echo 'Saved to the favorit products.';
}
?>
My problem is, that when i have id-s in the kedvenc_termek cookie, and i want to add another product id as favorit product, the new cookie value will overwrite the value in the cookie.
For example, if i have 179 stored in the cookie, and i add the 180 product id to it, the cookie value will be 180, and not 179,180.
You're overwriting the value every time. So you need to append the value, not overwrite it
$cookie_name = "kedvenc_termek";
$cookie_value = '';
if(isset($_COOKIE[$cookie_name])) {
$cookie_value = $_COOKIE[$cookie_name] . ',';
}
$cookie_value .= $product_id;
setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/");
You need to use an array to save the data. You can use PHP's serialize() method to save array data as a string, and use unserialize() to read the data. Try this:
<?php
include_once("connect.php");
if(isset($_POST['product_id']) && is_int($_POST['product_id'])) {
$product_id = $_POST['product_id'];
$cookie_name = "kedvenc_termek";
$data = unserialize($_COOKIE[$cookie_name]);
if(!in_array($product_id, $data)) {
$data[] = $product_id;
}
setcookie($cookie_name, serialize($data), time() + (86400 * 30), "/");
echo 'Saved to the favorite products.';
}
?>
I randomly choose a dominant color when the visitor arrive on the website, then I want to store this color in a cookie during 1 hour.
And I'm working on wordpress.
For the moment I've got this on my function.php
add_action( 'init', 'setting_my_first_cookie' );
function setting_my_first_cookie() {
$colourRange = array('#965c5d', '#5f797a', '#bc8b6a', '#7fc3a2', '#89383a', '#f28c5d');
$colourTheOnlyOne = $colourRange[array_rand($colourRange, 1)];
$cookieColor = 'cookieColor';
$cookieValue = $colourTheOnlyOne;
setcookie( $cookieColor, $cookieValue, 60 * DAYS_IN_SECONDS, COOKIEPATH, COOKIE_DOMAIN );
}
The problem is every time I refresh the page a new color is set… don't understand why !
You need to check the cookie value to see if it there to avoid resetting it. You are essentially overwriting your cookie every time you reload the page.
add_action( 'init', 'setting_my_first_cookie' );
function setting_my_first_cookie() {
$cookieColor = 'cookieColor';
if (!isset($_COOKIE[$cookieColor])) {
$colourRange = array('#965c5d', '#5f797a', '#bc8b6a', '#7fc3a2', '#89383a', '#f28c5d');
$cookieValue = $colourRange[array_rand($colourRange, 1)];
setcookie( $cookieColor, $cookieValue, 60 * DAYS_IN_SECONDS, COOKIEPATH, COOKIE_DOMAIN );
}
}
This checks for the cookie 'cookieColor' and if it is found, does not write the cookie.
So I finally solve my problem with that and after in the template I check if a cookie is available, if not I display the
$colorRange = array('#965c5d', '#5f797a', '#bc8b6a', '#7fc3a2', '#89383a', '#f28c5d');
$cookieValue = $colorRange[array_rand($colorRange, 1)];
global $cookieValue;
add_action( 'init', 'setting_cookie' );
function setting_cookie() {
global $cookieValue;
$cookieTheValue = $cookieValue;
$cookieColor = 'cookieColor';
if (!isset($_COOKIE[$cookieColor])) {
setcookie( $cookieColor, $cookieTheValue, time()+60, COOKIEPATH, COOKIE_DOMAIN );
}
}
i have this code for creating cookie
This works in firefox and crome browser but in IE it is creating cookie again and again
if (!isset($_COOKIE["cook"])) {
$expire = time() + 60 * 60 * 24 * 30 * 2;
$data = array(
"ip" => $_SERVER['REMOTE_ADDR'],
"browser" => $_SERVER['HTTP_USER_AGENT'],
"create_time" => $now
);
$result = $db->insert("cookies", $data);
$cookie_id = $db->lastid;
$cookie_id = my_encrypt($cookie_id);
setcookie("cook", $cookie_id, $expire,"/","localhost");
} else {
$cookie_id = $_COOKIE["cook"];
}
Everytime I visit page it creates new cookie
omit the domain-parameter for setcookie()
I am new to cookies. I created a user login php class that uses a cookie to store a unique MD5 key to remember a user that has logged in. However, the cookie is not being reset when the user logs out. I created a function from code I found on stack overflow to clear the cookies on logout.
static public function clearCookies()
{
$past = time() - 3600;
foreach ( $_COOKIE as $key => $value )
{
$value = '';
setcookie( $key, $value, $past );
setcookie( $key, $value, $past, '/' );
}
}
However, the cookie is still not being cleared.
This is the line of code that sets the cookie
setcookie("auth_key", $authKey, time() + 60 * 60 * 24 * 7);
Thanks ahead of time
Try to add
static public function clearCookies()
{
$past = time() - 3600;
foreach ( $_COOKIE as $key => $value )
{
$value = '';
setcookie( $key, $value, $past );
setcookie( $key, $value, $past, '/' );
unset($_COOKIE[$key]);
}
}
You have to note that changed cookies are readable AFTER sending them to client (if you do not set them manually via $_COOKIE), so the next refresh.
Here is the solution that worked.
I changed
setcookie("auth_key", $authKey, time() + 60 * 60 * 24 * 7);
to
setcookie("auth_key", $authKey, time() + 60 * 60 * 24 * 7, '/');
It seems that the cookie was not being reset because the url it was being reset from was different than the url it was set in. After adding '/' it could be reset from the new url.