如果用户已登录,我希望导航栏中显示用户名,如下所示:
https://i.stack.imgur.com/uRP4R.png
我有一个login.php页面引用(成功登录)到我的Loggedin.php。在此Loggedin页面上,我开始一个新会话并输入
($_SESSION['username']) echo $username;
使用html看起来像这样:
<a class="right" href="register.php"><span class="glyphicon glyphicon-user"></span> <?php ($_SESSION['username']) echo $username; ?></a>
我知道我可能不会从登录页面获取会话但我无法找到解决方案。
了解更多信息:
的login.php:
<?php
session_start();
require('../includes/connect_to_db.php');
if (isset($_POST['username']) and isset($_POST['password'])){
$username = $_POST['username'];
$password = $_POST['password'];
$query = "SELECT * FROM `user` WHERE username='$username' and password='$password'";
$result = mysqli_query($connection, $query) or die(mysqli_error($connection));
$count = mysqli_num_rows($result);
if ($count == 1){
$_SESSION['username'] = $username;
}else{
$fmsg = "Invalid Login Credentials.";
}
}
if (isset($_SESSION['username'])){
header("Location: Loggedin.php");
$username = $_SESSION['username'];
echo "<a href='logout.php'>Logout</a>";
}else{
}
?>
以下是我在loggedin.php页面上尝试的内容:
<?php
//Start Session
session_start();
//Connect to db
require('../includes/connect_to_db.php');
//echo the username
echo $_SESSION['username'];
?>
和echo我使用已经显示的html / php代码实现到导航栏:
<a class="right" href="register.php"><span class="glyphicon glyphicon-user"></span> <?php ($_SESSION['username']) echo $username; ?></a>
答案 0 :(得分:-1)
如果用户名存储在会话中,则应输出该值:
echo $_SESSION['username'];