简体   繁体   English

用户登录时在顶部显示用户名

[英]Show the username in the top when the user is login

I know this question look really simple for u guys but i cant find a tutorial or topic about this. 我知道这个问题对你们来说看起来很简单,但是我找不到关于这个的教程或主题。 I been trying and searching for days but no luck. 我一直在努力寻找,但没有运气。 What i want to do is when the user login, their name will be display at the top in the menu bar in every page. 我想要做的是当用户登录时,他们的名字将显示在每个页面的菜单栏的顶部。 Please, Please, Please any help or suggestion will be appreciated. 请,请,任何帮助或建议将不胜感激。

This is the image when there is no login user 这是没有登录用户时的图像

And this when a user is login. 这是用户登录时的情况。 the login/register button should be hide then display the email and logout button 登录/注册按钮应该隐藏然后显示电子邮件和注销按钮

Here is the code i made. 这是我制作的代码。 I try javascript and session but i cant do it properly. 我尝试javascript和会话,但我不能正确地做到这一点。

 <?php error_reporting(E_ALL & ~E_NOTICE); session_start(); if($_POST['LogIn']) { $servername = "localhost"; $username = "root"; $password = ""; $dbname = "dbuseraccounts"; // Create connection $conn = new mysqli($servername, $username, $password, $dbname); $email = strip_tags($_POST['Email']); $password = strip_tags($_POST['Password']); $sql = "SELECT UserID, Email, Password FROM users WHERE Email = '$email'"; $query = mysqli_query($conn, $sql); if($query) { $row = mysqli_fetch_row($query); $userid = $row[0]; $dbEmail = $row[1]; $dbPassword = $row[2]; } if($email == $dbEmail && $password == $dbPassword) { $_SESSION['Email'] = $email; $_SESSION['id'] = $userid; } else { $message = "Incorect username or password"; echo "<script type='text/javascript'>alert('$message');</script>"; } } ?> <!doctype html> <html> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Login</title> <link href="css/bootstrap.css" rel="stylesheet" type="text/css"> <link href="css/stylesheet.css" rel="stylesheet" type="text/css"> <style type="text/css"> body { margin-left: 0px; margin-right: 0px; } </style> <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries --> <!-- WARNING: Respond.js doesn't work if you view the page via file:// --> <!--[if lt IE 9]> <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script> <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script> <![endif]--> </head> <body> <div class="wrapper"> <div class="container-fluid"> <div class="header"> <nav class="navbar navbar-default navbar-fixed-top"> <!-- Brand and toggle get grouped for better mobile display --> <div class="navbar-header"> <button type="button" class="navbar-toggle collapsed" id="Register" aria-expanded="false" data-toggle="collapse" data-target="#topFixedNavbar1"><span class="sr-only">Toggle navigation</span><span class="icon-bar"></span><span class="icon-bar"></span><span class="icon-bar"></span></button> <a class="navbar-brand" href="index.html">Neo</a></div> <!-- Collect the nav links, forms, and other content for toggling --> <div class="collapse navbar-collapse" id="topFixedNavbar1"> <ul class="nav navbar-nav"> <li class="active"></li> <li></li> </ul> <form class="navbar-form navbar-left" role="search"> <div class="form-group"> </div> </form> <ul class="nav navbar-nav navbar-right"> <li><a href="Home.php">Home</a></li> <li><a href="Services.html">Services</a></li> <li><a href="Promo.html">Promo</a></li> <li><a href="Promo.html">About Us</a></li> <li><a href="ContactUs.html">Contact Us</a></li> <li><a href="login.php"><?php echo $_SESSION["Email"]?></a></li> <li><a id="logout">Logout</li> <li class="dropdown"> <ul class="dropdown-menu"> <li><a href="#">Action</a></li> <li><a href="#">Another action</a></li> <li><a href="#">Something else here</a></li> <li role="separator" class="divider"></li> <li><a href="#">Separated link</a></li> </ul> </li> </ul> </div> <!-- /.navbar-collapse --> </div> <!-- /.container-fluid --> </nav> </div> <div class="content"> <h1 id="h1Contactus">Login</h1> <form id="RegisterForm" name="RegisterForm" method="post"> <div class="FormElement"><input name="Email" type="email" autofocus required="required" class="TField" id="Email" placeholder="Email Address"></div> <div class="FormElement"><input name="Password" type="password" required="required" class="TField" id="Password" placeholder="Password"></div> <div class="FormElement"> <input name="LogIn" type="submit" class="button" id="LogIn" value="Log In"> </div> </form> </div> <div class="footerStick"> <footer>© 2015 Neo</footer> </div> </div> </div> <script src="js/jquery-1.11.3.min.js"></script> <script src="js/bootstrap.js"></script> </body> </html> 

You're on the right track. 你走在正确的轨道上。 Essentially how it works is this: 基本上它是如何工作的是这样的:

You have an HTML form. 你有一个HTML表单。 The html form contains the username/email and password to login. html表单包含用于登录的用户名/电子邮件和密码。 The html form submits(POST's) to the php login script. html表单提交(POST)到php登录脚本。 In the php login script you create a session on successful login. 在php登录脚本中,您可以在成功登录时创建会话。 In the Session you store the username, in php you can then echo the data from the session to display the username. 在会话中存储用户名,在php中,您可以回显会话中的数据以显示用户名。

So don't copy a tutorial, real about php sessions! 所以不要复制教程,真正关于php会话!

http://www.w3schools.com/php/php_sessions.asp http://www.w3schools.com/php/php_sessions.asp

http://php.net/manual/en/book.session.php http://php.net/manual/en/book.session.php

This is NOT secure, this is just a tutorial. 这不安全,这只是一个教程。

Essentially this is fairly easy if you got the right idea! 基本上,如果你有正确的想法,这是相当容易的!

How to achieve... 如何实现......

  • First when the user successfully logs in create a $_SESSION[] var. 首先,当用户成功登录时创建$ _SESSION [] var。 $_SESSION[] vars are used on almost every website that runs on PHP, you can start a plain session by adding the code below to the top of your page. 几乎每个在PHP上运行的网站都使用$ _SESSION []变量,您可以通过将以下代码添加到页面顶部来启动简单会话。

     session_start(); 
  • Second when your code says the user login is successful create a session var as the code is shown below. 第二,当您的代码表示用户登录成功时,创建会话var,因为代码如下所示。

     $_SESSION['loggedIn'] = true; 
  • Third you need to create some sort of code that checks to see if the user is logged in, an example code is shown below. 第三,您需要创建某种代码来检查用户是否已登录,示例代码如下所示。

     if (isset($_SESSION['loggedIn'])) { // code to execute if the user is logged in } else { // code to execute if the user is not logged in } 
  • And last of all when you log out you need to destroy the session and delete all $_SESSION[] vars by using the code below. 最后,当您注销时,您需要使用下面的代码销毁会话并删除所有$ _SESSION []变量。

     $_SESSION = array(); session_destroy(); 

Execute HTML Code based on the if statement : 基于if语句执行HTML代码

<?php
if (isset($_SESSION['loggedIn'])) {
?> 
<p>Logged In!</p>
<?php
} else {
?>
<p>Logged Out!</p>
<?php
}
?>

If you want to display the username, you need to create a $_SESSION[] var called username and use the echo the $_SESSION[] var. 如果要显示用户名,则需要创建名为username的$ _SESSION [] var并使用echo $ _SESSION [] var。

// To Echo
<?= $_SESSION['username']; ?>

// To Create
<?php $_SESSION['username'] = 'someusername'; ?>

Here are a couple resources that might help you security wise, i just looked at your code and it's simple but not really secured. 这里有一些资源可以帮助你明智地安全,我只是查看你的代码,它很简单但不是真正安全。

http://php.net/manual/en/session.security.php http://php.net/manual/en/session.security.php

http://php.net/manual/en/mysqli.quickstart.prepared-statements.php http://php.net/manual/en/mysqli.quickstart.prepared-statements.php

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 用户使用cakephp登录时如何显示用户名? - how to show username when user is logged in with cakephp? 如何在任何用户登录而不使用 signalr 单击按钮时获取实时用户名? - How to get real time username when any user login without clicking on button using signalr? 在密码或用户名错误时在登录时添加Toast - Adding toast in login when password or username is wrong 用户登录模拟器,js,验证用户名和密码,失败 - user login simulator,js,validating the username&password,failed 当用户滚动到页面顶部时,如何才能使此代码仅显示div标签? - How can I go about making this code only show the div tag when the user scrolls to the top of the page? 用户成功注册并将其重定向到“登录”页面后,如何显示消息框? - How can I show messagebox when user register successfully and redirect him to Login page? 如果用户在使用 react-navigation 打开应用程序时未登录,则显示登录屏幕 - Show login screen if user is not logged in when opening the app with react-navigation 仅当用户未登录时才显示登录屏幕 - Show login screen only if user is not logged in 如何显示哪个用户登录到我的HTML - How to show which user is login on into my HTML 显示登录屏幕如果用户未登录| jQuery的 - Show login screen If the user is not logged in | jQuery
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM