use this code
index.php
<!DOCTYPE html>
<html>
<body>
<h2>HTML Forms</h2>
<form action="login.php" method="post">
<label for="fname">First name:</label><br>
<input type="text" id="fname" name="username" value="John"><br>
<label for="lname">Last name:</label><br>
<input type="password" id="lname" name="password" value="Doe"><br><br>
<input type="submit" value="Submit">
</form>
<p>If you click the "Submit" button, the form-data will be sent to a page called "/action_page.php".</p>
</body>
</html>
login.php
<?php
$servername = "localhost";
$username = "root";
$password = "";
$database = "mydb";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $database);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully";
$username_err = $_POST['username'];
$password_err= $_POST['password'];
$sql = "SELECT * FROM users where username="$username_err"";
$result = $conn->query($sql);
// output data of each row
while($row = $result->fetch_assoc()) {
session_start();
// Store data in session variables
$_SESSION["loggedin"] = true;
$_SESSION["id"] = $row['userId'];
$_SESSION["username"] = $row['username'];
$_SESSION["name"] = $row['name'];
// Redirect user to welcome page
header("location: welcome.php");
}
$conn->close();
?>
welcome.php
<?php
session_start();
?>
<h1 class="my-5">Hi, <b><?php echo htmlspecialchars($_SESSION["name"]); ?></b>. Welcome to our site.</h1>
<p>
<a href="reset-password.php" class="btn btn-warning">Reset Your Password</a>
<a href="logout.php" class="btn btn-danger ml-3">Sign Out of Your Account</a>
</p>
</p>
note:- you have to replace database name ‘mydb’ to your database name.
8
solved Session Full Name instead of Username in PHP Login Form [closed]