skip to Main Content

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

Answers


  1. Chosen as BEST ANSWER

    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);
        }
    

  2. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search