在$ _Session中存储用户权限

时间:2018-08-24 04:44:19

标签: php html session mysqli

我刚刚尝试过在登录页面中使用会话,并且我还想存储“权限”以启用或禁用某些页面的访问。存储“用户”有效,但不能存储“权利”。

  

更新:已正常运行。干杯!

表格:

"SELECT * FROM {0} WHERE 1=0"

登录页面/会话:

CREATEDATABASE`account`
CREATETABLE`profile`

id INT(3) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
user VARCHAR(20) NOT NULL,
pass VARCHAR(20) NOT NULL,
rights VARCHAR(10) NOT NULL,

我从每个页面开始:

<?php
	session_start();
	$user = $_POST['user'];
	$pass = $_POST['pass'];
		
	$conn = mysqli_connect("localhost", "root", "", "account");
	
	$query = mysqli_query($conn, "SELECT * FROM account.profile WHERE user = '$user', pass = '$pass' ")
		or die("Failed to connect DB".mysqli_error());
					
	$rows = mysqli_num_rows($query);
	$right=$rows['right'];
	if ($rows == 1 ){
	$_SESSION['user']=$user; 

		header("Location: main.php");
		} 
	else 
	{
	 echo "<script>
     alert('Username and Password not found!');
     window.location.href='logout.php';
     </script>";
		}
?>

希望有人可以引导我走正确的道路。谢谢!

1 个答案:

答案 0 :(得分:0)

第一: 无需在下面的行,因为用户仅输入他的用户名和密码。把它拿出来。 $rights = $_POST['rights'];

第二,更改此行: $_SESSION['right'] = $right;$_SESSION['right'] = $role; 您之前将选定的用户权限设置为变量$role而非$right