0

How can i compare IP submit by Form with IP range in my database (postgresql) ?

Actually my IP Range are stored in jsonb in database

        $builder
        ->add('firstIpAddresses', TextType::class, [
            'constraints'   => [
                new Ip([
                    'version'   => Ip::V4_ONLY_PUBLIC
                ]),
            ],
        ])
        ->add('lastIpAddresses', TextType::class, [
            'constraints'   => [
                new Ip([
                    'version'   => Ip::V4_ONLY_PUBLIC
                ]),
            ],
        ]);

(The data transform)

    /**
 * From the Form to the Database
 *
 * @param mixed $value
 * @return array|string[]
 */
public function reverseTransform($value) : array
{
    $toJsonEncode   = [];

    if ( is_array($value) && ! empty($value) ) {
        foreach ($value as $key => $val) {
            $toJsonEncode[] = [
                'start' => $val['firstIpAddresses'],
                'end'   => $val['lastIpAddresses'],
            ];
        }
    }

    return $toJsonEncode;
}

(This is the IP range in database)

[{"end": "100.100.100.130", "start": "100.100.100.120"}]

If the IP is not in the range on the IP in the database so it will add the new IP else it will put error message

(Sorry for my bad english, I'm new developper and this is my first post on stackoverflow)

  • 3
    Welcome to StackOverflow. Can I ask you **please** don't post images of code and data. It's really unhelpful to the people who are trying to answer your question (you can read https://stackoverflow.com/help/how-to-ask to understand why). It's all text, so please just paste it as text. Hopefully it's quicker anyhow to do that than to take a screenshot, save it and upload it! – ADyson Feb 12 '20 at 11:52
  • Your example of “IP range” here only differs in the last octet. Is that the only case you need to cover? – misorude Feb 12 '20 at 11:55
  • Take a loot at [these answers](https://stackoverflow.com/q/11121817/5914775), they tell you how to do the comparison. – Tom Udding Feb 12 '20 at 11:59
  • @misorude Not only the last octet, sometimes it's for example 203.104.2.0 - 203.104.18.200 – Jérémy BATILLIOT Feb 12 '20 at 13:04
  • @TomUdding Thank you, i will check this post – Jérémy BATILLIOT Feb 12 '20 at 13:09

0 Answers0