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);
}
2
  • Possible duplicate of PHP parse/syntax errors; and how to solve them? Commented May 29, 2019 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'] Commented May 29, 2019 at 16:55

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

That helped break it down for the answer. I appreciate it.
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);
    }

1 Comment

[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.]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.