Hello guys, I’m quite a beginner in php and I’ve been trying to deal with this for over a week now but I just don’t know what to do to make this work. I created a simple login system on my website (login.php), containing variables username and password. This works pretty fine but I got another page called advisory (advisory.php) and I want the username from login.php to be displayed in the form in advisory.php (instead of the ‘Anonymous’ value that I temporarily put there) when the user is logged in, as well. And I also would like to have the username displayed through all of the pages of the website. Do you please have any advice how to do that? I have been trying to figure this out with echo "Hello {$_SESSION['account']}";
or echo "Welcome " . $_SESSION['account'] . "!";
or $_SESSION['username']
but no success. I actually don’t know how to make this work, since I’ve set $_SESSION['account'] = $row['id'];
but for login.php this works: echo 'You're logged in as ' . $row['username'];
but I don’t know how to display the username in the advisory.php and in all the pages of the website. Nothing works so far. Thank you all a lot for any kind of advice!
This is my login.php
<?php
session_start();
?>
// html part
<?php
if(isset($_SESSION['account'])) {
$conn = mysqli_connect("localhost", "root", "", "eshop-tech");
$id = $_SESSION['account'];
$result = mysqli_query($conn, "SELECT * FROM users WHERE id= $id");
$row = mysqli_fetch_assoc($result);
echo 'You're logged in as ' . $row['username'];
echo '<div class="logout">
<h3>Logout</h3>
<form action="logout-sql.php" method="post">
<input type="submit" name="submit" value="Logout"><br>
</form>
</div>';
} else {
echo '<span class="login-page">
<h3>Log In here</h3><br>
<form id="inputs-page" action="login-sql.php" method="post">
<div class="form-inputs-page">
<input type="text" name="username" placeholder="username"><br>
<input type="password" name="password" placeholder="password"><br>
</div>
<div class="forget-page">
<a href="forgot.php">Forgot password?</a>
</div><br>
<input type="submit" id="send-button-page" name="submit" value="Submit"><br>
<div class="createacc-page">
<a href="signup.php">Sign Up</a>
</div>
</form>
</span>';
}
?>
This is login-sql.php
<?php
$conn = mysqli_connect("localhost", "root", "", "eshop-tech");
$username = $_POST['username'];
$password = $_POST['password'];
$sql = "SELECT * FROM users WHERE username = ?";
$stmt = mysqli_stmt_init($conn);
if (mysqli_stmt_prepare($stmt, $sql)) {
mysqli_stmt_bind_param($stmt, "s", $username);
mysqli_stmt_execute($stmt);
$result = mysqli_stmt_get_result($stmt);
if (mysqli_num_rows($result) > 0) {
while ($row = mysqli_fetch_assoc($result)) {
if (password_verify($password, $row['password'])) {
session_start();
$_SESSION['account'] = $row['id'];
header('Location: ' . $_SERVER['HTTP_REFERER']);
exit();
} else {
header("Location: login.php?error=invalidusernameorpassword");
exit();
}
}
} else {
header("Location: login.php?error=invalidusernameorpassword");
exit();
}
} else {
header("Location: login.php?error=sql");
exit();
}
?>
This is my form in advisory.php
<?php
session_start();
date_default_timezone_set('Europe/Berlin');
include "advisory-sql.php";
include "advisory-comments.php";
echo "<form method='POST' action='".setComments($conn)."'>
<input type='hidden' name='name' value='Anonymous'>
<input type='hidden' name='date' value='".date('Y-m-d | H:i:s')."'>
<textarea name='message' cols='30' rows='10'></textarea><br>
<input type='submit' name='submitComment' value='Submit'><br>
</form>";
getComments($conn);
?>
This is advisory-comments.php
<?php
function setComments($conn) {
if (isset($_POST['submitComment'])) { $name = $_POST['name'];
$date = $_POST['date'];
$message = $_POST['message'];
$sql = "INSERT INTO advisory (name, date, message) VALUES ('$name', '$date', '$message')";
$result = $conn->query($sql);
}
}
function getComments($conn) {
$sql = "SELECT * FROM advisory ORDER BY id DESC LIMIT 5";
$result = $conn->query($sql);
while ($row = mysqli_fetch_assoc($result)) {
echo "<div class='comment_box'><p>";
echo $row['name']."<br>";
echo $row['date']."<br>";
echo nl2br($row['message']);
echo"</p></div>";
}
}
?>