473,396 Members | 2,036 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.

Cookies problems - why do they need special treatment?

Hii everyone,
I've been programming in php for a long time, and the cookies have
always been a trouble for me.
When I have headers before the cookies set, I get a problem.
Even if I leave space on the first row before I set the cookie, and
there is nothing else,
It says I have headers before.
Is it like that for everyone, or is it just my host?
If so, is there anything that I can do to set cookies anywhere I want?
Please help,
Ofir.

Aug 13 '07 #1
2 2089
Rik
On Mon, 13 Aug 2007 11:35:59 +0200, ofiras <of****@gmail.comwrote:
Hii everyone,
I've been programming in php for a long time, and the cookies have
always been a trouble for me.
When I have headers before the cookies set, I get a problem.
Even if I leave space on the first row before I set the cookie, and
there is nothing else,
It says I have headers before.
Is it like that for everyone, or is it just my host?
If so, is there anything that I can do to set cookies anywhere I want?

Read up on the HTTP protocol. A response of a server is divided into
headers & body (not to be confused with an HTML head & body). Something
like this:

(header)
200 OK
Content-length: xxx
(...more headers)

(empty line, followed by body)
<!DOCTYPE html>
<html>
....etc

As your script produces (non-header) output, the empty line, which
seperates the headers from the body of the response, is sent to the
browser, followed by whatever output you specified further. A cookie has
to be set in the headers (with a Set-Cookie statement), and there's no way
of going back as soon as this empty line has been sent.

If you have to set a cookie somewhere, consider these options:

1. Do almost all processing _before_ generating output. As soon as you get
out of an HTML mindset (and those atrocious files mixing HTML & PHP like
there's no tomorrow) this is actually quite easy.

2. Capture output in a variable, and only echo / print it as soon as you
know no further processing/header() or setcookie() calls are needed.

3. Turn on output buffering (ob_start()) at the start of the script.
(Which will effectively do something like (2) only with somewhat less
control).

The last one is a bit of cheat, and does not promote good code writing. It
is however most simple one. Drawback is that on a slow loading page, it
will take a while to get even a bit of content, while with output
buffering of it can load 'as you go' (depending on how your browser
handles it offcourse). If possible, go for (1).
--
Rik Wasmus
Aug 13 '07 #2
On Aug 13, 12:49 pm, Rik <luiheidsgoe...@hotmail.comwrote:
On Mon, 13 Aug 2007 11:35:59 +0200, ofiras <ofi...@gmail.comwrote:
Hii everyone,
I've been programming in php for a long time, and the cookies have
always been a trouble for me.
When I have headers before the cookies set, I get a problem.
Even if I leave space on the first row before I set the cookie, and
there is nothing else,
It says I have headers before.
Is it like that for everyone, or is it just my host?
If so, is there anything that I can do to set cookies anywhere I want?

Read up on the HTTP protocol. A response of a server is divided into
headers & body (not to be confused with an HTML head & body). Something
like this:

(header)
200 OK
Content-length: xxx
(...more headers)

(empty line, followed by body)
<!DOCTYPE html>
<html>
...etc

As your script produces (non-header) output, the empty line, which
seperates the headers from the body of the response, is sent to the
browser, followed by whatever output you specified further. A cookie has
to be set in the headers (with a Set-Cookie statement), and there's no way
of going back as soon as this empty line has been sent.

If you have to set a cookie somewhere, consider these options:

1. Do almost all processing _before_ generating output. As soon as you get
out of an HTML mindset (and those atrocious files mixing HTML & PHP like
there's no tomorrow) this is actually quite easy.

2. Capture output in a variable, and only echo / print it as soon as you
know no further processing/header() or setcookie() calls are needed.

3. Turn on output buffering (ob_start()) at the start of the script.
(Which will effectively do something like (2) only with somewhat less
control).

The last one is a bit of cheat, and does not promote good code writing. It
is however most simple one. Drawback is that on a slow loading page, it
will take a while to get even a bit of content, while with output
buffering of it can load 'as you go' (depending on how your browser
handles it offcourse). If possible, go for (1).
--
Rik Wasmus
Thanks a lot, ill think ill go for the second one... ill see what best
for me.
Thanks,
Ofir.

Aug 13 '07 #3

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

Similar topics

4
by: Brian Burgess | last post by:
Hi all, Anyone know of any special issues with storing cookies with ASP? I'm trying this with two browsers: One is IE 6.0 with cookies set to 'prompt'. This has been working properly as any...
0
by: yukon | last post by:
Hi there the next is my platform IIS5 windows 2000 asp vbscript My working area has the next virtual directoy: testAc (corrective ambient) testAe (evolutionary ambient)
0
by: Dan Meehan | last post by:
I created a music website that allows people to sign up and post messages on some message boards and update their profiles and such. The login script uses cookies, so when they Log Into the site it...
9
by: | last post by:
Is it possible for a user to enable permanent cookies but disable session cookies.....this seems like a contradition yet this is what I appear to be reading in online articles?
6
by: Mark | last post by:
Hi... I've come across some weird bug with Response.Cookies. Or maybe it will be called "by design" but for the life of me I can't figure out what purpose it would serve. If you're setting a...
4
by: Robin Briggs | last post by:
Hi. I have an ASP (3.0, not .NET) app that writes cookies with hyphens and spaces in the names and values. If I had the control over the apps that will be looking for these cookies, I would...
5
by: Martin Heuckeroth | last post by:
Hi We are working on a webservice application and are having some problems with the cookies and/or sessions. We have them working on our intranet but then its not working on the internet. We...
2
by: john.lehmann | last post by:
Attacked is a piece of code which first hits the login page successfully and receives back login cookies. But then when I attempt to hit a page which is restricted to logged in users only, I fail....
84
by: jacob navia | last post by:
As many people know, I think that garbage collection is a good solution for many memory allocation problems. I am aware however, that nothing is "the silver bullet", not even the GC. A recent...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
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...

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.