Are there alternative methods to protect PHP pages from external access if .htaccess files cannot be used?
If .htaccess files cannot be used to protect PHP pages from external access, an alternative method is to implement access control directly in the PHP code. This can be done by checking the IP address of the client making the request and only allowing access if it matches a predefined list of allowed IP addresses.
<?php
$allowed_ips = array('192.168.1.1', '10.0.0.1'); // List of allowed IP addresses
$client_ip = $_SERVER['REMOTE_ADDR']; // Get the IP address of the client
if (!in_array($client_ip, $allowed_ips)) {
header('HTTP/1.1 403 Forbidden');
echo 'Access forbidden';
exit;
}
// Rest of the PHP code for the protected page
?>