What are the potential challenges of implementing a unified login system for a website and forum in PHP?

One potential challenge of implementing a unified login system for a website and forum in PHP is ensuring that user credentials are securely shared and validated across both platforms. One solution to this issue is to use a centralized database to store user information and authenticate users based on this shared database.

// Connect to the centralized database
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "central_db";

$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

// Validate user credentials
$username = $_POST['username'];
$password = $_POST['password'];

$sql = "SELECT * FROM users WHERE username='$username' AND password='$password'";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // User authenticated, set session variables and redirect to website/forum
    $_SESSION['username'] = $username;
    header("Location: website.php");
} else {
    // Invalid credentials, display error message
    echo "Invalid username or password";
}

$conn->close();