473,405 Members | 2,444 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,405 software developers and data experts.

Cookie problems (simple code included)

Hi all,

I'm trying to develop a website that selects from 3 stylesheets
depending on which style they have chosen. Their preference is stored
in a cookie and this is where the problems begin. I have boiled my code
down to the bare essentials in the hope that someone can help me with
this problem. I would be grateful if you could explain what I have done
is wrong too, because I read that cookie settings should be done before
the <htmltag, which is what I have done.

=== CODE (this can be copied and pasted into a php file) ===

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<?php
$cstyle = (int)$_COOKIE["cstyle"];
print("***$cstyle***");

if ($cstyle < 1 || $cstyle 3)
{
$cstyle = 1;
}

@extract($_POST);
$cstylenew = (int)$_GET['cstyle'];

if ($cstylenew >= 1 && $cstylenew <= 3)
{
setcookie("cstyle3", "$cstylenew", time()+5000000);
}
?>

<html>

<head>
<link href="style<?php print($cstyle);?>.css" rel="stylesheet"
type="text/css">
</head>

<body>
<br>
<a href="<?php print("$PHP_SELF"); ?>?cstyle=1">style 1</a><br>
<a href="<?php print("$PHP_SELF"); ?>?cstyle=2">style 2</a><br>
<a href="<?php print("$PHP_SELF"); ?>?cstyle=3">style 3</a><br>
</body>
</html>

Best regards,
ii2o

Sep 20 '06 #1
4 1293
ii2o wrote:
Hi all,

I'm trying to develop a website that selects from 3 stylesheets
depending on which style they have chosen. Their preference is stored
in a cookie and this is where the problems begin. I have boiled my
code down to the bare essentials in the hope that someone can help me
with this problem. I would be grateful if you could explain what I
have done is wrong too, because I read that cookie settings should be
done before the <htmltag, which is what I have done.

=== CODE (this can be copied and pasted into a php file) ===

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<?php
$cstyle = (int)$_COOKIE["cstyle"];
print("***$cstyle***");

if ($cstyle < 1 || $cstyle 3)
{
$cstyle = 1;
}

@extract($_POST);
$cstylenew = (int)$_GET['cstyle'];

if ($cstylenew >= 1 && $cstylenew <= 3)
{
setcookie("cstyle3", "$cstylenew", time()+5000000);
}
?>

<html>

<head>
<link href="style<?php print($cstyle);?>.css" rel="stylesheet"
type="text/css">
</head>

<body>
<br>
<a href="<?php print("$PHP_SELF"); ?>?cstyle=1">style 1</a><br>
<a href="<?php print("$PHP_SELF"); ?>?cstyle=2">style 2</a><br>
<a href="<?php print("$PHP_SELF"); ?>?cstyle=3">style 3</a><br>
</body>
</html>

Best regards,
ii2o
Cookies are set in the headers, meaning that setcookie() must be called
before *any* output is sent to the user (even before your DOCTYPE
statement). Make sure that you don't have any blank spaces before <?php
as well.

--
Kim André Akerĝ
- ki******@NOSPAMbetadome.com
(remove NOSPAM to contact me directly)
Sep 20 '06 #2
Hi there,

Thanks for the response. Unfortunately even when I move the doctype
statement to below the php code, I still get the following error
"Warning: Cannot modify header information - headers already sent by
(output started at d:\testserver\testcookies.php:3) in
d:\testserver\testcookies.php on line 14".

When you mean before any output is sent to the user, do you mean that I
can't use the $_GET statement? If so, how is possible to select a
stylesheet in the code without accessing this variable?

I'm very confused!

Thanks,
ii2o
Kim André Akerĝ wrote:
ii2o wrote:
Hi all,

I'm trying to develop a website that selects from 3 stylesheets
depending on which style they have chosen. Their preference is stored
in a cookie and this is where the problems begin. I have boiled my
code down to the bare essentials in the hope that someone can help me
with this problem. I would be grateful if you could explain what I
have done is wrong too, because I read that cookie settings should be
done before the <htmltag, which is what I have done.

=== CODE (this can be copied and pasted into a php file) ===

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<?php
$cstyle = (int)$_COOKIE["cstyle"];
print("***$cstyle***");

if ($cstyle < 1 || $cstyle 3)
{
$cstyle = 1;
}

@extract($_POST);
$cstylenew = (int)$_GET['cstyle'];

if ($cstylenew >= 1 && $cstylenew <= 3)
{
setcookie("cstyle3", "$cstylenew", time()+5000000);
}
?>

<html>

<head>
<link href="style<?php print($cstyle);?>.css" rel="stylesheet"
type="text/css">
</head>

<body>
<br>
<a href="<?php print("$PHP_SELF"); ?>?cstyle=1">style 1</a><br>
<a href="<?php print("$PHP_SELF"); ?>?cstyle=2">style 2</a><br>
<a href="<?php print("$PHP_SELF"); ?>?cstyle=3">style 3</a><br>
</body>
</html>

Best regards,
ii2o

Cookies are set in the headers, meaning that setcookie() must be called
before *any* output is sent to the user (even before your DOCTYPE
statement). Make sure that you don't have any blank spaces before <?php
as well.

--
Kim André Akerĝ
- ki******@NOSPAMbetadome.com
(remove NOSPAM to contact me directly)
Sep 20 '06 #3
ii2o wrote:
Kim André Akerĝ wrote:
ii2o wrote:
Hi all,
>
I'm trying to develop a website that selects from 3 stylesheets
depending on which style they have chosen. Their preference is
stored in a cookie and this is where the problems begin. I have
boiled my code down to the bare essentials in the hope that
someone can help me with this problem. I would be grateful if you
could explain what I have done is wrong too, because I read that
cookie settings should be done before the <htmltag, which is
what I have done.
>
=== CODE (this can be copied and pasted into a php file) ===
>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<?php
$cstyle = (int)$_COOKIE["cstyle"];
print("***$cstyle***");
>
if ($cstyle < 1 || $cstyle 3)
{
$cstyle = 1;
}
>
@extract($_POST);
$cstylenew = (int)$_GET['cstyle'];
>
if ($cstylenew >= 1 && $cstylenew <= 3)
{
setcookie("cstyle3", "$cstylenew", time()+5000000);
}
?>
>
<html>
>
<head>
<link href="style<?php print($cstyle);?>.css" rel="stylesheet"
type="text/css">
</head>
>
<body>
<br>
<a href="<?php print("$PHP_SELF"); ?>?cstyle=1">style
1</a><br <a href="<?php print("$PHP_SELF");
?>?cstyle=2">style 2</a><br <a href="<?php
print("$PHP_SELF"); ?>?cstyle=3">style 3</a><br </body>
</html>
>
Best regards,
ii2o
Cookies are set in the headers, meaning that setcookie() must be
called before any output is sent to the user (even before your
DOCTYPE statement). Make sure that you don't have any blank spaces
before <?php as well.

Hi there,

Thanks for the response. Unfortunately even when I move the doctype
statement to below the php code, I still get the following error
"Warning: Cannot modify header information - headers already sent by
(output started at d:\testserver\testcookies.php:3) in
d:\testserver\testcookies.php on line 14".

When you mean before any output is sent to the user, do you mean that
I can't use the $_GET statement? If so, how is possible to select a
stylesheet in the code without accessing this variable?

I'm very confused!
print() is considered sending output, too.

--
Kim André Akerĝ
- ki******@NOSPAMbetadome.com
(remove NOSPAM to contact me directly)
Sep 20 '06 #4
Brilliant - I see where I was going wrong now. Thanks for your help!
ii2o

Kim André Akerĝ wrote:
ii2o wrote:
Kim André Akerĝ wrote:
ii2o wrote:
>
Hi all,

I'm trying to develop a website that selects from 3 stylesheets
depending on which style they have chosen. Their preference is
stored in a cookie and this is where the problems begin. I have
boiled my code down to the bare essentials in the hope that
someone can help me with this problem. I would be grateful if you
could explain what I have done is wrong too, because I read that
cookie settings should be done before the <htmltag, which is
what I have done.

=== CODE (this can be copied and pasted into a php file) ===

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<?php
$cstyle = (int)$_COOKIE["cstyle"];
print("***$cstyle***");

if ($cstyle < 1 || $cstyle 3)
{
$cstyle = 1;
}

@extract($_POST);
$cstylenew = (int)$_GET['cstyle'];

if ($cstylenew >= 1 && $cstylenew <= 3)
{
setcookie("cstyle3", "$cstylenew", time()+5000000);
}
?>

<html>

<head>
<link href="style<?php print($cstyle);?>.css" rel="stylesheet"
type="text/css">
</head>

<body>
<br>
<a href="<?php print("$PHP_SELF"); ?>?cstyle=1">style
1</a><br <a href="<?php print("$PHP_SELF");
?>?cstyle=2">style 2</a><br <a href="<?php
print("$PHP_SELF"); ?>?cstyle=3">style 3</a><br </body>
</html>

Best regards,
ii2o
>
Cookies are set in the headers, meaning that setcookie() must be
called before any output is sent to the user (even before your
DOCTYPE statement). Make sure that you don't have any blank spaces
before <?php as well.
Hi there,

Thanks for the response. Unfortunately even when I move the doctype
statement to below the php code, I still get the following error
"Warning: Cannot modify header information - headers already sent by
(output started at d:\testserver\testcookies.php:3) in
d:\testserver\testcookies.php on line 14".

When you mean before any output is sent to the user, do you mean that
I can't use the $_GET statement? If so, how is possible to select a
stylesheet in the code without accessing this variable?

I'm very confused!

print() is considered sending output, too.

--
Kim André Akerĝ
- ki******@NOSPAMbetadome.com
(remove NOSPAM to contact me directly)
Sep 20 '06 #5

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

Similar topics

13
by: Manlio Perillo | last post by:
Hi. I'm using the Cookie module (on the client side). I have found a problem trying to parse the cookie: "Set-Cookie: value=thevalue; path=/; expires=Fri, 21-May-2004 10:40:51 GMT" The date...
1
by: HorseGeek | last post by:
I have three servers: Server1, Server2, and Server3. There is a login cookie does not expire for SOME users on Server2. Ironically, the login cookie expires correctly for ALL users on the...
7
by: What-a-Tool | last post by:
How does the expire date work setting it server side with asp. I know with javascript setting it client side it will be set to the clients local time, and therefore expire when the clients local...
9
by: robbie.carlton | last post by:
Hello! I've programmed in c a bit, but nothing very complicated. I've just come back to it after a long sojourn in the lands of functional programming and am completely stumped on a very simple...
6
by: Jason Collins | last post by:
There seems to be an inconsistency (bug?) in the way the Set-Cookie header is handled by the WebHeaderCollection. That is, the values of Set-Cookie, when an Expires is specified, contain the ","...
2
by: Alan Silver | last post by:
Hello, I have discovered that if I try and add a cookie when one by that already exists, nothing happens. No error, but the cookie is not set to the new value. For example (this is running in...
1
by: Stef | last post by:
Hi people, I have a problem with cookies set via javascript. What I try to achieve is, when a user comes on the intranet, he can click on a link ( a simple href) that will set the content to...
3
by: Groove | last post by:
Hey guys - hoping you can help me out here. I'm using asp.net 2 (VB) and trying to set and retrieve values from a cookie. I've done this a few times before w/o a problem but I seem to be...
16
by: Stevo | last post by:
I'm guessing this is a laughably obvious answer to many here, but it's not to me (and I don't have a server or any knowledge of PHP to be able to try it). It's not strictly a PHP question, but...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.