0

I'm trying to set cookies for the first time. I've been following the documentation on W3 schools but it doesn't seem to be working for me.

Here is my code (some naming omitted for this example):

$namenospace = 'thisnameexample';

function someSetup($somename){
    $nameofcookie = $somename . '_some_value';

    if(!isset($_COOKIE[$nameofcookie])) {
        $someValue = rand(1, getrandmax());
        $cookielastsfor = time() + (86400 * 30);
        setcookie($nameofcookie, $someValue, $cookielastsfor, "/");
    }
    else {
        $someValue = $_COOKIE[$nameofcookie];
    };

    return $someValue;
};

$setSomeValue = someSetup($namenospace);

As you can see, I'm trying to check if a cookie is set, if it is, grab the value and use it, otherwise, set the cookie, then return the value anyway.

For some reason this setcookie() function isn't working. Does anyone have any insight on this?

Possibly worth noting this is a wordpress site if that's relevant?

Thanks!

Edit: have updated as per scope issues highlighted in comments and still not working as intended.

Edit again: definitely not a duplicate. My team and I discovered the issue and would like this unmarked as duplicate so an answer can be given please!

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
jock.perkins
  • 469
  • 6
  • 23
  • 1. You dont call `someSetup()` – RiggsFolly Jun 14 '17 at 10:42
  • @RiggsFolly - have updated now, I forgot to update the name of the function there where I have changed the code for the question – jock.perkins Jun 14 '17 at 10:43
  • 2. Variable **SCOPE, Scope, scope**........ `$namenospace` is INVISIBLE inside the `someSetup()` function. Pass it as a parameter if you want to use it in the function – RiggsFolly Jun 14 '17 at 10:44
  • `$namenospace` is out of scope in `someSetup`. You need to use `function someSetup() use ($namenospace) {` – Styphon Jun 14 '17 at 10:44
  • Have updated and tried this, as per edits and still not working – jock.perkins Jun 14 '17 at 10:49
  • 1
    @RiggsFolly I don't think this is a duplicate, although scope was an issue it wouldn't have stopped the creation of the cookie, the name would just have been `_some_value`. Please consider re-opening this. – Styphon Jun 14 '17 at 10:55
  • @RiggsFolly - definitely not a duplicate as me and my team now have the answer to this. Please unmark as duplicate so an answer can be given. – jock.perkins Jun 14 '17 at 10:57
  • @jock.perkins have you looked in the debug tools to see if there is a cookie? (In chrome) press F12 to open the debug tools, switch to the application tab and look under Storage > Cookies to see if it's being created. – Styphon Jun 14 '17 at 10:57
  • You have changed your code! **A question on SO is not a movable feast!!** But as no actual answers were given, OK – RiggsFolly Jun 14 '17 at 11:00

1 Answers1

2

Make sure you are setting your cookies before output. Where are you calling your function?

As an alternative, you could use JS to set a cookie on client side.

  • I would have layed bets on that. Again DUPLICATE OF https://stackoverflow.com/questions/8028957/how-to-fix-headers-already-sent-error-in-php/8028987#8028987 – RiggsFolly Jun 14 '17 at 11:15
  • 1
    @RiggsFolly quiet day at the office? –  Jun 14 '17 at 11:23
  • @James :) Kinda. – RiggsFolly Jun 14 '17 at 11:24
  • @jock.perkins If you had been monitoring your php error log or better still running with error reporting turned on there would have been a **wopping great hint** in the error log giving an error message that you could have used in a search that would have told you this – RiggsFolly Jun 14 '17 at 11:30
  • Hi @jock.perkins –  Sep 20 '19 at 15:29