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.';
}
?>
Related
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.
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 am trying to insert cookie. The logic is simple as:
Check if the value is present .
If not, insert new value with comma separated form.
I have tried some code but I could not able to get a correct result.
In this code a new value should be inserted, which is happening but not getting old value.
Edited Code 2
$current_value = '';
if(!isset($_COOKIE['blog_id_cookie'])){
setcookie('blog_id_cookie', $id);
$current_value[] = $_COOKIE['blog_id_cookie'];
} else {
$current_value = explode(',', $_COOKIE['blog_id_cookie']);
}
if(!in_array($id, $current_value)){
$current_value[] = $id;
$cookie_name = "blog_id_cookie";
setcookie($cookie_name, implode(',', $current_valu));
}
With that code $current_value will be defined only if the cookies is not set. Set $current_value in the else part. Concatenate the cookie instead of $current_value. Try with -
Update
$current_value = '';
if(!isset($_COOKIE['blog_id_cookie'])){
setcookie('blog_id_cookie', $id);
$current_value[] = $_COOKIE['blog_id_cookie'];
} else {
$current_value = explode(',', $_COOKIE['blog_id_cookie']);
}
if(!in_array($id, $current_value)){
$current_value[] = $id;
$cookie_name = "blog_id_cookie";
setcookie($cookie_name, implode(',', $current_value));
}
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.