I've made a login script for my website and need some feedback as to whether it's secure or not and how I can improve it:
<?php
session_start();
$user = "root";
$host = "localhost";
$pass = "";
$db = "test_db";
$cxn = mysqli_connect($host, $user, $pass, $db) or die ("Couldn't connect to the server. Please try again.");
if(isset($_POST['submit'])) {
$username = mysql_real_escape_string(strip_tags(trim($_POST['username'])));
$password = mysql_real_escape_string(strip_tags(trim($_POST['password'])));
$message = "";
$stmt = $cxn->prepare('SELECT * FROM users WHERE username = ?');
$stmt->bind_param('s', $username);
$stmt->execute();
$result = $stmt->get_result();
while ($row = $result->fetch_assoc()) {
if(password_verify($password, $row['password'])) {
$_SESSION['username'] = $username;
$_SESSION['password'] = $password;
header("location:index.php");
} else {
$message = "The username or password is incorrect.";
}
}
}
?>
Also, I'm just learning about sessions and have a few questions:
- After the user logs in successfully, I need their username to show up on any other page. How can I make the sessions secure?
- How do I make the "log out" feature end the session?