Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Help with converting python code to php
Old 03-12-2009, 08:54 PM Help with converting python code to php
Junior Talker

Posts: 1
Name: Kevin
Trades: 0
I need to convert convert a python function to php.

the function gets a dotted ip address and convert it to long integer.

python code: return struct.unpack('L',socket.inet_aton(ip))[0]

Can someone give me some ideas what functions i should use to change it to php code.

I tried to use ip2long() and unpack() but i am getting a different number.

Thanks
chantskevin is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 03-14-2009, 10:43 PM Re: Help with converting python code to php
amw_drizz's Avatar
Ultra Talker

Posts: 340
Name: Jon
Location: New York
Trades: 0
whats the purpose of this? inputting it to a mysql database? flat file? I am just curious why you need the ip with out decimals.

Um on the other note, you could try the following,
PHP Code:
$ip $_SERVER['REMOTE_ADDR'];
$exploded_ip explode('.',$ip);
$final_ip implode('',$exploded_ip); 
__________________
AMW_Drizz
Dev Machine:: Apache 2.2.6 PHP 5.2.6 MySQL 5.1
amw_drizz is offline
Reply With Quote
View Public Profile Visit amw_drizz's homepage!
 
Old 03-14-2009, 10:54 PM Re: Help with converting python code to php
NullPointer's Avatar
Will Code for Food

Posts: 2,815
Name: Matt
Location: Irvine, CA
Trades: 0
Quote:
Originally Posted by amw_drizz View Post
whats the purpose of this? inputting it to a mysql database? flat file? I am just curious why you need the ip with out decimals.
I don't think he meant to just remove the decimals. Each component of an IP address is an 8bit value (0 - 255), so you can actually convert it to a 32bit integer.

PHP Code:
function iptoint($ip)
{
     
$ips explode('.'$ip);
     
$binip '';
     foreach(
$ips as $i)
     {
          
//append 0s if strlen < 8
          
$bin decbin($i);
          
$zeros '';
          for(
$i strln($bin); $i 8$i++)
          {
                
$zeros .= '0';
          }
          
$bin $zeros $bin;
          
$binip .= $bin;
     }
     return 
bindec($binip);

__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE

Last edited by NullPointer; 03-14-2009 at 10:59 PM.. Reason: Fixed bug
NullPointer is online now
Reply With Quote
View Public Profile Visit NullPointer's homepage!
 
Reply     « Reply to Help with converting python code to php
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.51143 seconds with 12 queries