What are the best practices for implementing user authentication and access control in PHP instead of relying on IP addresses?

When implementing user authentication and access control in PHP, it is best to avoid relying solely on IP addresses for security. Instead, use a combination of secure password hashing, session management, and role-based access control to ensure proper authentication and authorization of users.

// Start a session
session_start();

// Check if user is logged in
if (!isset($_SESSION['user_id'])) {
    header('Location: login.php');
    exit();
}

// Check user roles for access control
$allowed_roles = ['admin', 'editor'];
if (!in_array($_SESSION['role'], $allowed_roles)) {
    die('You do not have permission to access this page.');
}