IP 该如何保存?

最简单的办法是使用字符串(varchar)来保存,如果从效率考虑的话,可以将 ip 保存
为整型(unsigned int) ,使用 php 或 mysql 提供的函数将 ip 转换为整型,然后存储即可。


PHP 函数:long2ip()和 ip2loang()


MySQL 函数:inet_aton()和 inet_ntoa

 

  • long2ip()  ——Converts an (IPv4) Internet network address into a string in Internet standard dotted format互联网标准点格式

string long2ip ( string $proper_address )

The function long2ip() generates an Internet address in dotted format (i.e.: aaa.bbb.ccc.ddd) from the proper address representation.

参数

proper_address

A proper address representation.

返回值

Returns the Internet IP address as a string.

long2ip("256"); //返回 IP 为 "0.0.1.0"
  • ip2long()  ——Converts a string containing an (IPv4) Internet Protocol dotted address into a proper address

int ip2long ( string $ip_address )

The function ip2long() generates an IPv4 Internet network address from its Internet standard format (dotted string) representation.

ip2long() will also work with non-complete IP addresses. Read » http://publibn.boulder.ibm.com/doc_link/en_US/a_doc_lib/libs/commtrf2/inet_addr.htm for more info.

参数

ip_address

A standard format address.

返回值

Returns the IPv4 address or FALSE if ip_address is invalid.

 


 

转载于:https://my.oschina.net/u/2672206/blog/715027

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值