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');
}
Keywords
Related Questions
- When redirecting to another page in PHP, what considerations should be made regarding the placement of header functions and HTML content output?
- In PHP, what are some best practices for efficiently reading and storing data from a file into an array?
- In terms of PHP development, what advantages does ATOM's integration with GitHub offer compared to other IDEs?