我是一个nOOb。我知道如何制作简单的登录脚本,但我不太了解如何使用$ _SESSION跨不同页面访问数据库信息/列。我试图根据user_level显示表格。
用户表
id |username |email |password | user_level | user_roles |last_login
dashboard.php
<?php if ($_SESSION['login_user'] == user_level["admin"]):?>
<div class="table">
Admin table
</div>
<?php else: ?>
<div class="table">
Employee table
</div>
的login.php
<?php
ob_start();
include 'includes/header.php';
if($_SERVER["REQUEST_METHOD"] == "POST") {
// username and password sent from form
$myusername = mysqli_real_escape_string($db,$_POST['username']);
$mypassword = mysqli_real_escape_string($db,$_POST['password']);
$update_query = "UPDATE users SET last_login=now() WHERE username='" .$myusername. "'";
$update_q = "UPDATE users SET last_update=now() WHERE username='" .$myusername. "'";
$sql = "SELECT id FROM users WHERE username = '$myusername' and password = '$mypassword'";
$execute_update = mysqli_query($db,$update_query);
$execute_u= mysqli_query($db,$update_q);
$result = mysqli_query($db,$sql);
$row = mysqli_fetch_array($result,MYSQLI_ASSOC);
$active = $row['active'];
$count = mysqli_num_rows($result);
// If result matched $myusername and $mypassword, table row must be 1 row
if($count == 1) {
$_SESSION['login_user'] = $myusername;
header("location: dashboard.php");
}else {
$error = "Your Login Name or Password is invalid";
}
}
?>
session.php文件
<?php
include '../includes/config.php';
$user_check = $_SESSION['login_user'];
$ses_sql = mysqli_query($db,"SELECT username FROM users WHERE username = '$user_check' ");
$row = mysqli_fetch_array($ses_sql,MYSQLI_ASSOC);
$login_session = $row['username'];
if(!isset($_SESSION['login_user'])){
header("location:../login.php");
}
?>
答案 0 :(得分:0)
由于$ _SESSION [&#39; login_user&#39;]和数据库中的用户名相同,您可以在多个页面中使用$ _SESSION变量来获取登录用户的user_level。
//checkUserLevel.php
$user_check = $_SESSION['login_user'];
$ses_sql = mysqli_query($db,"SELECT * FROM users WHERE username = '$user_check' ");//This query will only return 1 user if usernames are unique
while($row = mysqli_fetch_assoc($ses_sql)){
$user_level = $row['user_level']; //Get user_level of that user
}
//Use in dashboard.php
include 'includes/checkUserLevel.php';
if($user_level=='admin'){
//Show Tables
}else{
//Do something else
}
如果不让我知道并且我会尝试帮助你应该可以使用这种方法