RE: [PHP] testing for cookies on the server side (again, still ca n't get my head around it)
| From: | Johnson, Kirk | Date: | Tue, 05 Feb 2002 15:47:12 +0000 |
| Subject: | RE: [PHP] testing for cookies on the server side (again, still ca n't get my head around it) | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-83596@lists.php.net to get a copy of this message | ||
> So what's the flow of code to test for cookies on the server
> side? I'm
> pretty sure that the only way is to set a cookie, then test for it.
That is pretty much it. On the first page request to your application, if
PHP encounters a session_start() in your code, it will return a cookie named
PHPSESSID in the response headers. So you need to look for that cookie in
the request for the *second* page. If cookies are enabled in the browser,
the browser will return the cookie in the next request. Here's some code to
put at the top of the 2nd page:
<?php
function check_cookies() {
global $HTTP_COOKIE_VARS;
if(!$HTTP_COOKIE_VARS["PHPSESSID"]) {
header("Location: ./cookie_error.php");
exit();
}
}
?>
I usually make my index.php page just set up the session, etc., then do an
automatic redirect to my second page, which is the first visible page. I put
the cookie check on this second page.
Kirk