Solved

Disappearing cookie value

Posted on 2009-05-16
13
188 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
 

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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
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.
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 …

758 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now