How can PHP be used to restrict access to a website based on IP address?

To restrict access to a website based on IP address using PHP, you can check the visitor's IP address against a list of allowed or denied IP addresses. If the visitor's IP address matches an allowed IP address, they are granted access to the website. If the visitor's IP address matches a denied IP address, they are blocked from accessing the website.

$allowed_ips = array('192.168.1.1', '10.0.0.1'); // List of allowed IP addresses

$visitor_ip = $_SERVER['REMOTE_ADDR']; // Get visitor's IP address

if (!in_array($visitor_ip, $allowed_ips)) {
    // IP address not allowed, block access
    header('HTTP/1.0 403 Forbidden');
    die('Access Forbidden');
}