OneBeginer OneBeginer - 2 months ago 9
HTML Question

How to print user name after login?

I want after somone is logging in to display the user name on another page page , also is this a good way for login ?



<?php

session_start();

$database =mysqli_connect("localhost", "root", "", "login");

if (isset($_POST['login_bn'])) {

$usrname = mysqli_real_escape_string($db, $_POST['usrname']);
$pas = mysqli_real_escape_string($db, $_POST['pas']);

$sql = "SELECT * FROM accounts WHERE usrname='$usrname' AND pas='$pas'";
$result = mysqli_query($database, $sql);

if (mysqli_num_rows($result) == 1) {

header("location: home.php");

}

?>





And i want to display the username here on the nav bar somwhere



<?php

session_start();

?>

<!DOCTYPE html>

<html>
<head>

</head>
<body>

<button onclick="'">Log Out </button>

<ul>
<!-- Here somehwere -->

<li><a href="main.html"> Home</a></li>


</ul>





sorry for newbie Question

Answer

Try this, should read this Session in php

set session in login page $_SESSION["usrname"] = $usrname; and get value in home.php

<?php    
    session_start();    
    $database =mysqli_connect("localhost", "root", "", "login");
    if (isset($_POST['login_bn'])) {    
        $usrname = mysqli_real_escape_string($db, $_POST['usrname']);
        $pas = mysqli_real_escape_string($db, $_POST['pas']);   
        $sql = "SELECT * FROM accounts WHERE usrname='$usrname' AND pas='$pas'";
        $result = mysqli_query($database, $sql);    
        if (mysqli_num_rows($result) == 1) {
            $_SESSION["usrname"] = $usrname;
            header("location: home.php");   
        }   
    ?>

home.php

<?php
session_start();
?>
<!DOCTYPE html>
<html>
<head>
</head>
<body>
  <button onclick="'">Log Out </button>
<ul>
<!-- Here somehwere -->
<?php  echo $_SESSION["usrname"]; ?>
<li><a href="main.html"> Home</a></li>
</ul>

i hope it will be helpful.