Solved

Disappearing cookie value

Posted on 2009-05-16
13
194 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

These days socially coordinated efforts have turned into a critical requirement for enterprises.
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

734 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