Jump to content

setcookie() in function doesn't work


WoHe

Recommended Posts

I am trying to set a cookie when there is no cookie from my website found on the user's PC, but the following line of code doesn't work inside a function:

$cookiename = "test";
$cookievalue = "tests";

setcookie($cookiename, $cookievalue, false, "/", "localhost");

 

It does work however if it is placed outside of the function. For exampe, the code above works, but the next code doesn't:

$cookiename = "test";
$cookievalue = "tests";

function addCookie() {
    setcookie($cookiename, $cookievalue, false, "/", "localhost");
}

addCookie();
Edited by WoHe
Link to comment
Share on other sites

That's because the variables are not available inside the scope of the function. This will work:

function addCookie() {
    $cookiename = "test";
    $cookievalue = "tests";
    setcookie($cookiename, $cookievalue, false, "/", "localhost");
}

addCookie();

And this will work:

$cookiename = "test";
$cookievalue = "tests";
addCookie($cookiename, $cookievalue);

function addCookie($name, $value) {
    setcookie($name, $value, false, "/", "localhost");
}
  • Like 1
Link to comment
Share on other sites

Thanks, but it also doesn't work when I use it in an if-statement, like this:

$cookiename = "test";
$cookievalue = "test";

if(!isset($_COOKIE[$cookiename])) {
    echo "no cookie found <br />";
    setcookie($cookiename, $cookievalue, false, "/", "localhost");
    
    if(!isset($_COOKIE[$cookiename])) {
        echo "not able to set cookie <br />";
    } else {
        echo "cookie '" . $_COOKIE[$cookiename] . "' is set correctly <br />";
    }
} else {
    $cookievalue = $_COOKIE[$cookiename];
    echo "cookie found: " . $cookievalue . "<br />";
}

(this is btw my whole code for checking if there is a cookie and setting it if none is found)

Link to comment
Share on other sites

That code is working.

 

A cookie you set won't be in the $_COOKIE array until the next time you load the page, which is why it's telling you that it was unable to set the cookie.

Link to comment
Share on other sites

That is what I thought too, but I keep refreshing the page with this exact code and it keeps telling "no cookie found not able to set cookie".

Link to comment
Share on other sites

This is not likely causing the issue, but you're setting the $expires parameter to "false" instead of a number, which it expects. You should set it to 0 if you want the cookie to expire when the browsing session ends.

 

Have you tried removing the domain parameter?

 

Update:

It seems "localhost" is the issue:

http://php.net/manual/en/function.setcookie.php#73107

Edited by Ingolme
Link to comment
Share on other sites

And what do I need to set the $expires parameter if I don't want it to expire?

 

Also, I don't think localhost is be the issue, because in all the other cases it does work, except for the case where it is set in the if-statement...

 

 

PS: I've set the expire parameter to time() + (86400 * 30) and it still doesn't work...

Edited by WoHe
Link to comment
Share on other sites

Cookies have to expire. Setting expires to "false" (equivalent to zero) will cause the cookie to disappear once the browsing session ends.

You can set the cookie to a date far in the future, like six months:

$expires = time() + 60*60*24*30*6;
setcookie($cookiename, $cookievalue, $expires, "/", "localhost");

According to the comment on the PHP manual, setting the domain to "localhost" does not work, though the comment is from 9 years ago. Perhaps PHP has improved since then. I would test not setting the domain or setting an empty string to see if that fixes it.

Link to comment
Share on other sites

I had to set the domain to "localhost" to let the code work, I have now also set an expire date.

 

The only remaining problem is that

setcookie($cookiename, $cookievalue, time() + (86400 * 30), "/", "localhost");

doesn't work in an if-statement (as mentioned earlier), it does work outside and inside of any function, but not inside of the if-else statement...

 

 

For example:

 

This works:

setcookie($cookiename, $cookievalue, time() + (86400 * 30), "/", "localhost");

This works:

function set($cookiename, $cookievalue) {
    setcookie($cookiename, $cookievalue, time() + (86400 * 30), "/", "localhost");
}

set($cookiename, $cookievalue);

But this doesn't work:

if(!isset($_COOKIE[$cookiename])) {
    echo "no cookie found <br />";
    setcookie($cookiename, $cookievalue, time() + (86400 * 30), "/", "localhost");   
}
Edited by WoHe
Link to comment
Share on other sites

You'll have to do some debugging, I don't see any reason why a simple if condition would change how a function works.

 

Have you tried having a PHP file with just the bare minimum in it?

 

You should check to see the value of $_COOKIE.

var_dump($_COOKIE);
  • Like 1
Link to comment
Share on other sites

So I have rebuild the code piece by piece and I have found the problem.

The problem was in this piece of the code:

echo "no cookie found <br />";

I don't know why this would cause a problem, but I don't need it anyway for the actual code, it was just to check if my code works (ironically).

 

I'm still learning php so if you could figure out how this causes a problem, could you please explain it to me? Thanks already!

Link to comment
Share on other sites

Oh, right. You cannot print anything at all before setting a cookie. Cookies get sent with the HTTP headers, and once something is printed headers are no longer allowed to be sent.

  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...