473,396 Members | 1,827 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Setting cookies [URGENT]

Can anyone tell me why this code does not do what I want it to:

<?php
if (!isset($_COOKIE['basket'])) {
setcookie("basket", $item_code, time()+60*60*24*30, "coursework", '',
0) ;
}
else {
setcookie( "basket", $_COOKIE['basket']-$item_code );
}
?>

The page GETs the variable $item_code from a URL and stores it in a cookie
okay. When I revisit the page with a different $item_code I want the code to
check if the cookie is set already and, if so, *append* the new $item_code
to the existing cookie. Problem the old cookie is overwritten by the new one
rather than appended.

I've been trying all day to get this to work and whereas some other code
I've looked at does append, mine doesn't, and I can't see where I am going
wrong. Here is the other code that does work but it's no good as it is
different t what I need:
<?php
error_reporting(1); // suppress warning messages
if ($clear)
setcookie("ckTrolleyContents","", time()-3600);
elseif ($choice)
setcookie("ckTrolleyContents","$ckTrolleyContents< li>$choice</li>");
?>

<html><head><title>PHP cookie example</title>
</head><body>
<h2>Shopping trolley (php cookie version) </h2>
<form method="get" action="CookieTrolley.php">
<p>Choose an item: </p>
<p>
<input type="radio" name="choice" value="camera"/> camera<br />
<input type="radio" name="choice" value="snorkel"/> snorkel<br />
<input type="radio" name="choice" value="octopus"/> octopus<br />
<input type="radio" name="choice" value="cuddly toy"/> cuddly toy<br /><br
/>
<input type="submit" name="submit" value="Add to the trolley"/>
<input type="submit" name="clear" value="Empty the trolley"/>
</p>
</form>

<?php
if ((!$choice && !$ckTrolleyContents) || $clear)
echo "<p>Trolley currently empty</p>";
elseif (!$choice) {
echo "<p>Trolley contains:</p>";
echo "<ul>$ckTrolleyContents</ul>";
} else {
// NB the cookie will not contain what has been added to it
// during the current form execution
echo "<p>Trolley contains:</p>";
echo "<ul>$ckTrolleyContents<li>$choice</li></ul>";
}
?>

</body></html>
Jul 17 '05 #1
7 2083
There is a line that should read:

setcookie( "basket", "$_COOKIE['basket']-$item_code" );

Thanks
"Fnark" <fa**@reallyisfake.com> wrote in message
news:Sb*******************@news-binary.blueyonder.co.uk...
Can anyone tell me why this code does not do what I want it to:

<?php
if (!isset($_COOKIE['basket'])) {
setcookie("basket", $item_code, time()+60*60*24*30, "coursework", '', 0) ;
}
else {
setcookie( "basket", $_COOKIE['basket']-$item_code );
}
?>

The page GETs the variable $item_code from a URL and stores it in a cookie
okay. When I revisit the page with a different $item_code I want the code to check if the cookie is set already and, if so, *append* the new $item_code
to the existing cookie. Problem the old cookie is overwritten by the new one rather than appended.

I've been trying all day to get this to work and whereas some other code
I've looked at does append, mine doesn't, and I can't see where I am going
wrong. Here is the other code that does work but it's no good as it is
different t what I need:
<?php
error_reporting(1); // suppress warning messages
if ($clear)
setcookie("ckTrolleyContents","", time()-3600);
elseif ($choice)
setcookie("ckTrolleyContents","$ckTrolleyContents< li>$choice</li>");
?>

<html><head><title>PHP cookie example</title>
</head><body>
<h2>Shopping trolley (php cookie version) </h2>
<form method="get" action="CookieTrolley.php">
<p>Choose an item: </p>
<p>
<input type="radio" name="choice" value="camera"/> camera<br />
<input type="radio" name="choice" value="snorkel"/> snorkel<br />
<input type="radio" name="choice" value="octopus"/> octopus<br />
<input type="radio" name="choice" value="cuddly toy"/> cuddly toy<br /><br
/>
<input type="submit" name="submit" value="Add to the trolley"/>
<input type="submit" name="clear" value="Empty the trolley"/>
</p>
</form>

<?php
if ((!$choice && !$ckTrolleyContents) || $clear)
echo "<p>Trolley currently empty</p>";
elseif (!$choice) {
echo "<p>Trolley contains:</p>";
echo "<ul>$ckTrolleyContents</ul>";
} else {
// NB the cookie will not contain what has been added to it
// during the current form execution
echo "<p>Trolley contains:</p>";
echo "<ul>$ckTrolleyContents<li>$choice</li></ul>";
}
?>

</body></html>

Jul 17 '05 #2
Fnark wrote:
There is a line that should read:

setcookie( "basket", "$_COOKIE['basket']-$item_code" );


curly braces inside double quotes
setcookie( "basket", "{$_COOKIE['basket']}-$item_code" );

but I prefer always using single quotes and concatenation :-)
setcookie('basket', $_COOKIE['basket'] . '-' . $item_code);
refer to the manual page on strings for (sic) "Complex syntax"
http://www.php.net/manual/en/languag...arsing.complex
--
--= my mail box only accepts =--
--= Content-Type: text/plain =--
--= Size below 10001 bytes =--
Jul 17 '05 #3
Thanks for your reply.

I tried the second method previously ( and again for good measure) but it
still overwrites my cookie. If a cookie is set where the $item_code is '1'
the cookie value is '1'. But the next time I enter the page where the
$item_code is '2' the cookie is '2' not '1-2' which is what I want as I need
to store multiple $item_code(s) in my cookie.

It's driving me nuts as the other code I posted appended to the cookie but
mine doesn't even if I try the same way. Incidentally the writer of the
other code (in a book) said that appending takes place automatically - only
it won't in my case. Could it be that I use GET where he uses POST?.

Mark
"Pedro Graca" <he****@hotpop.com> wrote in message
news:bt************@ID-203069.news.uni-berlin.de...
Fnark wrote:
There is a line that should read:

setcookie( "basket", "$_COOKIE['basket']-$item_code" );
curly braces inside double quotes
setcookie( "basket", "{$_COOKIE['basket']}-$item_code" );

but I prefer always using single quotes and concatenation :-)
setcookie('basket', $_COOKIE['basket'] . '-' . $item_code);
refer to the manual page on strings for (sic) "Complex syntax"

http://www.php.net/manual/en/languag...arsing.complex --
--= my mail box only accepts =--
--= Content-Type: text/plain =--
--= Size below 10001 bytes =--

Jul 17 '05 #4
It should be:

setcookie( "basket", "$_COOKIE[basket]-$item_code" );
OR
setcookie( "basket", $_COOKIE['basket'].'-'.$item_code);

More efficient and error free.

--
Rahul
"Fnark" <fa**@reallyisfake.com> wrote in message news:<0z******************@news-binary.blueyonder.co.uk>...
There is a line that should read:

setcookie( "basket", "$_COOKIE['basket']-$item_code" );

Thanks

Jul 17 '05 #5
Hi Mark,

I tried this code at my system and it is working fine:

<SNIP>

$item_code = $_GET['item_code'];
if (!isset($_COOKIE['basket']))
setcookie('basket', $item_code, time()+60*60*24*30, "/", '',0) ;
else
setcookie('basket', $_COOKIE['basket'].'-'.$item_code ) ;

print_r($_COOKIE);

</SNIP>

Please not a cookie set in a script will be available in $_COOKIE from
next execution only.

I think problem must be in some other code.

Enabling your Notice Error may help in finding the problem code.

--
Rahul
"Fnark" <fa**@reallyisfake.com> wrote in message news:<IF***************@news-binary.blueyonder.co.uk>...
Thanks for your reply.

I tried the second method previously ( and again for good measure) but it
still overwrites my cookie. If a cookie is set where the $item_code is '1'
the cookie value is '1'. But the next time I enter the page where the
$item_code is '2' the cookie is '2' not '1-2' which is what I want as I need
to store multiple $item_code(s) in my cookie.

It's driving me nuts as the other code I posted appended to the cookie but
mine doesn't even if I try the same way. Incidentally the writer of the
other code (in a book) said that appending takes place automatically - only
it won't in my case. Could it be that I use GET where he uses POST?.

Mark

Jul 17 '05 #6
Thanks Rahul,

The code works for me to an extent. Did your code append to the cookie on
subsequent visits? My code writes a cookie with an initial value but then
overwrites it (not appends) on subsequent visits to the page.

I have a feeling the code is not getting to the else statement and always
executing the if statement yet I can't see why this should be so. When I

print_r($_COOKIE);

I only get the output Array() do you know how to get the contents of that
array to print?
Incidentally, if you like you may test out my code page here

http://stuweb.cms.gre.ac.uk/~jm925/w...ork/basket.php

(Yes it is my coursework but our tutors have no problems with us asking for
help from our peers debugging code so long as we have made an effort to
write it ourselves as this is common practice and expected in industry -
plus I've been stuck on this for days and can't find a solution anywhere!!
:0) )

Thanks
Mark
"Rahul Anand" <ra************@rediffmail.com> wrote in message
news:62**************************@posting.google.c om...
Hi Mark,

I tried this code at my system and it is working fine:

<SNIP>

$item_code = $_GET['item_code'];
if (!isset($_COOKIE['basket']))
setcookie('basket', $item_code, time()+60*60*24*30, "/", '',0) ;
else
setcookie('basket', $_COOKIE['basket'].'-'.$item_code ) ;

print_r($_COOKIE);

</SNIP>

Please not a cookie set in a script will be available in $_COOKIE from
next execution only.

I think problem must be in some other code.

Enabling your Notice Error may help in finding the problem code.

--
Rahul
"Fnark" <fa**@reallyisfake.com> wrote in message

news:<IF***************@news-binary.blueyonder.co.uk>...
Thanks for your reply.

I tried the second method previously ( and again for good measure) but it still overwrites my cookie. If a cookie is set where the $item_code is '1' the cookie value is '1'. But the next time I enter the page where the
$item_code is '2' the cookie is '2' not '1-2' which is what I want as I need to store multiple $item_code(s) in my cookie.

It's driving me nuts as the other code I posted appended to the cookie but mine doesn't even if I try the same way. Incidentally the writer of the
other code (in a book) said that appending takes place automatically - only it won't in my case. Could it be that I use GET where he uses POST?.

Mark

Jul 17 '05 #7
Problem was I did not put the correct full path in the cookie.

D'OH!! *slaps forehead*
"Fnark" <fa**@reallyisfake.com> wrote in message
news:Nm*****************@news-binary.blueyonder.co.uk...
Thanks Rahul,

The code works for me to an extent. Did your code append to the cookie on
subsequent visits? My code writes a cookie with an initial value but then
overwrites it (not appends) on subsequent visits to the page.

I have a feeling the code is not getting to the else statement and always
executing the if statement yet I can't see why this should be so. When I

print_r($_COOKIE);

I only get the output Array() do you know how to get the contents of that
array to print?
Incidentally, if you like you may test out my code page here

http://stuweb.cms.gre.ac.uk/~jm925/w...ork/basket.php

(Yes it is my coursework but our tutors have no problems with us asking for help from our peers debugging code so long as we have made an effort to
write it ourselves as this is common practice and expected in industry -
plus I've been stuck on this for days and can't find a solution anywhere!!
:0) )

Thanks
Mark
"Rahul Anand" <ra************@rediffmail.com> wrote in message
news:62**************************@posting.google.c om...
Hi Mark,

I tried this code at my system and it is working fine:

<SNIP>

$item_code = $_GET['item_code'];
if (!isset($_COOKIE['basket']))
setcookie('basket', $item_code, time()+60*60*24*30, "/", '',0) ;
else
setcookie('basket', $_COOKIE['basket'].'-'.$item_code ) ;

print_r($_COOKIE);

</SNIP>

Please not a cookie set in a script will be available in $_COOKIE from
next execution only.

I think problem must be in some other code.

Enabling your Notice Error may help in finding the problem code.

--
Rahul
"Fnark" <fa**@reallyisfake.com> wrote in message news:<IF***************@news-binary.blueyonder.co.uk>...
Thanks for your reply.

I tried the second method previously ( and again for good measure) but it still overwrites my cookie. If a cookie is set where the $item_code is '1' the cookie value is '1'. But the next time I enter the page where the
$item_code is '2' the cookie is '2' not '1-2' which is what I want as I need
to store multiple $item_code(s) in my cookie.

It's driving me nuts as the other code I posted appended to the cookie but mine doesn't even if I try the same way. Incidentally the writer of
the other code (in a book) said that appending takes place automatically -

only it won't in my case. Could it be that I use GET where he uses POST?.

Mark


Jul 17 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Sims | last post by:
Hi, Is it wrong to forcefully set a value HTTP_GET_VARS and HTTP_POST_VARS $HTTP_GET_VARS = 'bar'? If yes, why? and how can i pass value between one page and the other without using the url?...
2
by: junlia | last post by:
Hi All, I am working on a project that acts as a bridge. It does some checking with post xml data, and then redirects the request to an appropriate page. However, we find that depends on the...
4
by: Vlad Dogaru | last post by:
Hello, I am trying to use cookies and Python to create a simple login example. But I am very disoriented at the existence of two cookie libraries, namely Cookie and cookielib. I have seen...
1
by: laredotornado | last post by:
Hi, I'm using PHP 4.4.4 on Apache 2 on Fedora Core 5. PHP was installed using Apache's apxs and the php library was installed to /usr/local/php. However, when I set my "error_reporting"...
1
by: Joe | last post by:
In ASP.NET 1.1 I could detected expired form authentication tickets (which closely coincide with my expired session) by checking for the Authentication Cookie when the login screen loads. If the...
6
by: John H Clark | last post by:
I am designing a site that requires AnonymousID. I set my web.config to allow this using <anonymousIdentification enable="true".../as recommended in the documentation. To verify the settings I...
2
by: David Thielen | last post by:
Hi; 1) What is the easiest way to show in an aspx page if they have scripts enabled? 2) What is the easiest way to show in an aspx page if they have cookies enabled? This is for a test...
1
by: maheshv | last post by:
Hi all, Dear friends, i am facing a problem when using cookies i need help of yours... Lets come to the issue, i am using cookies all over the site, it is working all fine, the only problem is...
4
by: Jeff | last post by:
I have a vb.net application (2005) requiring session variables and want to test to make certain that the user's cookies are enabled. I can set a test session variable on one page and attempt to...
5
by: sophie_newbie | last post by:
Does anyone know how to do this? I can't seem to make it work. I'm using: c = Cookie.SimpleCookie() c = "unamepwordwhatever" c.expires = time.time() + 300 print c
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.