PHP

PHP IP address based redirect example

PHP IP address based redirect example
  • Romeo

    Thank you Sunmith for sharing this code with all of us.
    I am however experiencing an error though and I am wondering
    if you can assist please, also please bear with me as I am a newbie to PHP code…

    The error I am getting is:
    Notice: A non well formed numeric value encountered in checkUserIP()

    On both of these line numbers:
    $ipc = substr(join(”, $ipc), 0, $parts[1]);
    $ipux = substr($ipu, 0, $parts[1]);

    I have a about 26 entries of CIDR IP Ranges within my text file:
    Example:
    67.193.0.0/16
    72.38.0.0/15
    216.121.128.0/17
    and etc…

    I receive this error even if I only have one CIDR IP Range entry within the text file.

    Additional info – I running this on:
    PHP Version 5.3.2

    Any help you can provide would be much appreciated.

    • Enter line by line from top to bottom ip addresses and make sure there are not characters before and after in the ip addresses/CIDR range. Those values as it probably can’t be formatted as a number.Try this.

      $ipc = floatval(substr(join(”, $ipc), 0, $parts[1]));
      $ipux = floatval(substr($ipu, 0, $parts[1]));

      If not success,
      foreach ($ipu as &$v)

      change to
      foreach ($ipu as $v)

      Note there is & mark.

      Regards

      • Romeo

        Thanks Sunmith,

        This was my first thought to check the text table for any problems with the formatting of the CIDRs and all was good.

        I actually just got it working a few minutes ago without the error by replacing the text file with an array instead.

        Hence I replaced line:

        $cidrs = file($filename, FILE_IGNORE_NEW_LINES);

        with
        $cidrs = array(‘67.193.0.0/16′ ,’72.38.0.0/15′ ,’etc’);

        Works perfect now!
        I suspect there may be an issue with how the file was being accessed on the server, unfortunately it is a hosted server at an ISP so not able to troubleshoot this further…

        Thanks again for the quick reply and help.