使用 adldap 为 foreach() 提供的参数无效
我正在使用 adldap http://adldap.sourceforge.net/
并且我将会话从页面传递到页面,并检查以确保会话中的用户名是某个成员组的成员,在本例中,它是 STAFF 组。
<?php
ini_set('display_errors',1);
error_reporting(E_ALL);
require_once('/web/ee_web/include/adLDAP.php');
$adldap = new adLDAP();
session_start();
$group = "STAFF";
//$authUser = $adldap->authenticate($username, $password);
$result=$adldap->user_groups($_SESSION['user_session']);
foreach($result as $key=>$value) {
switch($value) {
case $group:
print '<h3>'.$group.'</h3>';
break;
default:
print '<h3>Did not find specific value: '.$value.'</h3>';
}
if($value == $group) { print 'for loop broke'; break; }
}
?>
它给了我错误:警告:第 15 行为 foreach() 提供的参数无效,这是这行代码: foreach($result as $key=>$value) {
当我取消注释代码 $authUser = $adldap -> 验证($用户名,$密码);并输入适当的用户名和密码,它工作正常,但我不必这样做,因为会话是有效的,我只想查看 valid_session 中存储的用户名是否属于 STAFF 组。
为什么它会给我带来这个问题?
I am using adldap http://adldap.sourceforge.net/
And I am passing the session from page to page, and checking to make sure the username within the session is a member of a certain member group, for this example, it is the STAFF group.
<?php
ini_set('display_errors',1);
error_reporting(E_ALL);
require_once('/web/ee_web/include/adLDAP.php');
$adldap = new adLDAP();
session_start();
$group = "STAFF";
//$authUser = $adldap->authenticate($username, $password);
$result=$adldap->user_groups($_SESSION['user_session']);
foreach($result as $key=>$value) {
switch($value) {
case $group:
print '<h3>'.$group.'</h3>';
break;
default:
print '<h3>Did not find specific value: '.$value.'</h3>';
}
if($value == $group) { print 'for loop broke'; break; }
}
?>
It gives me the error: Warning: Invalid argument supplied for foreach() on line 15, which is this line of code: foreach($result as $key=>$value) {
When I uncomment the code $authUser = $adldap->authenticate($username, $password); and enter in the appropriate username and password, it works fine, but I shouldn't have to, since the session is valid, I just want to see if the username stored within the valid_session is apart of the STAFF group.
Why would it be giving me that problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
根据 此源文件< /a>,
user_groups()
如果用户名为空,则返回 false(在某些其他情况下,也请检查源代码)。我敢打赌你的$_SESSION["user_session"]
是空的,那么$result
就是 false。您无法在非数组上运行foreach
,这就是您收到警告的原因。您将需要找出会话变量为何为空的原因,和/或检查 $result 是否是一个数组,因为对其执行了 foreach 操作:
According to this source file,
user_groups()
will, return false if the user name was empty (and in some other cases too, check the source). I bet your$_SESSION["user_session"]
is empty, and$result
is then false. You can't runforeach
on a non-array which is why you get the warning.You will need to find out why your session variable is empty, and/or check whether
$result
is an array because doing a foreach on it: