首页 > 解决方案 > 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: enter image description here

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?

标签: javascriptphphtmlsql

解决方案


您需要检查会话是否设置为这样

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

推荐阅读