• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 209
  • Last Modified:

Disappearing cookie value

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
AndyPSV
Asked:
AndyPSV
  • 7
  • 6
1 Solution
 
Beverley PortlockCommented:
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
 
AndyPSVAuthor Commented:
that's not the solution to the question
0
 
Beverley PortlockCommented:
"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
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

 
AndyPSVAuthor Commented:
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
 
Beverley PortlockCommented:
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
 
Beverley PortlockCommented:
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
 
AndyPSVAuthor Commented:
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
 
Beverley PortlockCommented:
Have you tried my modified code?
0
 
AndyPSVAuthor Commented:
YES.
the same problem
0
 
AndyPSVAuthor Commented:
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
 
Beverley PortlockCommented:
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
 
AndyPSVAuthor Commented:
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
 
Beverley PortlockCommented:
I see we cross posted.  :-D  Still, as long as you are sorted.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

  • 7
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now