Jump to content

setcookie() in function doesn't work


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 post
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 post
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 post
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 post
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 post
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 post
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 post
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 post
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 post
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 post
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 post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...