我有一个仪表板,它使用包括使其成为具有可变包含的静态页面。它使用GET从URL获取请求的页面。
在此包含我有一个登录页面,当用户登录时我希望它重定向到帐户页面。然后,我必须影响包含之外的页面,但由于某种原因它不起作用。
在这种情况下,我使用$ _SESSION变量,如果它已登录,它将包含帐户页面,但它不起作用。我是否一直使用错误的方法来实现我想要实现的目标?
dashboard.php
<?php
session_start();
if($_SESSION['isLoggedIn'] = TRUE){
$page = "dashboard-parts/account";
};
?>
<html>
<head>
<title>Dashboard</title>
<link rel="stylesheet" type="text/css" href="css/dashboard-style.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js">
</script>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="https://fonts.googleapis.com/css?family=Nunito+Sans:300,400,600,700" rel="stylesheet">
<link rel="stylesheet" href="fonts/font-awesome/css/font-awesome.min.css">
</head>
<body>
<div id="wrapper">
<div id="top-bar">
<h1 id="logo">Logo</h1>
<ul>
<li class="social-link"><a href=""><i class="fa fa-linkedin" aria-hidden="true" ></i></a></li>
<li class="social-link"><a href=""><i class="fa fa-instagram" aria-hidden="true"></i></a></li>
<li class="social-link"><a href=""><i class="fa fa-pinterest" aria-hidden="true"></i></a></li>
<li class="social-link"><a href=""><i class="fa fa-twitter" aria-hidden="true"></i></a></li>
<li class="social-link"><a href=""><i class="fa fa-facebook-official" aria-hidden="true"></i></a></li>
</ul>
</div>
<div id="nav">
<ul>
<li><a href="dashboard.php?page=dashboard-parts/account" class="nav-item">Account</a></li>
<li><a href="dashboard.php?page=dashboard-parts/statistics" class="nav-item">Statistics</a></li>
<li><a href="dashboard.php?page=dashboard-parts/tasks" class="nav-item">Tasks</a></li>
<li><a href="dashboard.php?page=dashboard-parts/research" class="nav-item">Research</a></li>
<li><a href="dashboard.php?page=dashboard-parts/inbox" class="nav-item">Inbox</a></li>
<li><a href="dashboard.php?page=dashboard-parts/support" class="nav-item">Support</a></li>
<li><a href="dashboard.php?page=dashboard-parts/upgrade" id="upgrade" class="nav-item">Upgrade</a></li>
</ul>
</div>
<div id="content">
<?php
$page = $_GET['page'];
$pages = array('dashboard-parts/account',
'dashboard-parts/statistics',
'dashboard-parts/tasks',
'dashboard-parts/research',
'dashboard-parts/inbox',
'dashboard-parts/support',
'dashboard-parts/login',
'dashboard-parts/reg',
'dashboard-parts/upgrade');
if (!empty($page)) {
if(in_array($page,$pages)) {
$page .= '.php';
include($page);
}
else {
echo 'Page not found. Return to the
<a href="dashboard-parts/login.php">login page.</a>';
}
}
else {
include('dashboard-parts/login.php');
}
?>
</div>
</div>
</body>
</html>
的login.php:
<html>
<head>
<title>Dashboard</title>
<link rel="stylesheet" type="text/css" href="css/login-style.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href="https://fonts.googleapis.com/css?family=Nunito+Sans:300,400,600,700" rel="stylesheet">
<link rel="stylesheet" href="fonts/font-awesome/css/font-awesome.min.css">
</head>
<body>
<div class="wrapper">
<div id="login-box">
<form method="post" action="">
<div>
<input placeholder="Username" type="text" name="username" id="username">
</div>
<div>
<input placeholder="Password" type="password" name="password" id="password">
</div>
<input class="submit" type="submit" name="login_submit" value="Log In">
</form>
<a href="dashboard.php?page=dashboard-parts/reg">Don't have an account? Click here!</a>
</div>
</div>
</body>
</html>
<?php
include_once ('dbstuff.inc');
session_start();
echo $_SESSION['isLoggedIn'];
if(isset($_POST['login_submit'])){
$errors = array();
//Basic validation
if(empty($_POST['username'])){
$errors[] = "Please enter your username";
}else{
$username = $conn->real_escape_string($_POST['username']);
}
if(empty($_POST['password'])){
$errors[] = "Please enter your password";
}else{
$password = trim($_POST['password']);
}
if (empty($errors)) {
$sql = "SELECT * FROM users WHERE username = '$username'";
$result = $conn->query($sql);
if ($result->num_rows === 1) {
$row = $result->fetch_array(MYSQLI_ASSOC);
if (password_verify($password, $row['password'])) {
//Password matches, so create the session and send to dashboard
$_SESSION['user']['user_id'] = $row['user_id'];
$_SESSION['isLoggedIn'] = True;
//Sets the user to logged in
echo $_SESSION['isLoggedIn'];
exit;
}else{
$errors[] = "The username or password do not match";
}
}else{
$errors[] = "The username or password do not match";
}
}
}
?>
答案 0 :(得分:0)
看来,当您第一次设置$page = "dashboard-parts/account";
时,它没有任何区别,因为您稍后再次设置它$page = $_GET['page'];
答案 1 :(得分:0)
首先,您没有检查,如果用户已登录,但您正在登录他。
if($_SESSION['isLoggedIn'] = TRUE){
$page = "dashboard-parts/account";
}
您有=
(已分配)而不是==
(比较)。