Convert IP to Long in MySQL

I am trying to use a table ip2countryto show user country flags on my website.

The simplest thing I came up with is to write an SQL statement that displays users from the session table and queries to find out if their corresponding IP is in a certain range in order to find out their country / flag.

It is simple, but also dangerous, because when there are 300 users on the Internet to show, and I retrieve them from the session table, asking their countries to display flags, there will certainly be a lot of memory usage.

Now I tried to do this in one request:

SELECT 
  s.session_ip, 
  ipc.*
FROM 
  session AS s
    LEFT JOIN ip2country AS ipc 
    ON ipc.ip_lo <= s.session_ip AND ipc.ip_hi >= s.session_ip
WHERE 
  s.session_time  > '".( time() - 60) )."' 

, , IP-, ip2country, , . 1000013824, IP-, , IP-, . 193.169.0.0

, IP long PHP ip2long(), - MySQL, ?

+3
1

SELECT INET_NTOA (1000013824) → 59.155.0.0

SELECT INET_ATON ('193.169.0.0') → 3249078272

:)

+23

Source: https://habr.com/ru/post/1784908/


All Articles