PHP code example of nigel / ip

1. Go to this page and download the library: Download nigel/ip library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

nigel / ip example snippets





use Nigel\Ip\IpAddress;

on/json');

//Initialize IpAddress
$ipService = new IpAddress();

// Define your IP address ranges
$allowedRanges = [
    '127.0.0.1/18',
];

// Get the visitor's IP address
$visitorIp = $ipService->getIp();

// Validate IP address format
if ($ipService->validateIPAddress($visitorIp) === false) {
    echo json_encode([
        'status' => 'error',
        'message' => 'Invalid Ip Address'
    ]);
}

// Check if the visitor's IP address is within any of the allowed ranges
if ($ipService->checkVisitorIpInRange($allowedRanges, $visitorIp)) {
    echo json_encode([
        'status' => 'success',
        'message' => 'Valid IP Address'
    ]);
} else {
    echo json_encode([
        'status' => 'error',
        'message' => 'Invalid IP Address'
    ]);
}