cookie issue in IE only - php

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()

Related

PHP Webshop cookie won't be saved

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.

undefined index $_COOKIE

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), '/');

Get Time From 5 Minutes Prior

I'm working on User Functions for the CMS I'm working on and I have users updated on each page load Using
if(User("online") == true) {
LastOnlineUpdate($_SESSION['key']['userid']);
}
(Yes User Online Works, That's my function to check online users)
And now my times are updated correctly hooking into the LastOnlineUpdate function then running my class function
public function SessionUpdate($arg) {
$query = <<<SQL
UPDATE {$this->tprefix}online
SET remote = :remote, timestamp = :timestamp
WHERE userid = :arg
SQL;
$resource = $this->db->db->prepare( $query );
$resource->execute( array(
':remote' => $_SERVER['REMOTE_ADDR'],
':timestamp' => time(),
':arg' => $arg,
));
}
Now whenever I go through my pages it updates the user by having the first code within my preloaded file, now I've hit my roadblock though with something I've never done and that's to take the time that's been inserted into my table, select it if the time is less than five minutes and utilize it for my online users section
public function OnlineUsers() {
$query = <<<SQL
SELECT userid
FROM {$this->tprefix}online
WHERE timestamp > time(5Minutes)
SQL;
$resource = $this->db->db->prepare( $query );
$resource->execute();
$this->onlinecount = $resource->rowCount();
if($resource->rowCount() == 0 ) {
$this->onlineusers = "No User's Online";
}
else
{
foreach($resource as $row) {
self::ConvertIDToName("displayname"); //Can be display, user, first or whatever else for the argument, The onlineusers variable is then set based off that query
}
}
}
$minutes = 5;
$date = gmdate('Y-m-d H:i:s'); // http://php.net/manual/en/function.gmdate.php
$date_plus_minutes = gmdate('Y-m-d H:i:s', ( time() + $minutes * 60 ) );
$date_minus_minutes = gmdate('Y-m-d H:i:s', ( time() - $minutes * 60 ) );

Symfony 2.7.3 cookie issue

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;

Cookie not being reset in PHP

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.

Categories