gpt4 book ai didi

php - 登录后如何在 Bootstrap 导航栏中显示登录用户名

转载 作者:太空宇宙 更新时间:2023-11-04 10:01:40 25 4
gpt4 key购买 nike

我的导航栏上有登录按钮,它是打开登录模式。我想用登录该模式的用户名更改登录按钮。

我是 php 和 mysqli 的新手。一星期,我就在这里。请给我解释说明我的问题,否则我无法理解:)

这是我的代码;

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>

最佳答案

如果其他一切都解决了,通常这样的事情对你有用

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

或者删除 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 ?>

如果您在理解这段代码时遇到任何问题,请告诉我

关于php - 登录后如何在 Bootstrap 导航栏中显示登录用户名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38295151/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com