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.
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.
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 am quite new to Symfony, and I want to set up a website were an user can select different board to display. All the board are in my twig template and are hidden if the value of the cookie is 0. If the user click on the menu, the value of the cookie will change to 1, displaying the board.
However, when I click on the menu, the first time, it does not change anything, but the second time, it work perfectly.
This is how I set up my cookie:
$var = 0;
$response = new Response();
$response->headers->setCookie(new Cookie('stream', $var, time() + 3600));
$response->send();
And this is how I change the value of the cookie:
$response = new Response();
$cookie = $this->getRequest()->cookies->get('stream');
$var = 1;
$response->headers->setCookie(new Cookie('stream', $var, time() + 3600));
return $response;
Edit: Here is my controller
public function streamAction()
{
$advert = $this->getAdvertEntity();
$stream = $this->getStreamEntity();
$cookie = $this->getRequest()->cookies->get('stream');
if (!isset($cookie) || $cookie == 0) {
$var = 1;
$response->headers->setCookie(new Cookie('stream', $var, time() + 3600));
$response->sendHeaders();
return $this->redirectToRoute('stream', array(
"adverts" => $advert,
"liststream" => $stream
));
}
$content = $this->get('templating')->render('IVPlatformBundle:Advert:Advert.html.twig', array(
"adverts" => $advert,
"liststream" => $stream
));
return new Response($content);
}
I really don't know what is wrong, so any help will be nice :)
Thanks
If you first time visit the route, cookie is not set in request, you receive it in response. Solution is to make redirect to the same route if cookie changed:
$cookie = $this->getRequest()->cookies->get('stream');
if (!isset($cookie) || $cookie == 0) {
$var = 1;
$response->headers->setCookie(new Cookie('stream', $var, time() + 3600));
$response->sendHeaders();
return $this->redirectToRoute('actual_route');
}
// Do other stuff if cookie set to 1.
return $response;
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()