简体   繁体   English

在存储之前加密用户的IP地址

[英]Encrypting user's IP address before storing it

I'm using PHP and MySQL , and I want to store users' IP addresses into the database for comparison purposes (eg allowing only one flag to a thread per IP). 我正在使用PHPMySQL ,我想将用户的IP地址存储到数据库中以进行比较 (例如,每个IP只允许一个标志到一个线程)。 Would it be okay to do it the following way? 可以通过以下方式进行吗?

Retrieving it in PHP: 在PHP中检索它:

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

And then saving it into the database as a VARCHAR(32) . 然后将其作为VARCHAR(32)保存到数据库中。

If I had to make a more comprehensive use of the IPs this wouldn't be the proper way to do it I guess, but if it's only to make sure that the same IP didn't do something twice would be okay to use the md5 encryption to simplify things (unifying IPv4 and IPv6 addresses into one)? 如果我必须更全面地使用IP,这不是正确的方法,我想,但如果只是为了确保相同的IP没有做两次的事情可以使用md5加密以简化事物(将IPv4IPv6地址统一为一个)?

Yes, this is fine, though your terminology is wrong: this is hashing, and hashing is not encryption. 是的,这很好,虽然你的术语是错误的:这是散列,而散列不是加密。

You should also parse the X-FORWARDED-FOR and Client-IP headers unless you want to block everyone behind a proxy as if they were a single user (eg everyone at large companies, high schools, etc). 您还应解析X-FORWARDED-FORClient-IP标头,除非您想阻止代理后面的每个人,就像他们是单个用户一样(例如,大公司,高中等所有人)。

You might want to consider converting the IP to a number. 您可能需要考虑将IP转换为数字。 A little quicker on the lookup because it's numeric data and you can use INET_ATON() and INET_NTOA() in your queries. 查找更快一些,因为它是数字数据,您可以在查询中使用INET_ATON()和INET_NTOA()。

http://dev.mysql.com/doc/refman/5.5/en/miscellaneous-functions.html#function_inet-aton 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 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 a number PHP转换为数字

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

Also, you might want to consider the IP Address you're using with this function: 此外,您可能需要考虑使用此功能的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 for IPv6 : RE -Edit for IPv6

Principles all still apply, but IPv6 Conversions already answered at How to convert IPv6 from binary for storage in MySQL 原则仍然适用,但IPv6转换已在如何将二进制转换为二进制存储在MySQL中进行了回答

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM