Well, that is natural - cookies can only be set before any output is
produced by the script. And since sessions in this case use cookies, it
follows that sessions can also be started before any output is produced.
Why is that? Because cookies are transmitted by means of http headers, and
http headers precede any content.

The approach I find works extremely well is using some kind of design of the
scripts, such that the script does some "analysis" of the situation at the
very beginning, processes, saves/loads any data and only then proceeds to
display the results.
Otherwise, you might want to use buffering. See manual for ob_start():
----
This function will turn output buffering on. While output buffering is
active no output is sent from the script (other than headers), instead the
output is stored in an internal buffer.
---
So you can use it at the beginning of your scripts.
Also, see the config file, php.ini, there is a directive that allows you to
enable it globally without modifying scripts:
---
; Output buffering allows you to send header lines (including cookies) even
; after you send body content, at the price of slowing PHP's output layer a
; bit.  You can enable output buffering during runtime by calling the output
; buffering functions.  You can also enable output buffering for all files
by
; setting this directive to On.  If you wish to limit the size of the buffer
; to a certain size - you can use a maximum number of bytes instead of 'On',
as
; a value for this directive (e.g., output_buffering=4096).
output_buffering = On
---

Yours trully,
--Dmitri


----- Original Message -----
From: "acleave" <>
To: "php-general" <[EMAIL PROTECTED]>
Sent: Wednesday, February 05, 2003 12:01 AM
Subject: [PHP] Sessions and Cookies


> I'm trying to use cookies in PHP4 (.whatever the latest release is).  I
want
> to use them for validation (ensuring a user has logged in) but all I can
find
> is setcookie, which seems only to create the cookie.  In trying to use PHP
> sessions, I end up with odd errors.
>
> When I try to use sessions I get the error message the header has already
been
> sent.  I've pasted them below (I was getting different errors before I
moved
> the code before the < html > tag).
>
> Warning: Cannot send session cookie - headers already sent by (output
started
> at /home/allan/public_html/sestest2.php:10) in
> /home/allan/public_html/verifysession.php on line 6
>
> Warning: Cannot send session cache limiter - headers already sent (output
> started at /home/allan/public_html/sestest2.php:10) in
> /home/allan/public_html/verifysession.php on line 6
>
>
> My Questions:
> If I create a cookie with set_cookie how do I read it/check it?
>
> How do I use sessions if they can't be sent in the code?
>
> What use are sessions if I can only mess with them in one place?  What if
I
> need to do some processing first to decide what to do with them?
>
> Any insight is welcome.
>
> Allan Cleaveland
> Webmaster and Computer Technician
> Math Department
> Univeristy of Arkansas
>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to