Page 1 of 1

Big Problem with the code!

Posted: Mon Nov 14, 2005 2:53 pm
by ph4nt4zm
I need help solving this code:

Code: Select all

function Dot2LongIP ($IPaddr)
{
if ($IPaddr == "") {
return 0;
} else {
$ips = split ("\.", "$IPaddr");
return ($ips[3] + $ips[2] * 256 + $ips[1] * 65536 + $ips[0]
*16777216); }
}

Thank you so' much.

Posted: Mon Nov 14, 2005 2:56 pm
by Nathaniel
Why do you have a slash before the period in your "split" call?

I don't know what your problem is, but try replacing $ips = split ("\.", "$IPaddr"); with $ips = explode('.', $IPaddr);

And you don't need quotes around $IPaddr... just the variable is fine...

- Nathaniel

Posted: Mon Nov 14, 2005 3:04 pm
by ph4nt4zm
I don't know much about PHP scripting. This is the code that a friend gaved me and I just want to make it work for my website, so' please, if you are kind to tell me the right final code.

Thanks.

Posted: Mon Nov 14, 2005 3:05 pm
by Nathaniel
Do as I said, test the function, then tell us if it doesn't work. :)

Posted: Mon Nov 14, 2005 3:08 pm
by wtf
What are you trying to achieve with this code? Why are you using it? For what purpose?

Posted: Mon Nov 14, 2005 3:10 pm
by timvw
If i get it right, the following (built-in) function does it for you: http://www.php.net/ip2long

Posted: Mon Nov 14, 2005 3:17 pm
by ph4nt4zm
wtf wrote:What are you trying to achieve with this code? Why are you using it? For what purpose?

I Just want to make it work, it's a simple code but I am stupid and I can't make it work, Appreciate all help given, THX!~

Posted: Mon Nov 14, 2005 3:21 pm
by ph4nt4zm
Nathaniel wrote:Do as I said, test the function, then tell us if it doesn't work. :)

OK :) This is how I entered it on my index.php let's say page:

Code: Select all

<?php
function Dot2LongIP ($IPaddr)
{
if ($IPaddr == "") {
return 0;
} else {
$ips = explode('.', $IPaddr);
return ($ips[3] + $ips[2] * 256 + $ips[1] * 65536 + $ips[0]
*16777216); }
}

It's not working now, what to do?

Posted: Mon Nov 14, 2005 3:32 pm
by timvw
You're missing a call to actually execute that function...

Eg:

Code: Select all

<?php
echo Dot2LongIP($_SERVER['REMOTE_ADDR'];
?>

Posted: Mon Nov 14, 2005 4:02 pm
by ph4nt4zm
I'm interested in inputing the Long IP Number (long2ip) and resulting the IP Adress.

For Example: I want to know Long IP Number: 0201327359 what is the IP adress?

Thanks alot.

Posted: Mon Nov 14, 2005 4:04 pm
by trukfixer
May I recommend using the php manual? if you go here : http://us2.php.net/manual/en/function.ip2long.php

and scroll down a little bit and *READ* (RTFM!) you would see another function just below it called long2ip to do the exact opposite.

Posted: Mon Nov 14, 2005 4:08 pm
by ph4nt4zm
Thanks Guys.
Made it.

Case closed!

Posted: Mon Nov 14, 2005 4:12 pm
by Ambush Commander
The PHP manual is your best friend. No other language has such a helpful manual (I'm serious. Every try using the Perl manual?).