简体   繁体   中英

Error in setting user roles in php using access column in mysql

I have a login/register website. both admin and users are in the same table in mysql.

My users table is below:

在此处输入图片说明

There is certain pages where only admins can visit. I have made an access column and put the values like in the image. I have the following php code for checking whether its admin or not

 <? include("config.php"); session_start(); $user_email=$_SESSION['email']; $check_user="select * from users WHERE user_email='$user_email'"; $run=mysqli_query($dbcon,$check_user); while($row = $run->fetch_assoc()) { $user_name=$row['user_name']; }?> <div class="sing"><?php if ($_SESSION['access'] != 'admin') { header("Location: login.php"); } else if (isset($_SESSION['email']) && $_SESSION['email'] == true) { echo " &nbsp You logged in as " . $_SESSION['email'] ; echo "<div style='float: right;'><a href='logout.php'>logout</a>&nbsp</div>"; } else{} ?> <!DOCTYPE html> <html> <head> <title>Nurses Pulse </title> <!--/tags --> <meta name="viewport" content="width=device-width, initial-scale=1"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <script type="application/x-javascript"> addEventListener("load", function () { setTimeout(hideURLbar, 0); }, false); function hideURLbar() { window.scrollTo(0, 1); } </script> <!--//tags --> <link rel="icon" type="image/png" href="images/imageedit_2_5125240109.gif"/> <link href="css/bootstrap.css" rel="stylesheet" type="text/css" media="all" /> <link href="css/style.css" rel="stylesheet" type="text/css" media="all" /> <link href="css/appointment_style.css" rel="stylesheet" type="text/css" media="all" /> <link href="css/font-awesome.css" rel="stylesheet"> <!-- //for bootstrap working --> <link href="//fonts.googleapis.com/css?family=Source+Sans+Pro:300,300i,400,400i,600,600i,700" rel="stylesheet"> </head> <body> <!-- header --> <div class="header" id="home"> <div class="content white"> <nav class="navbar navbar-default" role="navigation"> <div class="container"> <div class="navbar-header"> <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1"> <span class="sr-only">Toggle navigation</span> <span class="icon-bar"></span> <span class="icon-bar"></span> <span class="icon-bar"></span> </button> <a class="navbar-brand" href="index.php"> <h1 style="color:#e50000"><span class="fa fa-heartbeat" aria-hidden="true"></span>Nurses Pulse </h1> </a> </div> <!--/.navbar-header--> <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1"> <nav> <ul class="nav navbar-nav"> <li><a href="index.php" class="active">Home</a></li> <li class="dropdown"> <a href="#" class="dropdown-toggle" data-toggle="dropdown">Exams <b class="caret"></b></a> <ul class="dropdown-menu"> <li><a href="prometric.php">PROMETRIC</a></li> <li class="divider"></li> <li><a href="haadexam.php">HAAD</a></li> <li class="divider"></li> <li><a href="moh.php">MOH</a></li> <li class="divider"></li> </ul> </li> <li><a href="aboutus.php">About Us</a></li> </ul> </nav> </div> <!--/.navbar-collapse--> <!--/.navbar--> </div> </nav> </div> </div> <!-- banner --> <!--//banner --> <!-- about --> <!-- emergency --> <!-- //emergency --> <!-- services --> <div class="services"> <div class="container"> <h3 class="heading-agileinfo" style="color:#e50000"><span>Sorry! You Have Timed Out!</span></h3> <div class="services-top-grids"> <div> </div><br> <br> <?php $session_value=(isset($_SESSION['correct_score']))?$_SESSION['correct_score']:''; $session_value2=(isset($_SESSION['not_correct_score']))?$_SESSION['not_correct_score']:''; echo "<div class='div-left'> Number Of Correct Answers = " . $session_value . "</div>"; echo "<div class='div-left'> <br>Number Of Wrong Answers = " . $session_value2 . "</div>"; ?> </div> <div class="services-bottom-grids"> <div class="clearfix"></div> </div> </div> </div> <!-- //services --> <!-- Clients --> <!-- //Clients --> <!--/icons--> <!--//icons--> <!-- footer --> <div class="footer_top_agile_w3ls"> <div class="container"> <div class="col-md-3 footer_grid"> <h3>About Us</h3> <p>NursesPulse is for those who are preparing for the staff nurse entrance exam for abroad. </p> </div> <div class="col-md-3 footer_grid"> <h3>Other Services</h3> <ul class="footer_grid_list"> <li><i class="fa fa-angle-right" aria-hidden="true"></i> <a href="exambooking.php" >Book Exam </a> </li> <li><i class="fa fa-angle-right" aria-hidden="true"></i> <a href="#">Course Materials</a> </li> <li><i class="fa fa-angle-right" aria-hidden="true"></i> <a href="membership.php" >Membership </a> </li> <!-- <li><i class="fa fa-angle-right" aria-hidden="true"></i> <a href="#" data-toggle="modal" data-target="#myModal">Nurses Pulse Demo.</a> </li> <li><i class="fa fa-angle-right" aria-hidden="true"></i> <a href="#" data-toggle="modal" data-target="#myModal">Nurses Pulse Demo. </a> </li> --> </ul> </div> <div class="col-md-3 footer_grid"> <h3>Contact Info</h3> <ul class="address"> <li><i class="fa fa-map-marker" aria-hidden="true"></i>Team Pulse, Kannur <span>Kerala.</span></li> <li><i class="fa fa-envelope" aria-hidden="true"></i><a href="mailto:nursespulse.com">nursespulse@gmail.com</a></li> <li><i class="fa fa-phone" aria-hidden="true"></i>+91 95444 12311</li> </ul> </div> <div class="col-md-3 footer_grid "> <h3>Sign up for our Newsletter</h3> <p>Get Started For Free</p> <div class="footer_grid_right"> <form action="#" method="post"> <input type="email" name="Email" placeholder="Email Address..." required=""> <input type="submit" value="Submit"> </form> </div> </div> <div class="clearfix"> </div> </div> </div> <div class="footer_wthree_agile"> <p>© 2018 Nurses Pulse. All rights reserved | Design by <a href="">Bluroe Labs</a></p> </div> <!-- //footer --> <!-- bootstrap-modal-pop-up --> <div class="modal video-modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModal"> <div class="modal-dialog" role="document"> <div class="modal-content"> <div class="modal-header"> Nurses Pulse <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button> </div> <div class="modal-body"> <img src="images/g7.jpg" alt=" " class="img-responsive" /> <p></i></p> </div> </div> </div> </div> <!-- //bootstrap-modal-pop-up --> <!-- js --> <script type="text/javascript" src="js/jquery-2.1.4.min.js"></script> <script> $('ul.dropdown-menu li').hover(function () { $(this).find('.dropdown-menu').stop(true, true).delay(200).fadeIn(500); }, function () { $(this).find('.dropdown-menu').stop(true, true).delay(200).fadeOut(500); }); </script> <script type="text/javascript" src="js/bootstrap.js"></script> </body> </html> 

The code is not working, it's redirecting me to login page even if its admin who logs in.

Can anyone help me?

Thanks in advance.

try this one

while($row = $run->fetch_assoc())
{

 $user_name=$row['user_name'];
 $_SESSION['access'] = $row['access']; //define here

}?>
<div class="sing"><?php

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

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