使用mysql中的访问列在php中设置用户角色时出错

Posted

技术标签:

【中文标题】使用mysql中的访问列在php中设置用户角色时出错【英文标题】:Error in setting user roles in php using access column in mysql 【发布时间】:2018-06-07 05:11:22 【问题描述】:

我有一个登录/注册网站。管理员和用户都在mysql的同一个表中。

我的用户表如下:

有些页面只有管理员才能访问。我已经创建了一个访问列并将值放在图像中。我有以下 php 代码来检查它是否是管理员

<?
  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"  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>

代码不起作用,即使它的管理员登录,它也会将我重定向到登录页面。

谁能帮助我?

提前致谢。

【问题讨论】:

您的页面中有start_session()...! 是的。在顶部 分享您的完整代码以便更好地理解...! 我已经更新了代码 你在哪里设置$_SESSION['access']在你的代码..? 【参考方案1】:

试试这个

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");
  

【讨论】:

让我检查并告诉

以上是关于使用mysql中的访问列在php中设置用户角色时出错的主要内容,如果未能解决你的问题,请参考以下文章

使用不同的用户角色在passport-jwt中设置身份验证很热门?

在开发过程中设置管理员角色

如何在 Spring Security 3 中设置用户角色?

在 .htaccess 中设置 php_value 时出现意外错误?

GWT - 仅在 Internet Explorer 中设置 document.domain 时出现“拒绝访问”JavaScript 错误

在 asp.net mvc 中设置用户对每个应用程序的访问权限