[UPHPU] testing for browser without cookies

Alvaro Carrasco alvaro at epliant.com
Wed Apr 23 23:54:30 MDT 2008


Wade Preston Shearer wrote:
> I just discovered that some code that I recently implemented is 
> preventing bots (the good kind) from crawling our site. The reason for 
> this is that my new code relies on cookies and bots do not support 
> cookies. I have a language switcher that switches languages on a site. 
> Since cookies to not take effect until the next page load, I 
> immediately redirect after setting the cookie so that the user sees 
> the effects of switching languages. To avoid setting the cookie on 
> every page load, I check to see if the cookie exists before setting 
> it. The problem is that browsers (and bots) that do not support 
> cookies (or have them disabled) will never meet the criteria of the 
> cookie being set and will be stuck in an infinite redirect loop. I 
> have spent hours trying to come up with solutions to this problem and 
> am still at a loss. Does anyone have any ideas how I can make this work?
>
> This is the code that set's the cookie and does the redirect:
>
> http://rafb.net/p/wsNBaW27.html
> ...
try something like (I haven't tested it):

<?php
// first time
if (!isset($_GET['cookie_set'])) {
    setcookie('mycookie', 'somevalue');  
    header('Location: ?cookie_set=1');
    exit;
// second time
} else {
    if (isset($_COOKIE['mycookie'])) {
       // cookies work, so redirect to the correct page
    } else {
       // cookies not supported, take appropriate action
    }
}

Alvaro


More information about the UPHPU mailing list