Hey guys! I finally got my login system work on most pages of my website. However, there is one page on the website called advisory (advisory.php). It’s basically supposed to be a forum. I wanted the ‘name’ input there to display username when the user is logged in or display ‘Anonymous’ when the user is logged out. However, I tried to set a function
function getUsername() {
return isset($_SESSION[‘username’]) ? $_SESSION[‘username’] : ‘Anonymous’;
}
and then simply put .getUsername(). in the ’ name’ input in the form (advisory.php). But this didn’t work out. So, I temporarily put .($_SESSION[‘username’]). in the ‘name’ field.
But it turns out once the user is logged in, the name in comments is displayed as username (as it’s supposed to be) but when the user logs out, the name keeps displaying there with each new comment, as if the user was still logged in.
Do you guys please have any idea how can I fix this? Many thanks.
advisory.php :
<?php session_start(); date_default_timezone_set('Europe/Berlin'); include "advisory-sql.php"; include "advisory-comments.php";
// some html stuff //
echo "<form method='POST' action='".setComments($conn)."'> <input type='hidden' name='name' value='".($_SESSION['username'])."'> <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); ?>
advisory-comments.php:
<?php function setComments($conn) { if (isset($_POST['submitComment'])) { $name = $_POST['name']; $date = $_POST['date']; $message = $_POST['message']; $_SESSION['name'] = $name; $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)) { // if(!isset($_SESSION['username']) || empty($_SESSION['username'])) { // echo "$_SESSION['name']"; // } else { // echo "$_SESSION['username']"; // } echo "<div class='comment_box'><p>"; echo $row['name']."<br>"; echo $row['date']."<br>"; echo nl2br($row['message']); echo"</p></div>"; }
}
function getUsername() { return isset($_SESSION['username']) ? $_SESSION['username'] : 'Anonymous';
}
?>
login.php:
<?php include('server.php') ?> <div class="form-inputs-page"> <input type="text" name="username" placeholder="username"><br> <input type="password" name="password" placeholder="password"><br> <div class="forget-page"> <a href="forgot.php">Forgot password?</a> </div><br> <button type="submit" class="sbmt_log" name="login_user">Odeslat</button> </div> <div class="createacc-page"> <a href="register.php"> Click here to register! </a> </div>
server.php:
<?php session_start(); $name = ""; $username = ""; $email = ""; $errors = array(); $_SESSION['success'] = ""; $db = mysqli_connect('localhost', 'root', '', 'registration'); if (isset($_POST['reg_user'])) { $name = mysqli_real_escape_string($db, $_POST['name']); $username = mysqli_real_escape_string($db, $_POST['username']); $email = mysqli_real_escape_string($db, $_POST['email']); $password_1 = mysqli_real_escape_string($db, $_POST['password_1']); $password_2 = mysqli_real_escape_string($db, $_POST['password_2']); if (empty($name)) { array_push($errors, "Your name is required"); } if (empty($username)) { array_push($errors, "Username is required"); } if (empty($email)) { array_push($errors, "Email is required"); } if (empty($password_1)) { array_push($errors, "Password is required"); } if ($password_1 != $password_2) { array_push($errors, "The two passwords do not match"); } if (count($errors) == 0) { $password = md5($password_1); $query = "INSERT INTO users (`name`, `username`, `email`, `password`) VALUES('$name', '$username', '$email', '$password')"; mysqli_query($db, $query); $_SESSION['username'] = $username; $_SESSION['success'] = "You have been successfully registered"; header('location: welcome.php'); exit(); }
}
// USER LOGIN // if (isset($_POST['login_user'])) { $username = mysqli_real_escape_string($db, $_POST['username']); $password = mysqli_real_escape_string($db, $_POST['password']); if (empty($username)) { array_push($errors, "Username is required"); } if (empty($password)) { array_push($errors, "Password is required"); } if (count($errors) == 0) { $password = md5($password); $query = "SELECT * FROM users WHERE username= '$username' AND password='$password'"; $results = mysqli_query($db, $query); if (mysqli_num_rows($results) == 1) { $_SESSION['username'] = $username; $_SESSION['success'] = "You have logged in!"; header('location: welcome.php'); exit(); } else { array_push($errors, "Username or password incorrect"); } }
}
?>