0

I'm checking to see whether a cookie is set before setting it (As a unique variable). It was working on my LocalHost but it won't work on the live site.

They're allowed in the php.ini file on my domain.

Code:

$bits = 3;
$cookieID = bin2hex(openssl_random_pseudo_bytes($bits));
if (!isset($_COOKIE["UserID"])) { //check cookie is not set
    setcookie("UserID", $cookieID, time()+(60*60*24*30), '/', 'domain.co.uk');
    $_COOKIE["UserID"] = $cookieID; //call the cookie to use immediately.
}
setCookie("Testing", "Test"); //testing
$UserID = "'" . $_COOKIE["UserID"] . "'";
echo $_COOKIE["UserID"]; //check cookie is there

Neither the testing cookie or the UserID cookie are working.

The cookie is simply not being set. The code is at the TOP of my header.php before anything else. I've tried unsetting the cookie and doing it again, but there is not cookie in my Google Chrome settings either.


EDIT:

I've looked elsewhere, and none of the answers seem to solve my issue or are doing something different.

As far as I am aware, this has not been answered. At least not one that resolves my issue.


EDIT:

I've now got it to set the cookie, however the cookie is not being saved across the domain, it's changing on each page. (As though the if (!isset...) is not working.

EDIT

I fixed it.

$bits = 3;
$cookieID = bin2hex(openssl_random_pseudo_bytes($bits));
if (!isset($_COOKIE["UserID"])) {
    setCookie("UserID", $cookieID, 0, "/", "domain.co.uk");
};

My main issue was I had overcomplicated it, so I reverted it back to how I had it before and changed some things to add in the "/" and "domain.co.uk" and now it works.

Community
  • 1
  • 1
Mallander
  • 336
  • 3
  • 16

0 Answers0