Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7848643
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T18:16:46+00:00 2026-06-02T18:16:46+00:00

I am getting the IP address of a user using $_SERVER[‘REMOTE_ADDR’] who tries to

  • 0

I am getting the IP address of a user using $_SERVER[‘REMOTE_ADDR’] who tries to visit my site. I have different boxes with different IDS and different CIDR entries in my database for each box. I want to check if the user IP exists in any of the CIDR or not for a specific box.
If it exists then he is allowed to enter otherwise not.
let suppose the user IP is

127.0.0.1

suppose The CIDR entries for box 12 is

192.168.1.20/27
192.168.0.10/32

The CIDR entries are stored in a column in database

if he come with an IP that comes inside this range then he should be allowed to enter the site otherwise not.
I want to check his ip against each CIDR entry.

Any ideas please?

Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T18:16:48+00:00Added an answer on June 2, 2026 at 6:16 pm

    OK, try following this 5 simple steps…

    1. Store your CIDRs into array (read ’em from database; guess you know how to get this)

    $cidrs = array(
      '192.168.1.20/27', 
      '192.168.0.10/32'
      );
    

    2. Get user’s IP (remote address)

    $user_ip = $_SERVER['REMOTE_ADDR'];
    

    3. Add this function

    function IPvsCIDR($user_ip, $cidr) {
      $parts = explode('/', $cidr);
      $ipc = explode('.', $parts[0]);
      foreach ($ipc as &$v)
        $v = str_pad(decbin($v), 8, '0', STR_PAD_LEFT);
      $ipc = substr(join('', $ipc), 0, $parts[1]);
      $ipu = explode('.', $user_ip);
      foreach ($ipu as &$v)
        $v = str_pad(decbin($v), 8, '0', STR_PAD_LEFT);
      $ipu = substr(join('', $ipu), 0, $parts[1]);
      return $ipu == $ipc;
      }
    

    4. Compare user’s IP address against $cidrs

    $validaddr = false;
    foreach ($cidrs as $addr)
      if (IPvsCIDR($user_ip, $addr)) {
        $validaddr = true;
        break;
        } 
    

    5. Decide what to do with user

    if ($validaddr) {
      echo "CORRECT IP ADDRESS";
      }
    else {
      echo "INCORRECT IP ADDRESS";
      }
    

    That’s it!

    how this function works. It converts CIDR address-part (x.x.x.x) into binary string and takes first N digits. Then it does same job with user’s IP and checks do values match.

    Example 2 (complete job from function)

    function testUserIP($user_ip, $cidrs) {
      $ipu = explode('.', $user_ip);
      foreach ($ipu as &$v)
        $v = str_pad(decbin($v), 8, '0', STR_PAD_LEFT);
      $ipu = join('', $ipu);
      $res = false;
      foreach ($cidrs as $cidr) {
        $parts = explode('/', $cidr);
        $ipc = explode('.', $parts[0]);
        foreach ($ipc as &$v) $v = str_pad(decbin($v), 8, '0', STR_PAD_LEFT);
        $ipc = substr(join('', $ipc), 0, $parts[1]);
        $ipux = substr($ipu, 0, $parts[1]);
        $res = ($ipc === $ipux);
        if ($res) break;
        }
      return $res;
      }
    

    Usage:

    $user_ip = $_SERVER['REMOTE_ADDR'];
    $cidrs = array('192.168.1.20/27', '192.168.0.10/32'); 
    if (testUserIP($user_ip, $cidrs)) {
      // user ip is ok
      }
    else {
      // access denied
      }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What is the better way of getting the IP address in PHP: getenv('REMOTE_ADDR'); or,
I need to getting the client ip address using javascript.Actually,our connection provider ip is
I'm having a problem getting emails to bounce to a specific email address, different
i am using NetworkInterface class to get the MAC address but i am getting
I have two objects a user and a role which I make persistant using
I have problem with getting username form windows system. I tried using getlogin function
HI..I doing geocoding in android map.(i.e)getting address as input from user & locate that
I am trying to retrieve the user address using MKReverseGeocoder. It works fine, but
Is the only way of getting the address of an address in C (opposite
I'm trying to get the FROM email address in Mule ESB. I'm getting the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.