Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Disappearing cookie value

Posted on 2009-05-16
13
Medium Priority
?
200 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 2000 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

885 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