Cookies problems - why do they need special treatment?

This is a discussion on Cookies problems - why do they need special treatment? within the PHP Language forums, part of the PHP Programming Forums category; Hii everyone, I've been programming in php for a long time, and the cookies have always been a trouble ...


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 08-13-2007
ofiras
 
Posts: n/a
Default 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.

Reply With Quote
  #2 (permalink)  
Old 08-13-2007
Rik
 
Posts: n/a
Default Re: Cookies problems - why do they need special treatment?

On Mon, 13 Aug 2007 11:35:59 +0200, ofiras <ofiasu@gmail.com> wrote:

> 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
Reply With Quote
  #3 (permalink)  
Old 08-13-2007
ofiras
 
Posts: n/a
Default Re: Cookies problems - why do they need special treatment?

On Aug 13, 12:49 pm, Rik <luiheidsgoe...@hotmail.com> wrote:
> On Mon, 13 Aug 2007 11:35:59 +0200, ofiras <ofi...@gmail.com> wrote:
> > 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.

Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 07:57 PM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0