根据登录状态显示不同的包含文件

时间:2014-06-25 03:26:09

标签: php html session

我试图为已登录但似乎遇到问题的用户提供不同的导航菜单。我当前正在设置会话,当用户成功进行身份验证时,该会话然后设置会话变量,以便我们知道用户是否已登录或注销。

如果他们已登录,他们应该会看到登录菜单,否则他们应该只看到已注销的菜单。出于某种原因,我无法通过检查用户是否已登录来实现此目的 - 不确定我做错了什么。

的index.php

<?php

require_once("inc/config.php");
require(ROOT_PATH . "inc/database.php");

session_start();

?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Property Rental</title>

    <!-- CSS -->
    <link href="css/bootstrap.min.css" rel="stylesheet">
    <link href="css/style.css" rel="stylesheet">
    <link href="css/responsive.css" rel="stylesheet">

    <!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
    <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
    <!--[if lt IE 9]>
      <script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
      <script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
</head>

<body>

    <header role="banner" id="top" class="navbar navbar-static-top bs-docs-nav">
        <div class="container">
            <div class="navbar-header">
                <button data-target=".bs-navbar-collapse" data-toggle="collapse" type="button" class="navbar-toggle">
                    <span class="sr-only">Toggle navigation</span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                    <span class="icon-bar"></span>
                </button>
            </div>
            <nav role="navigation" class="collapse navbar-collapse bs-navbar-collapse">
                <ul class="nav navbar-nav">
                    <li class="active">
                        <a href="../getting-started">Menu Item 1</a>
                    </li>
                    <li>
                        <a href="../css">Menu Item 2</a>
                    </li>
                    <li>
                        <a href="../components">Menu Item 3</a>
                    </li>
                    <li>
                        <a href="../javascript">Menu Item 4</a>
                    </li>
                    <li>
                        <a href="../customize">Menu Item 5</a>
                    </li>

                    <?php

                    if ( $_SESSION['loggedin'] == true) { 
                        include("inc/logged.php");
                    } else {
                        include("inc/loggedOut.php");
                    }

                    print_r($_SESSION);

                    ?>

                </ul>
            </nav>
        </div>
    </header>

    <div class="container">

        <h1>Hello, world!</h1>

    </div>

    <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
    <script src="/js/bootstrap.js"></script>
    <script src="/js/core.js"></script>

</body>
</html>

users.database.php

<?php 

$username = $_POST['username'];          
$password = $_POST['password'];

try {   

    $result = $db->prepare("SELECT username, pass FROM user_info WHERE username = :user AND BINARY pass = :pass");
    $result->bindParam(':user', $username);
    $result->bindParam(':pass', $password);
    $result->execute();
    $rows = $result->fetch(PDO::FETCH_NUM);
}

catch (Exception $e) {
    echo "Could not retrieve data from database";
    exit();
}

if ($password = $rows) {
    session_start();
    $_SESSION['username'] = $_POST['username'];
    $_SESSION['loggedin'] = true;
} else {
    if (isset($_POST['login'])) {
        echo "Username or password incorrect (passwords are case sensitive)";
    }
}

?>

logged.php

<li class="login-register pull-right">
<ul>
    <li class="login pull-left">
        <a href="#">My Account</a>
        <div class="login-box">
            <div class="login-box-inner">
                <a href="/logout.php">Logout</a>
            </div>
        </form>
    </div>
    </li>
</ul>
</li>

loggedOut.php

<li class="login-register pull-right">
<ul>
    <li class="login pull-left">
        <a href="#">Login</a>
        <a href="/logout.php">LOGOUT</a>
        <div class="login-box">
            <div class="login-box-inner">
                <div class="up-arrow"></div>
                <form role="form" method="post">
                    <div class="form-group">
                        <label>Username or Email</label>
                        <input type="text" name="username">
                    </div>
                    <div class="form-group">
                        <label>Password</label>
                        <input type="password" name="password">
                    </div>

                    <?php require(ROOT_PATH . "inc/users.database.php"); ?>

                    <div class="form-group">
                        <div class="row">
                            <div class="col-xs-6">
                                <label class="remember-me">
                                    <input type="checkbox" name="remember"> Remember me
                                </label>
                            </div>
                            <div class="col-xs-6">
                                <button type="submit" name="login" class="btn btn-small btn-primary pull-right">Login</button>
                            </div>
                        </div>
                    </div>
                </form>
            </div>
        </form>
    </div>
    </li>
    <span class="slash">/</span>
    <li class="pull-right">
        <a href="/register">Register</a>
    </li>
</ul>
</li>

1 个答案:

答案 0 :(得分:2)

$loggedin未在index.php文件中设置。

用以下内容替换你的if条件:

 if ($_SESSION['loggedin'] == true) {

您也可以使用具有完全相同含义的...

 if ( $_SESSION['loggedin'] ) {

使用上面的代码,您需要使用session_start();

在该页面上启动会话

如果您在用户登录时使用javascript返回历史记录,请将其更改为此...

<script>
    window.location.href = document.referrer;
</script>

注意:这只会返回历史记录中的一页。