1

I am receiving the following foreach error on my PHP file and I have no idea how to fix it. Does anyone have any ideas?

When I load the page I get this:

Warning: Invalid argument supplied for foreach() in /home/mysite/public_html/merge/class/global_functions.php on line 61

Warning: Invalid argument supplied for foreach() in /home/mysite/public_html/merge/class/global_functions.php on line 89

Line 61 and 89 of my /class/global_functions.php are as followed:

Here is my code from line 61 to line 98:

    foreach($GLOBALS['userpermbit'] as $v)
    {
        if(strstr($v['perm'],'|'.$pageperm_id[0]['id'].'|'))
            return true;
    }

    //if they dont have perms and we're not externally including functions return false
    if ($GLOBALS['external'] != true) return false; return true;

}

//FUNCTION: quick perm check using perm info from the onload perm check 
function stealthPermCheck($req)
{
    #if theyre an admin give them perms
    if(@in_array($GLOBALS['user'][0]['id'], $GLOBALS['superAdmins']))
            return true;    

    if(!is_numeric($req))
    {
        #if the req is numeric we need to match a title, not a permid. So try to do that
        foreach($GLOBALS['userpermbit'] as $v)
        {
            if(stristr($v['title'],$req))
                return true;
        }
    }else{
        #check if they have perms numerically if so return true
        foreach($GLOBALS['userpermbit'] as $v)
        {
            if(strstr($v['perm'],'|'.$req.'|'))
                return true;
        }
    }

    #if none of this returned true they dont have perms, return false
    return false;
}
0

4 Answers 4

5

foreach works only if the variable is either an array or an object.

If you provide something else you see the error you see:

Warning: Invalid argument supplied for foreach() in ...

To make that error stop, ensure that the variable you pass to foreach is either an array or an object.

Evil php coderz cope with it this way if they want it normally to be an array but are too lazy to check anything cauz life's too short:

foreach ((array) @$prunzels as $do_not_care)
{
}

I highly recommend it, because you use $GLOBALS anyway, which makes me believe you want to level up in PHP evilness.

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

8 Comments

I was warned to not use irony on the internet, so explicitly spoken: just joking.
Just put ob_start(); as the first statement in your PHP-files and you will never see errors again.
@fragmentedreality: Uh, I guess you mean error_reporting(0); to never see errors again.
Yeah, that would work, too. But I thought with all the globals and stuff… In fact, I made the same mistake as you did, @hakre and used irony…
No, but your first comment seemed to be… Besides, my method works. I did not say that you would get any output at all by using it.
|
2

Change your code on 69 line to this: & do same on 89

$GLOBALS['userpermbit'] : this might be blank & not being cosidered as array by foreach.

$u_per_arr = $GLOBALS['userpermbit'];
if(!is_array($u_per_arr)) {
 $u_per_arr = array();
}

foreach($u_per_arr as $v)

Comments

1

$GLOBALS['userpermbit'] is either not set or not an array. you'll need to check where it has been initialized, or whats is going wrong with it. Try to give us more context.

1 Comment

#set up an sql query to get their perm values (if they have no usergroup (ie theyre a guest) set group to 0) if(empty($GLOBALS['user'][0]['permgroup'])) $usergroup = '0'; else $usergroup =& $GLOBALS['user'][0]['permgroup']; #select usergroup permissions value $db->select("title, perm FROM permtable WHERE id IN({$usergroup})", $GLOBALS['userpermbit']); $db->kill();
0

The error says, that wrong type of variable has been passed to foreach() construct. The error occured in line 89.

The foreach() construct expects first argument to be an array. Your $userpermbit variable, used in line 89 as argument of foreach() construct seems not to be an array type.

Search your code for any occurrences of $userpermbit and find out where it is set. Correct it to set $userpermbit as an array.

Comments

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.