用户登录php后更改导航栏

时间:2016-01-28 13:42:13

标签: php session login navigation webshop

我希望导航栏中的“登录/注册”按钮在用户登录后更改为“我的帐户”。

我目前使用的navigation.php文件如下所示:

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


<?php 

if ($_SESSION['username']){ ?>
    <div id="nav">
        <ul >
            <li class="navbar-left"><a href="artikelen.html">Men</a></li>
            <li class="navbar-left"><a href="artikelen.html">Women</a></li>
            <li class="navbar-left"><a href="artikelen.html">Kids</a></li>
            <li class="navbar-right"><a href="view_cart.php">Cart (0)</a></li>
            <li class="navbar-right"><a href="inlog.php">My Account</a></li>
        </ul>
    </div>

<?php } else { ?>
    <div id="nav">
        <ul >
            <li class="navbar-left"><a href="artikelen.html">Men</a></li>
            <li class="navbar-left"><a href="artikelen.html">Women</a></li>
            <li class="navbar-left"><a href="artikelen.html">Kids</a></li>
            <li class="navbar-right"><a href="view_cart.php">Cart (0)</a></li>
            <li class="navbar-right"><a href="inlog.php">Login / Sign Up</a></li>
        </ul>
    </div> 
<?php }
?>

check.php文件如下所示:

<?php
include('connection.php');
session_start();
$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(!isset($user_check))
{
header("Location: index.php");
}
?>

如果我使用此导航栏转到网页,则会收到以下错误:

  

此网页有一个重定向循环ERR_TOO_MANY_REDIRECTS

有人可以帮帮我吗?

3 个答案:

答案 0 :(得分:0)

将check.php更新为此类内容。

$user_check = (isset($_SESSION['username']) && trim($_SESSION['username'])!='')?trim($_SESSION['username']):false;
if(!$user_check) header("Location: index.php");
else{
    $ses_sql = mysqli_query($db,"SELECT username FROM users WHERE username='$user_check' ");
    $row=mysqli_fetch_array($ses_sql,MYSQLI_ASSOC); 
    $$_SESSION['username']=$row['username'];
}

答案 1 :(得分:0)

删除check.php中的Location标头。您可以在显示登录表单后设置位置标题

答案 2 :(得分:0)

试试这个

navigation.php

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


    <?php 

    if ($loginst == 1){ ?>
        <div id="nav">
            <ul >
                <li class="navbar-left"><a href="artikelen.html">Men</a></li>
                <li class="navbar-left"><a href="artikelen.html">Women</a></li>
                <li class="navbar-left"><a href="artikelen.html">Kids</a></li>
                <li class="navbar-right"><a href="view_cart.php">Cart (0)</a></li>
                <li class="navbar-right"><a href="inlog.php">My Account</a></li>
            </ul>
        </div>

    <?php } else { ?>
        <div id="nav">
            <ul >
                <li class="navbar-left"><a href="artikelen.html">Men</a></li>
                <li class="navbar-left"><a href="artikelen.html">Women</a></li>
                <li class="navbar-left"><a href="artikelen.html">Kids</a></li>
                <li class="navbar-right"><a href="view_cart.php">Cart (0)</a></li>
                <li class="navbar-right"><a href="inlog.php">Login / Sign Up</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;
}

}

?>