duanbo1659 2009-07-01 16:05
浏览 112
已采纳

PHP in_array不起作用

I am using the PHP in_array() function in order to authenticate (with sessions) if a user can access a particular page. For some reason, it is not working...

PHP PAGE

session_start();
require_once('../scripts/functions.php'); 
$role_auth = @$_SESSION['role_auth'];
access($role_auth, array(0,1,2,3,4));

access FUNCTION

function access($role_auth, $array){

if(!(in_array($role_auth, $array))){ 
   header("Location: ../index.html");
}
}

If I insert print statements in the function, I can see that all of the correct values are being passed into the function. The problem is, if the function is called without a session variable set, for some reason it is considered as being in the array, and it authenticates.

Any ideas?

  • 写回答

4条回答 默认 最新

  • dongwei9365 2009-07-01 16:11
    关注

    you may want to enable strict type checks by using:

    in_array($role_auth, $array, true)
    

    as what is likely happening is that $role_auth is being eval'd as false and that could match 0 in your in_array statement.

    what you SHOULD be doing is this:

    session_start(); 
    require_once('../scripts/functions.php'); 
    $role_auth = (isset($_SESSION['role_auth']))?$_SESSION['role_auth']:-1; 
    access($role_auth, array(0,1,2,3,4));
    

    or something similiar. nothing good ever comes of using the @ operator

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作