Show username at the top when user logs in

I know this question looks very simple for the guys, but I can’t find a tutorial or topic about it. I tried and looked for days, but no luck. What I want to do is when the user login, his name will be displayed at the top of the menu bar on each page. Please, please, please, any help or suggestion will be appreciated.

This image when the user login does not exist

And this is when the user is login. the login / registration button should be hidden and then display the email and logout button

Here is the code I made. I try javascript and session, but I can not do it right.

<?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> 
+5
source share
2 answers

This is NOT safe, this is just a tutorial.

Essentially, it's pretty easy if you got the right idea!

How to reach ...

  • First, when the user logs in successfully, create $ _SESSION [] var. $ _SESSION [] vars are used on almost every website that runs on PHP, you can start a simple session by adding the code below at the top of the page.

     session_start(); 
  • Secondly, when your code says that the user login has been successfully created, the var session, as shown below, is the code.

     $_SESSION['loggedIn'] = true; 
  • Thirdly, you need to create some kind of code that checks if the user is logged in, a code example 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 finally, when you log out, you need to destroy the session and delete all $ _SESSION [] vars using the following code.

     $_SESSION = array(); session_destroy(); 

Execute HTML based on if statement:

 <?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 the username $ _SESSION [] var and use the echo file $ _SESSION [] var.

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

Here are some resources that can help you in security, I just looked at your code and is simple, but not very secure.

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

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

+1
source

You are on the right track. Essentially how it works:

You have an HTML form. The html form contains the username / email address and password for logging in. The html form submits (POST) to the php login script. In php login script, you create a session on successful login. In the session you save the username, in php you can then echo the data from the session to display the username.

So do not copy the tutorial, really about php sessions!

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

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

+1
source

All Articles