简体   繁体   中英

restricting access to specific users in php error

I have a made a simple website with login. My users sql table is as shown below: 在此处输入图片说明

my login form is like below:

 <div class="container"> <div class="row"> <div class="col-md-4 col-md-offset-4"> <div class="login-panel panel panel-success"> <div class="panel-heading"> <h3 class="panel-title">Sign In</h3> </div> <div class="panel-body"> <form role="form" method="post" action="login.php"> <fieldset> <div class="form-group" > <input class="form-control" placeholder="E-mail" name="email" type="email" autofocus> </div> <div class="form-group"> <input class="form-control" placeholder="Password" name="pass" type="password" value=""> </div> <input type="submit" value="login" name="login" > 

i want to restrict some users from entering some pages. i have made the access column for this. I have added the following code to the protected page

session_start();
  if($_SESSION["access"]!=0)
            {
            header('Location: login.php');
            }

there are some problems with this coode i guess. when the access=0,the page is shown even if the user is not logged in and shows like Undefined index: access in C:\\xampp\\htdocs\\nurse\\index.php on line 12 when the access is set to 1 ,its redirected to login page. can anyone help me?

You need to check session is set or not like this

    if(isset($_SESSION["access"])) {
        if($_SESSION["access"] !=0) {
        header('Location: login.php');
     }
   }

You need to check if the access key exists on $_SESSION.
Try changing your code to
if( !isset($_SESSION['access']) ) { /* redirect to login */ }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM