RE: [PHP] testing for cookies on the server side (again, still can't get my head around it)

2002-02-05 Thread Johnson, Kirk

> 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:



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

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




[PHP] testing for cookies on the server side (again, still can't get my head around it)

2002-02-04 Thread Justin French

Hi all,

Forgive me for re-posting this topic, but I still can't get my head
around the right way to work with sessions/cookies, whilst providing
some sort of server side testing for people without cookies.  I do not
want to do it client side (javascript etc).

I've got a block of code that I can include at the top of any page,
which protects it by:

1   looking for a session
2a  if one doesn't exist, provides a login form
2b  if one does exist, displays the page


Easy enough, untill someone comes in with cookie turned off, and they
get a login page each and every time they get a protected page.  Bad.


If I want to provide access to all users, then I need to pass the
session around in the URL, but if I'm reasonably happy to exclude non
cookie browsers, I'd like to provide an explanation page that tells them
why they can't access the site, etc etc.


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.


+ look for the session

   + if session does exist, show the page

   + if session doesn't exist, look for a cookie called 'cookieAvailable'

  + if 'cookieAvailable' doesn't exist, set it, and refresh the
script with ?cookieTest=set (to know i've already set it)

 + if 'cookieAvailable' still doesn't exist, show a "sorry, no
cookies" page

  + if 'cookieAvailable' does exist, show a login form, let them
login and start a session


I guess the expiry date of the cookieAvailable should be only a day
(since it's *possible* that they could turn cookies off), and same with
the session expiry.


Anything I'm missing here?


Thanks in advance,
Justin French

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