0

If have 2 errors:

Invalid argument supplied for foreach()...

Use of undefined constant c - assumed 'c' in...

I first am trying to find if the cookie exists. Then if a duplicate cookie exists in the array.

I was looking at other examples on here, but they seems to show the same foreach loops. Is there something I am missing?

$seo = "perma-link"
$count = 0;
$duplicate = 0;
if (isset($_COOKIE['c'])) {
    foreach($_COOKIE['c'] as $key => $value) {
        if($value === $seo){
            $duplicate = 1;
        }
    }
} else {
    setcookie(c[$count], $seo, time()+3600);
    $duplicate = 1;
}
if($duplicate == 0){
    $count = count($_COOKIE['c']);
    setcookie(c[$count], $seo, time()+3600);
}
dWinder
  • 11,597
  • 3
  • 24
  • 39
Sol
  • 949
  • 1
  • 11
  • 23
  • Possible duplicate of [PHP parse/syntax errors; and how to solve them?](https://stackoverflow.com/questions/18050071/php-parse-syntax-errors-and-how-to-solve-them) – Dave May 29 '19 at 16:54
  • What is `c` in `setcookie(c[$count], $seo, time()+3600);`? missing `$`? And I guess `$_COOKIE['c']` is a string... (maybe comma separated and explode needed?) Please post the content of `$_COOKIE['c']` – dWinder May 29 '19 at 16:55

2 Answers2

1

Invalid argument supplied for foreach()

Your $_COOKIE['c'] is not an array.

Use of undefined constant c

setcookie(c[$count], $seo, time()+3600);

What is "c"? The interpreter says about this error. The first argument of setcookie() is a cookie name. You shold provide a correct string. The second argument of setcookie() is a value. If you want to iterate this using foreach it should be an array. Check you $seo variable.

James Bond
  • 2,229
  • 1
  • 15
  • 26
0

I did one too many foreach loops and "c[$count]" has to have quotation marks to work. Fixed it. I appreciate the feedback James Bond.

    $seo = "perma-link"
    $count = 0;
    $duplicate = 0;
    if (isset($_COOKIE['c'])) {
        foreach($_COOKIE as $k => $v) {
                if($k == "c" && $v == $seo){
                    $duplicate = 1;
                }
         }
    }else{
        setcookie("c[$count]", $seo, time()+3600);
        $duplicate = 1;
    }
    if($duplicate ==0){
        $count = count($_COOKIE['c']);
        setcookie("c[$count]", $seo, time()+3600);
    }
Sol
  • 949
  • 1
  • 11
  • 23
  • [I love how people just put a negative on everything in stack overflow without any input on why. [Pro]grammers should be a positive group with people helping each other.] – Sol May 22 '21 at 05:05