douluan8828 2013-11-19 19:05
浏览 20
已采纳

为什么变量在函数protect()输出什么? [重复]

This question already has an answer here:

I wrote this code to read the access leve of users logged in so, protect access to pages based on the access level of user logged in

and do function has sessionloginid and $v but $v variable output nothing what I need to edit in the function code? error message appear say :
Notice: Undefined variable: v

 if(isset($_SESSION['sessionloginid']))// point to id of user logged in
        {  
        $query =
        "SELECT * 
        FROM privilege 
        where login_id='".$_SESSION['sessionloginid']."'  " ;



    $access_level= array();
    $result = mysqli_query($link,$query) or die('');
    while($row = mysqli_fetch_array($result, MYSQLI_ASSOC))
            {


         $_SESSION['sessionloginid']= $row['login_id']; 
        $_SESSION['sessionaccess']= $row['AccessLevel'];//output 
         $access_level[]=$_SESSION['sessionaccess'];
        print_r ($access_level);//Array([0]=>1)Array([0]=>1[1]=>2) 

        }   }

foreach($access_level as $k => $v)
    {
        print_r($v) ;// output 12 

    }
function protect(){
 if($_SESSION['sessionloginid']==true && $v !=1)
 {
 echo $v; //output nothing
 }

}
    protect($v);
</div>
  • 写回答

1条回答 默认 最新

  • dsvcqvp139098 2013-11-19 19:07
    关注

    You need to pass in $v as a function parameter

    protect($v);
    

    At it's current scope, once the foreach loop ends, $v doesn't exist. It only exists within the loop, so you'll have to either call the function inside the loop, or set a variable on a higher scope to the value so you can use it elsewhere.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料