4

I'm developing a website on my localhost which uses cookies. I have a function that generates a random, 25 character string which will be stored in a database and set as a referential cookie on the user's browser.

I've searched the internet and this site, but wasn't able to find a solution for my problem.

Below an overview of the related code


function generateRandomString($length){
    $string = "";
    $possible = "012346789abcdefghijklmnopqrstuvwABCDEFGHIJKLMNOPQRSTUVW";
    $maxlength = strlen($possible);
    if($length > $maxlength){
        $length = $maxlength;
    }
    $i = 0; 
    while($i < $length){ 
        $char = substr($possible, mt_rand(0, $maxlength-1), 1);
        if(!strstr($string, $char)){ 
            $string .= $char;
            $i++;
        }
    }
    return $string;
}

$uCookie = generateRandomString(25);

setcookie('uHash', $uCookie, time()+60*60*24*30);

$stmt = $dbh->prepare('
    UPDATE User 
    SET u_UserCookie = :cookie 
    WHERE u_UserId = :id
'); 

$stmt->execute(array(
    ':cookie' => $uCookie,
    ':id' => $user_id
));

Now when i try echo($_COOKIE['uHash']); i get an empty string.

The strange part is that when i check my chrome preferences, the cookie does exists

Name:    uHash
Content:    134uHnEPrCmBNGqeAjhRSUiJL
Domain: localhost
Path:   /~path/to/login
Send for:   Any kind of connection
Accessible to script:   Yes
Created:    Wednesday, April 17, 2013 4:21:27 PM
Expires:    Friday, May 17, 2013 4:21:27 PM

The string '134uHnEPrCmBNGqeAjhRSUiJL' can also be found in the database, so that works

Am i missing some basic info about cookies (on a localhost)?

SOLVED

The problem is, according to php.net

'that domain names must contain at least two dots (.), hence 'localhost' is invalid and the browser will refuse to set the cookie'

So i solved it by doing this


$domain = ($_SERVER['HTTP_HOST'] != 'localhost') ? $_SERVER['HTTP_HOST'] : false;
setcookie('uHash', $uCookie, time()+60*60*24*30, '/', $domain, false);
Maurice
  • 1,082
  • 1
  • 20
  • 43
  • 3
    Are you trying to read `$_COOKIE` in the same script that set it? – Barmar Apr 18 '13 at 07:02
  • Are you using chrome? – Freeman Lambda Apr 18 '13 at 07:12
  • Nope, it's not on the same page. The cookie is set in a login page and i try to display the cookie value on the index page without any result. And yes, i'm using Chrome – Maurice Apr 18 '13 at 07:13
  • In chrome cookies are restricted from local use. Acces your website through 127.0.0.1/ rather than through localhost/ to work a way around chrome's behaviours. Also check this question on SO: [link](http://stackoverflow.com/questions/335244/why-does-chrome-ignore-local-jquery-cookies/6468432#6468432) – Freeman Lambda Apr 18 '13 at 07:16

3 Answers3

2

See this issue: Cookies on localhost with explicit domain

Domain names in a cookie must contain two dots. Localhost therefor is invalid.

Community
  • 1
  • 1
Luceos
  • 6,629
  • 1
  • 35
  • 65
0

Be sure that you're trying to access the cookie in the right path/domain :

 setcookie ('uHash', $uCookie, time()+60*60*24*30, /path/of/the/website, false);

You need to redirect to having the headers sending your cookie before you can catch it.

See setcookie doc.

soyuka
  • 8,839
  • 3
  • 39
  • 54
0

We don't have all the relevant code but I guess it looks like this:

setcookie('uHash', $uCookie, time()+60*60*24*30);
echo $_COOKIE['uHash'];

From the manual:

Once the cookies have been set, they can be accessed on the next page load with the $_COOKIE or $HTTP_COOKIE_VARS arrays.

If you absolutely need it on the same page, you can probably do this:

setcookie('uHash', $uCookie, time()+60*60*24*30);
$_COOKIE['uHash'] = $uCookie;
echo $_COOKIE['uHash'];
Álvaro González
  • 142,137
  • 41
  • 261
  • 360
  • Nope, it's not on the same page. The cookie is set in a login page and i try to display the cookie value on the index page without any result – Maurice Apr 18 '13 at 07:12