PHP

 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me



Go Back   Tutorialized ForumsWeb Design & DevelopmentPHP

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Tutorialized Forums Sponsor:
  #1  
Old March 8th, 2016, 04:55 AM
JebaQpt JebaQpt is offline
Banned
Tutorialized Newbie (0 - 499 posts)
 
Join Date: Feb 2014
Posts: 12 JebaQpt User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 6 h 58 m 39 sec
Reputation Power: 0
How to convert IP address to number in php?

I need to store IP addresses in database, but storing them as strings is not very efficient and not very convenient for my purpose.
So What is the proper way to convert ip to integer in php?

Reply With Quote
  #2  
Old May 6th, 2016, 05:37 AM
Mikecoulson Mikecoulson is offline
Registered User
Tutorialized Newbie (0 - 499 posts)
 
Join Date: Jan 2016
Posts: 6 Mikecoulson User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 51 m 27 sec
Reputation Power: 0
Convert ip in integer

You can use ip2long function.

Here is the code:


Code:
<?php
$ip = gethostbyname('www.ficode.com');
$out = "The following URLs are equivalent:<br />\n";
$out .= 'http://www.ficode.com/, http://' . $ip . '/, and http://' . sprintf("%u", ip2long($ip)) . "/<br />\n";
echo $out;
?>



Let me know if this works or not for you.

Reply With Quote
  #3  
Old January 10th, 2017, 03:49 AM
dishagandhi dishagandhi is offline
Registered User
Tutorialized Newbie (0 - 499 posts)
 
Join Date: Dec 2016
Location: Ahmedabad
Posts: 52 dishagandhi User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 17 h 40 m 42 sec
Reputation Power: 1
Use ip2long(ip) and to get it back use INET_NTOA(ip)

Hi,
You can use an INT

$ip = ip2long($ip);

To save in mysql:

$sql = "INSERT INTO user(ip) VALUES('$ip')";
$dbQuery = mysql_query($sql,$dbLink
You can get back the ip later :

SELECT INET_NTOA(ip) FROM 'user' WHERE 1
INET_NTOA(expr):

Given a numeric IPv4 network address in network byte order, returns the dotted-quad representation of the address as a binary string. INET_NTOA() returns NULL if it does not understand its argument.

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


Thanks

Reply With Quote
Reply

Viewing: Tutorialized ForumsWeb Design & DevelopmentPHP > How to convert IP address to number in php?


Developer Shed Advertisers and Affiliates


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 

Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.

© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap