简体   繁体   English

从包含内部更改包含

[英]Change include from inside an include

I have a dashboard which uses includes to make it a static page with changeable includes.我有一个仪表板,它使用包含使其成为具有可变包含的静态页面。 It uses GET to get the requested page from the URL.它使用 GET 从 URL 获取请求的页面。

In this include I have a login page, when the user logs in I want it to redirect to the account page.在这包括我有一个登录页面,当用户登录时,我希望它重定向到帐户页面。 I then have to affect the page outside of the include but for some reason it does not work.然后我必须影响包含之外的页面,但由于某种原因它不起作用。

In this case I use a $_SESSION variable which if it is logged in it will instead include the account page but it does not work.在这种情况下,我使用 $_SESSION 变量,如果它已登录,它将包含帐户页面,但它不起作用。 Am I using the wrong method all together to what I'm trying to achieve?我是否正在使用错误的方法来实现我想要实现的目标?

dashboard.php仪表盘.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:登录.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";
    }
}
}

?>

It seems that when you first set $page = "dashboard-parts/account";似乎当你第一次设置$page = "dashboard-parts/account"; , it doesn't make any difference because you set it again later $page = $_GET['page']; ,它没有任何区别,因为你稍后再设置它$page = $_GET['page'];

At first, you are not checking, if user is logged in, but you are logging him in.起初,您不是在检查用户是否已登录,而是在让他登录。

 if($_SESSION['isLoggedIn'] = TRUE){
    $page = "dashboard-parts/account";
}

You have = (assign) instead of == (comparision).你有= (分配)而不是== (比较)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM