This question already has an answer here:
- Global Variables vs. Local Variables 6 answers
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>