如果用户已登录,请更改导航栏

时间:2019-05-25 16:32:59

标签: php html mysql session navigation

我希望用户登录后将导航栏中的“登录/注册”按钮更改为“我的帐户”。我有一个问题,因为我的index.html页面不显示导航栏。

index.html页面:

<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
      <meta charset="utf-8" />
      <title>Index Page</title>
</head>

<body>

<?php include("navigation.php"); ?>

</body>
</html>

navigation.php

<?php 
include("check.php");   
?>


<?php 
if ($loginst == 1){ ?>
    <div id="nav">
        <ul >               
            <li class="navbar-left"><a href="page2.html">Page 2</a></li>
            <li class="navbar-left"><a href="page3.html">Page 3</a></li>
            <li class="navbar-right"><a href="account.php">My Account</a></li>
            <li class="navbar-right"><a href="logout.php">Sign Out</a</li>
        </ul>
    </div>
<?php } else { ?>
    <div id="nav">
        <ul >               
            <li class="navbar-left"><a href="page2.html">Page 2</a></li>
            <li class="navbar-left"><a href="page3.html">Page 3</a></li>
            <li class="navbar-right"><a href="login.php">Login</a</li>
        </ul>
    </div> 
<?php } ?>

check.php

<?php
include('connection.php');
session_start();
$loginst = 0;
if ($_SESSION['username']){ 

$user_check = $_SESSION['username'];

$ses_sql = mysqli_query($db,"SELECT username FROM users WHERE username='$user_check' ");

$row=mysqli_fetch_array($ses_sql,MYSQLI_ASSOC);

$login_user=$row['username'];

if(!empty($login_user)) 
{
   $loginst = 1;
}   
}

?>

如果我转到index.html,则不会显示导航栏。我究竟做错了什么?谢谢。

1 个答案:

答案 0 :(得分:2)

因为您的文件格式是html,但不支持php标签:

<?php include("navigation.php"); ?>

将index.html重命名为index.php以支持php include。