Solved

Disappearing cookie value

Posted on 2009-05-16
13
192 Views
Last Modified: 2012-05-07
This is kind a difficult question i think.

when I click cart/?product_1=10
$_COOKIE['cart'] = 'product_1|10';

when I THEN click cart/?product_2=20
$_COOKIE['cart'] = 'product_1|10;product_2;20';

BUT when I CLICK SOMEWHERE ELSE IT DISAPPEARS
$_COOKIE['cart'] = 'product_2;20';

I've been working on it 10hours and nothing. HELP ME please
$get -> nvm
 
k_ = product_1 (PRODUCT);
v_ = 10 (PRICE)
 
---------------
if(!empty($_GET[1])) {
			$c_name = 'cart';	loadLib('url-val');	$get = url_parse($_GET[1]); foreach($get as $key => $val) { $k_ = $key; $v_ = $val; }
			
			if(isset($_COOKIE[$c_name])) {
				$_list = explode(';', $_COOKIE[$c_name]);
				foreach($_list as $k => $v) {
					$item = explode('|', $_list[$k]);
					$item = array($item[0] => $item[1]);
				}
				
				if(array_key_exists($k_, $item)) { if($v_ <> $item[$k_]) foreach($item as $k => $v) $item = array($k => $v_); }
				else { $item = array_merge($item, array($k_ => $v_)); }
				
				end($item);	$last = key($item);
				$arr = ''; foreach($item as $k => $v) if($k == $last) $arr .= $k.'|'.$v; else $arr .= $k.'|'.$v.';';
				if(!sendCookie($c_name, $arr, COOKIE_EXP_TIME)) die('sendCookie err');
			} else if(!sendCookie($c_name, $k_.'|'.$v_, COOKIE_EXP_TIME)) die(_MSG.'sendCookie err2');
		}
		
		print_r($_COOKIE);

Open in new window

0
Comment
Question by:AndyPSV
  • 7
  • 6
13 Comments
 
LVL 34

Expert Comment

by:Beverley Portlock
ID: 24402041
Use $_COOKIE to retrieve the values and "setcookie" to set them. Other things to watch for are

1. Set the expiry to a sufficiently far off future time
2. If you are traversing folders and directories, it is easiest to set the domain parameter to '/' so the cookie is available everywhere

See http://www.php.net/setcookie

0
 

Author Comment

by:AndyPSV
ID: 24402080
that's not the solution to the question
0
 
LVL 34

Accepted Solution

by:
Beverley Portlock earned 500 total points
ID: 24402146
"that's not the solution to the question"

I know. It is suggestion of things to check first. People do not always post all the code and leave out bits that can be vital. I was concerned by the code that you posted at the top where you are assigning things to the $_COOKIE array. That's why I made the comment.

0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 

Author Comment

by:AndyPSV
ID: 24402244
everything is ok, I think
anybody know the solution?

It's very important to me.
define('COOKIE_EXP_TIME', time()+24*3600);
 
--[auth]------
 
function sendCookie($name, $data, $exp) {
	return setcookie($name, $data, $exp, '/');
}
 
function delCookie($name) {
	return setcookie($name, '', 1, '/');
}
 
function isUlogd($name) {
	if(isset($_COOKIE[$name])) {
		if(empty($_COOKIE[$name])) die('ENGINE :: X cookie empty');
		$d = explode('|', $_COOKIE[$name]);
	}
	
	if(empty($d[0]) or empty($d[1])) { $u['is_logd'] = false; return $u; }
	elseif(!empty($d[0])) {
		$q = q('SELECT * FROM `'.PRFX.'u` WHERE u = "'.$d[0].'" AND p = "'.$d[1].'" AND act_lnk = "" LIMIT 1');
		if(n_r($q) == 0) $u['is_logd'] = false; else { $u = f($q); $u['is_logd'] = true; } return $u;
	} else { $u['is_logd'] = false; return $u; }
}

Open in new window

0
 
LVL 34

Expert Comment

by:Beverley Portlock
ID: 24402371
Sorry I have not responded earlier, I'm tackling some other stuff.

In your code you use sendCookie. Is that making a direct call to setcookie?
0
 
LVL 34

Expert Comment

by:Beverley Portlock
ID: 24402386
The attached seems to work OK for me. I've had to comment out one or two bits but I set the values you outlined, went elsewhere and the returned to the page and it seemed to hold the cookie.


<?php
 
 
//$get -> nvm
 
//k_ = product_1(PRODUCT);
//v_ = 10 (PRICE) -- -- -- -- -- -- -- - 
 
 
if (!empty ($_GET)) {
     $c_name = 'cart';
     //loadLib('url-val');
     //$get = url_parse($_GET[1]);
     foreach ($_GET as $key => $val) {
          $k_ = $key;
          $v_ = $val;
     }
 
 
     if (isset ($_COOKIE[$c_name])) {
          $_list = explode(';', $_COOKIE[$c_name]);
          foreach ($_list as $k => $v) {
               $item = explode('|', $_list[$k]);
               $item = array (
                    $item[0] => $item[1]
               );
          }
 
          if (array_key_exists($k_, $item)) {
               if ($v_ <> $item[$k_])
                    foreach ($item as $k => $v)
                         $item = array (
                              $k => $v_
                         );
          } else {
               $item = array_merge($item, array (
                    $k_ => $v_
               ));
          }
 
          end($item);
          $last = key($item);
          $arr = '';
          foreach ($item as $k => $v)
               if ($k == $last)
                    $arr .= $k . '|' . $v;
               else
                    $arr .= $k . '|' . $v . ';';
          if (!setcookie($c_name, $arr, time()+3600, "/"))
               die('sendCookie err');
     } else
          if (!setcookie($c_name, $k_ . '|' . $v_, time()+3600, "/"))
               die(_MSG . 'sendCookie err2');
}
 
print_r($_COOKIE);
?>

Open in new window

0
 

Author Comment

by:AndyPSV
ID: 24402451
the problem is that it theoretically works, but when I click anywhere else on the site it disappears
when I THEN click cart/?product_2=20
$_COOKIE['cart'] = 'product_1|10;product_2;20';
 
BUT when I CLICK SOMEWHERE ELSE IT DISAPPEARS
$_COOKIE['cart'] = 'product_2;20';

Open in new window

0
 
LVL 34

Expert Comment

by:Beverley Portlock
ID: 24402455
Have you tried my modified code?
0
 

Author Comment

by:AndyPSV
ID: 24403639
YES.
the same problem
0
 

Author Comment

by:AndyPSV
ID: 24403783
I've modified whole method. Now it works brilliant.
if(!empty($_GET[1])) {
			loadLib('url-val');	$get = url_parse($_GET[1]); foreach($get as $key => $val) { $name = $key; $v_ = $val; }
			
			if(isset($_COOKIE[$name])) {
				if($_COOKIE[$name] <> $_) {
					$_COOKIE[$name] = $v_;
				} else $_COOKIE[$name] = array_merge($_COOKIE[$name], array($name, $v_));
				
				foreach($_COOKIE as $k => $v) if(!setcookie($k, $v, COOKIE_EXP_TIME, '/')) die(_MSG.'sendCookie err');
			} else if(!setcookie($name, $v_, COOKIE_EXP_TIME, '/')) die(_MSG.'sendCookie err2');
		}
		
		print_r($_COOKIE);

Open in new window

0
 
LVL 34

Expert Comment

by:Beverley Portlock
ID: 24403786
That code worked OK here, so let us see if we can eliminate the code itself as the source of the problem. I'm thinking of an environment difference that might trash cookies. What I would like to try to use the code from here with a set of URLs that do not work for you and to see if it works OK for me. If the same code that fails for you works for me then it is a setting local to your PC, if not then the PHP code is geniunely the problem.

I appreciate that you may not want to put a link up to the code so click on my username and go to my profile. I have put up an email address there that you can send me a URL to  for testing. I will leave the email address available for a few hours.
0
 

Author Closing Comment

by:AndyPSV
ID: 31582195
THANKS FOR READING ANYWAY

if(!empty($_GET[1])) {
                  loadLib('url-val');      $get = url_parse($_GET[1]); foreach($get as $key => $val) { $name = $key; $v_ = $val; }
                  
                  if(isset($_COOKIE[$name])) {
                        if($_COOKIE[$name] <> $_) {
                              $_COOKIE[$name] = $v_;
                        } else $_COOKIE[$name] = array_merge($_COOKIE[$name], array($name, $v_));
                        
                        foreach($_COOKIE as $k => $v) if(!setcookie($k, $v, COOKIE_EXP_TIME, '/')) die(_MSG.'sendCookie err');
                  } else if(!setcookie($name, $v_, COOKIE_EXP_TIME, '/')) die(_MSG.'sendCookie err2');
            }
            
            print_r($_COOKIE);
0
 
LVL 34

Expert Comment

by:Beverley Portlock
ID: 24403873
I see we cross posted.  :-D  Still, as long as you are sorted.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question