用户登录php后更改导航栏
Posted
技术标签:
【中文标题】用户登录php后更改导航栏【英文标题】:Change navbar after user login php 【发布时间】:2016-05-05 22:12:00 【问题描述】:我希望用户登录后导航栏中的“登录/注册”按钮更改为“我的帐户”。
我用于此的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
有人可以帮帮我吗?
【问题讨论】:
【参考方案1】:将您的 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'];
【讨论】:
【参考方案2】:删除 check.php 中的 Location 标头。您可以在显示登录表单后设置位置标题
【讨论】:
【参考方案3】:试试这个
导航.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 ?>
检查.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;
?>
【讨论】:
以上是关于用户登录php后更改导航栏的主要内容,如果未能解决你的问题,请参考以下文章
用户在 Vue.js 和 Firebase 中注册后更改导航栏的状态