Encrypt a user's IP address before saving it

I use PHP and MySQL , and I want to store the IP addresses of users in the database for comparison purposes (for example, allowing only one flag per thread for an IP address). Would it be nice to do this as follows?

Extracting it in PHP:

$ipAddress = md5($_SERVER["REMOTE_ADDR"]);

And then save it in the database as VARCHAR(32).

If I had to use IP addresses more fully, this would not be the right way to do this, I think, but if only to make sure that the same IP address did not do something twice, it would be good to use md5encryption to simplify things (combining IPv4 and IPv6 into one)?

+3
source share
2 answers

Yes, thatโ€™s fine, although your terminology is wrong: this is hashing, and hashing is not encryption.

You should also parse the headers X-FORWARDED-FORand Client-IP, if you do not want to block everyone behind the proxy server, as if they were one user (for example, all in large companies, high schools, etc.).

+5
source

You might want to consider converting IP to number. A bit faster than searching because it is numeric data and you can use INET_ATON () and INET_NTOA () in your queries.

http://dev.mysql.com/doc/refman/5.5/en/miscellaneous-functions.html#function_inet-aton

mysql> SELECT INET_ATON('10.0.5.9');
        -> 167773449

http://dev.mysql.com/doc/refman/5.5/en/miscellaneous-functions.html#function_inet-ntoa

mysql> SELECT INET_NTOA(167773449);
        -> '10.0.5.9'

PHP to convert to number

$ipA = $_SERVER["REMOTE_ADDR"];
$octets = split ("\.", $ipA);
$ipN = ($octets[3] + $octets[2] * 256 + $octets[1] * pow(256,2) + $octets[0] * pow(256,3);

, IP-, :

/* Get Actual IP Address, in spite of proxy server */
function getRealIpAddr() {
    if (!empty($_SERVER['HTTP_CLIENT_IP']))   //check ip from share internet
    {   $ip=$_SERVER['HTTP_CLIENT_IP']; }
    elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR']))   //to check ip is pass from proxy
    {   $ip=$_SERVER['HTTP_X_FORWARDED_FOR']; }
    else
    {   $ip=$_SERVER['REMOTE_ADDR']; }
    return $ip;
}

RE -Edit IPv6:

, IPv6 Conversions IPv6 MySQL

+2

All Articles