Re: Sessions and Cookies
| From: | 1LT John W. Holmes | Date: | Wed, 05 Feb 2003 11:05:24 +0000 |
| Subject: | Re: Sessions and Cookies | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-134391@lists.php.net to get a copy of this message | ||
> 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
session_start() must be before <html>. Solve those problems first because
sessions are going to be a better solution than a cookie.
> My Questions:
> If I create a cookie with set_cookie how do I read it/check it?
Whatever you name the cookie, say 'MyCookie', on the following pages from
where you set it, you'll have a $_COOKIE['MyCookie'] variable with it's
value. Use that variable just like you would any other.
> How do I use sessions if they can't be sent in the code?
They must be started before output. You can use the values within your code
and set/change values within your 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?
No problem, see above.
---John Holmes...