0

I am trying to set a cookie as part of a login script but the cookies never seem to set. I've checked the code with copious debug points and the st cookie is called and setcookie responds true but on inspection no cookies exist. I've double checked by using chrome to look at my cookies - there are none there for this app.

The step before this checks for headers already sent so I know that is not the problem.

<?php
// ...
public function set_cookie($cookie,$value,$time=0){
    $cookieCONF = core::get()->factory()->get_config('cookie',array('path'=>'/','domain'=>'.'));
    core::get()->debug()->log("COOKIE[{$cookie}]", $value, FALSE, 7);
    if($time!==0){
        $time= time()+$time;
    }
    if(!is_array($value)){
        setcookie($cookie, $value, $time, $cookieCONF['path'], $cookieCONF['domain']);
    }else{
        foreach($value as $val=>$ue){
            core::get()->debug()->log("{$cookie}[{$val}]", $ue, FALSE, 8);
            if(setcookie("{$cookie}[{$val}]", $ue, $time, $cookieCONF['path'], $cookieCONF['domain'])){
                core::get()->debug()->log('COOKIE RESULT',"SET {$cookie}[{$val}]={$ue}", FALSE, 8);
            }else{
                core::get()->debug()->log('COOKIE RESULT',"NOPE {$cookie}[{$val}]={$ue}", FALSE, 8);
            }
        }
    }
}

echoing out the values the function being called is:

setcookie("user[k]", "295f<SNIP>98f2", $time, "/~username/folder/", "localhost");

(Except with the path information for the user and folder in actual use).

This is the debug line that shows that setcookie() is returning true (success).

[15] => Array
    (
        [message] => COOKIE RESULT
        [ref] => SET user[k]=295f<SNIP>98f2
    )

So unless there is a limit to numeric keys only that I am unaware of I cannot see why these cookies refuse to set.

What have I done wrong?

Edit

Cookies can take non-numeric keys.

Changing the code to

setcookie("user[k]", "295f<SNIP>98f2", $time);

resulted in cookies being set. However, that set to too wide a scope.

  • 1
    debugging: try a very simple php script with a hard-coded html page and that cookie? There may be other issues with the code you are using. It won't take long to do and will determine if the cookie is the issue or not? – Ryan Vincent Jan 19 '16 at 14:35

1 Answers1

0

Cookies can take non-numeric keys.

Changing the code to:

 setcookie("user[k]", "295f<SNIP>98f2", $time);

resulted in cookies being set. Likewise:

setcookie("user[k]", "295f<SNIP>98f2", $time, "/~username/folder/");

was also fine.

It transpires that localhost cannot be explicitly set as domains must have at least two dots.

So the problem here was not the code - it was setting the cookie but the browser was rejecting it.

Thus, setting localhost to null explicitly solved the problem.

public function set_cookie($cookie,$value,$time=0){
    $cookieCONF = core::get()->factory()->get_config('cookie',array('path'=>'/','domain'=>'.'));
    core::get()->debug()->log("COOKIE[{$cookie}]", $value, FALSE, 7);
    if($time!==0){
        $time= time()+$time;
    }
    if($cookieCONF['domain']=='localhost'){
        $cookieCONF['domain']=null;
    }
    if(!is_array($value)){
        setcookie($cookie, $value, $time, $cookieCONF['path'], $cookieCONF['domain']);
    }else{
        foreach($value as $val=>$ue){
            core::get()->debug()->log("{$cookie}[{$val}]", $ue, FALSE, 8);
            if(setcookie("{$cookie}[{$val}]", $ue, $time, $cookieCONF['path'], $cookieCONF['domain'])){
                core::get()->debug()->log('COOKIE RESULT',"SET {$cookie}[{$val}]={$ue}", FALSE, 8);
            }else{
                core::get()->debug()->log('COOKIE RESULT',"NOPE {$cookie}[{$val}]={$ue}", FALSE, 8);
            }
        }
    }
}
Community
  • 1
  • 1