Skip to content
PHP Logo

What potential issues can arise when using IP-based access control in PHP scripts for an Intranet site?

One potential issue with IP-based access control in PHP scripts for an Intranet site is that IP addresses can be easily spoofed or changed, leading to unauthorized access. To mitigate this risk, you can combine IP-based access control with additional authentication methods such as username and password.

php
// Example code snippet combining IP-based access control with username and password authentication
$allowed_ips = array('192.168.1.100', '192.168.1.101');
$allowed_users = array('admin' => 'password123');

$remote_ip = $_SERVER['REMOTE_ADDR'];
$user = isset($_POST['user']) ? $_POST['user'] : '';
$password = isset($_POST['password']) ? $_POST['password'] : '';

if (in_array($remote_ip, $allowed_ips) && isset($allowed_users[$user]) && $allowed_users[$user] == $password) {
    // User is authorized
    echo 'Access granted';
} else {
    // User is not authorized
    echo 'Access denied';
}