What are some best practices for structuring PHP websites, especially in terms of user authentication and template usage?

Issue: When structuring PHP websites, it is important to properly handle user authentication to ensure secure access to protected areas of the site. Additionally, using templates can help streamline the design and maintenance of the website. Solution: To properly handle user authentication in PHP, you can create a login system that verifies user credentials against a database and sets a session variable upon successful login. Here is an example code snippet for a basic login system:

```php
<?php
session_start();

// Check if the user is already logged in
if(isset($_SESSION['user_id'])){
    // Redirect to the dashboard or home page
    header("Location: dashboard.php");
    exit;
}

// Check if the form is submitted
if($_SERVER['REQUEST_METHOD'] == 'POST'){
    // Validate user credentials
    $username = $_POST['username'];
    $password = $_POST['password'];

    // Query the database to check if the user exists
    // and the password is correct
    // You can use prepared statements to prevent SQL injection

    // If user authentication is successful, set session variable
    $_SESSION['user_id'] = $user_id;

    // Redirect to the dashboard or home page
    header("Location: dashboard.php");
    exit;
}
?>

<!DOCTYPE html>
<html>
<head>
    <title>Login</title>
</head>
<body>
    <h1>Login</h1>
    <form method="post" action="login.php">
        <label for="username">Username:</label>
        <input type="text" name="username" id="username" required><br>
        <label for="password">Password:</label>
        <input type="password" name="password" id="password" required><br>
        <input type="submit" value="Login">
    </form>
</body>
</html>
``` 

To use templates in PHP, you can create separate files for header, footer, and other reusable components, and then include them in your main PHP files using `include` or `require`. Here is an example code snippet for using templates:

```php
<?php
// header.php
?>
<!DOCTYPE html>
<html>
<head>
    <title>My Website</title>
</head>
<body>
    <header>
        <h1>Welcome to My Website</h1>
    </header>
    
<?php
// main.php
include 'header.php';
?>
    <main>