2016-01-28 168 views
1

我想要的「登錄/註冊」按鈕,在導航欄在用戶登錄後更改爲「我的賬戶」。導航欄

的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"); 
} 
?> 

如果我去一個網頁,這個導航欄我得到以下錯誤:

This webpage has a redirect loop ERR_TOO_MANY_REDIRECTS

有人能幫我嗎?

回答

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']; 
} 
0

刪除check.php中的位置標題。您可以顯示登錄表單後設置的位置標頭

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; 
} 

} 

?>