2016-07-10 116 views
0

我有我的導航欄上的登錄按鈕,它的開放登錄模式。 我想用登錄該模式的用戶名更改登錄按鈕。如何在登錄後顯示登錄用戶名在bootstrap導航欄中

我在php和mysqli中使用newbee。一個星期,我在這裏。請給我解釋我的問題的方向,否則i'cant理解:)

這是我的代碼;

的index.php

<?php 
    include 'config/setup.php'; //database connection here 
    session_start(); 
    ?> 

<!doctype html> 
<html> 
    <head> 
      <!-- showing title dynamicly --> 
    <title><?php echo $page['title'].' | '.$site_title; ?></title> 

     <meta name="viewport" content="width=device-width, initial-scale=1"> 
     <link rel="shortcut icon" type="image/x-icon" href="img/favicon.png"> 

     <?php include "config/css.php"; ?> <!-- bootstrap css links --> 
     <?php include "config/js.php"; ?> <!-- jquery javascript links -->  
    </head>  
<body> 

<?php include (D_TEMPLATE.'/navigation.php'); ?> <!-- including navbar --> 

    <div class="container"> 
     <!-- body items will be here --> 
    </div> 

<?php include (D_TEMPLATE.'/footer.php'); ?>  <!-- including footer--> 

</body> 

</html> 

navigation.php

<?php include 'config/js.php'; ?> 

<!-- Navbar --> 
<nav class="navbar navbar-inverse"> 
    <div class="container">  
    <div class="navbar-header"><img src="img/brand.png"></div> 

     <ul class="nav navbar-nav"> 
     <?php nav_main($dbc, $pageid); ?> <!-- Im showing page links dynamicly in mysql database --> 
     </ul> 

     <form class="navbar-form navbar-left" role="search"> 
      <div class="form-group"> 
       <input type="text" class="form-control" placeholder="Search"> 
       <a href="#"> <i class="fa fa-search fa-lg" style="color:#FFFFFF;" aria-hidden="true"></i></a>    
      </div> 
     </form> 


<div class="navbar-form navbar-right"> 

<?php 

if(isset($_POST['login'])){ 
    require 'config/dbc.php'; 
    $username = $_POST['username']; 
    $password = $_POST['password']; 
    $result = mysqli_query($dbc, "SELECT * FROM users WHERE username = '$username' AND password = '$password'"); 
    if(mysqli_num_rows($result) == 1){ 
     $_SESSION['username'] = $username; 
     echo $username; 
    }else{ 
     echo "problem"; 
    } 
} 

?> 
    <!-- Modal Login Button, i'm asking to erase this and write $username when user logged in, php codes is here because of this. In this situation, writing username WITH buttons, i want to erase that buttons after user logged in, it has to be write only username after login. I've tried echo this buttons after }else{ code; but any buttons not shown before login.I'm trying trying trying --> 
    <button id="login" name="login" class="btn btn-primary"><i class="fa fa-sign-in" aria-hidden="true"></i> Login</button> 

    <!-- It will be register modal button, i will work on it later --> 
    <button id="register" name="register" class="btn btn-success"><i class="fa fa-user-plus" aria-hidden="true"></i> Register</button> 
    </div>  

</nav> 


    <!-- Here is my Login Modal --> 

<section id="modal" class="modal fade"> 
    <div class="modal-body"> 

     <form method="post" action="index.php"> 

     <div class="form-group"> 
      <h4><i class="fa fa-sign-in" aria-hidden="true"></i> Login to Your Account 
      <a href="#"><span id="close" class="glyphicon glyphicon-remove-circle pull-right"></span></a></h4> 
     </div>   

      <div class="form-group"> 
       <label for="username">Username</label> 
       <input type="text" name="username" class="form-control" id="username" placeholder="Enter username"> 
      </div> 

      <div class="form-group"> 
       <label for="password">Password</label> 
       <input type="password" name="password" class="form-control" id="password" placeholder="Enter password"> 
      </div> 

      <div class="checkbox"> 
       <label><input type="checkbox">Remember Me</label> 
       <button class="btn btn-primary pull-right" data-dismiss="modal">Close</button> 
       <button type="submit" name="login" class="btn btn-danger pull-right"><i class="fa fa-sign-in" aria-hidden="true"></i> Login</button><br><br> 
       <a href="#"><label>Forgot password?</label></a> 
      </div> 

     </form> 

    </div> 
</section> 


<!-- It will be my Register Modal --> 
<section id="modal_register" class="modal fade"> 
    <div class="modal-body"> 

     <form action="" method="post" role="form"> 

      Register Here 

      <button id="submit" class="btn btn-danger">Login</button> 

     </form> 

    </div> 
</section> 

回答

1

如果其他一切都整理出來,通常像這樣能爲您的工作

<button id="login" name="login" class="btn btn-primary"><i class="fa fa-sign-in" aria-hidden="true"></i> 
    <?php if (isset($_SESSION['username'])) : ?> 
     <?php echo $_SESSION['username'] ?> 
    <?php else: ?> 
     Login 
    <?php endif ?> 

</button> 

OR之後移動ID

<?php if (isset($_SESSION['username'])) : ?> 
     <button class="btn btn-primary"><i class="fa fa-sign-in" aria-hidden="true"></i> <?php echo $_SESSION['username'] ?> </button> 
    <?php else: ?> 
     <button id="login" name="login" class="btn btn-primary"><i class="fa fa-sign-in" aria-hidden="true"></i> Login</button> 
    <?php endif ?> 

讓我知道,如果你有理解這個代碼

+0

謝謝回答任何問題。但; 我使用 <如果PHP(isset($ _ SESSION [ '名'])):?> 前按鈕。按鈕後我使用<?php endif?> 當我刷新頁面;我的導航欄上沒有登錄按鈕,但是正在寫用戶名。我是否應該銷燬會話以查看登錄按鈕以登錄? p.s. :我不明白你如何使用else和(:)這個,elseif之後。我試圖弄清楚:) –

+0

是的,您需要銷燬會話或從瀏覽器中刪除cookie以再次查看按鈕。 並瞭解更多關於這個替代語法if():[read here](http://php.net/manual/en/control-structures.alternative-syntax.php) – codisfy

+0

現在意識到你需要顯示用戶名在按鈕內部,我已經更改了我的答案中的代碼,但是現在您需要更新您的類/ ID,以便點擊該按鈕時不顯示登錄彈出窗口等。 – codisfy

相關問題