Hi all

i am tracing ip addresses of all the site visitor using $_SERVER; and its working fine, But i want to exclude all ip starting from 172.122.*.* . i want to put "if" condition but don't know how to track these first two figure.
can anyone please tell me how can i exclude ip addresses starting from 172.122.?

Many Thanks in advance.

Dani AI

Generated

Nice quick fix from — a simple string match will do the job for many cases and clearly solved s problem. For production code, consider a slightly more robust approach that treats the IP as a numeric value instead of a substring. That avoids mistakes with different formats and makes it easy to test whole ranges. Note that 172.122.x.x is not in the RFC1918 private block (172.16.0.0/12) so those addresses are public; see the RFC for private-address ranges RFC 1918.

A practical, safe check for IPv4 looks like this:

<?php
$ip = $_SERVER['REMOTE_ADDR'];

if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) !== false) {
    $num = ip2long($ip);
    $start = ip2long('172.122.0.0');
    $end   = ip2long('172.122.255.255');

    if ($num >= $start && $num <= $end) {
        // exclude
    } else {
        // allow
    }
}
?>

Use ip2long and validate with filter_var. Be aware ip2long can return negative values on 32-bit builds (use sprintf('%u', ip2long(...)) if you need unsigned). For IPv6 use inet_pton and comparable binary comparisons (inet_pton). Finally, if your app sits behind proxies, only trust X-Forwarded-For when it comes from known, trusted proxies.

Recommended Answers

All 4 Replies

$ip = $_SERVER['remote_addr'];
$firstDigits = substr($ip, 0, 8);
if($firstDigits != '172.122.') {
//do whatever
}

Hope that helps :)

oh my god

you are just master of masters , i was unable to solve the problem since last 6 months.
Big thanks to you bro. it worked great and solved my problem.

aaaah how relaxing is it now.

Thanks again.

.

glad I could help! :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.