使用数组中的 contextid 进行 Moodle 身份验证
任何人都可以解释为什么这不起作用:
$USER->id
是登录用户。
$contextroles = get_records_sql("SELECT userid FROM {$CFG->prefix}role_assignments WHERE contextid = 23 AND roleid = 3");
if (in_array($USER->id, $contextroles)) {
echo'your in<br />';
echo $USER->id.'<br />';
print_r($contextroles);
}
else{
echo'Access denied<br />';
echo $USER->id.'<br />';
print_r($contextroles);
}
这是输出:
访问被拒绝
5410
Array ( [7] => stdClass 对象 ( [userid] => 7 ) [9] => stdClass 对象 ( [userid] => 9 ) [27] => stdClass 对象 ( [userid ] ] => 27 ) [98] => stdClass 对象 ( [userid] => 98 ) [203] => stdClass 对象 ( [userid] => 203) [252] => stdClass 对象 ( [userid] => 252) [5410] => stdClass 对象 ( [userid] => 5410 ) )
任何帮助都会很大赞赏。
Can anyone shed any light as to why this is not working:
$USER->id
is the logged in user.
$contextroles = get_records_sql("SELECT userid FROM {$CFG->prefix}role_assignments WHERE contextid = 23 AND roleid = 3");
if (in_array($USER->id, $contextroles)) {
echo'your in<br />';
echo $USER->id.'<br />';
print_r($contextroles);
}
else{
echo'Access denied<br />';
echo $USER->id.'<br />';
print_r($contextroles);
}
This is the output:
Access denied
5410
Array ( [7] => stdClass Object ( [userid] => 7 ) [9] => stdClass Object ( [userid] => 9 ) [27] => stdClass Object ( [userid] => 27 ) [98] => stdClass Object ( [userid] => 98 ) [203] => stdClass Object ( [userid] => 203 ) [252] => stdClass Object ( [userid] => 252 ) [5410] => stdClass Object ( [userid] => 5410 ) )
Any help would be greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
$contextroles 是一个对象数组,您正在搜索的指针位于该对象内。 in_array 无法处理对象,因此失败。您可以使用以下代码片段来获得所需的结果:
http: //www.php.net/manual/en/function.in-array.php#105937
$contextroles is an array of objects and the needle you are searching is within the object. in_array cannot handle objects and hence it fails. You can use the following code snippet to get the desired results:
http://www.php.net/manual/en/function.in-array.php#105937