Big Problem with the code!

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ph4nt4zm
Forum Newbie
Posts: 6
Joined: Mon Nov 14, 2005 2:49 pm

Big Problem with the code!

Post 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.
User avatar
Nathaniel
Forum Contributor
Posts: 396
Joined: Wed Aug 31, 2005 5:58 pm
Location: Arkansas, USA

Post 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
ph4nt4zm
Forum Newbie
Posts: 6
Joined: Mon Nov 14, 2005 2:49 pm

Post 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.
User avatar
Nathaniel
Forum Contributor
Posts: 396
Joined: Wed Aug 31, 2005 5:58 pm
Location: Arkansas, USA

Post by Nathaniel »

Do as I said, test the function, then tell us if it doesn't work. :)
User avatar
wtf
Forum Contributor
Posts: 331
Joined: Thu Nov 03, 2005 5:27 pm

Post by wtf »

What are you trying to achieve with this code? Why are you using it? For what purpose?
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

If i get it right, the following (built-in) function does it for you: http://www.php.net/ip2long
ph4nt4zm
Forum Newbie
Posts: 6
Joined: Mon Nov 14, 2005 2:49 pm

Post 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!~
ph4nt4zm
Forum Newbie
Posts: 6
Joined: Mon Nov 14, 2005 2:49 pm

Post 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?
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

You're missing a call to actually execute that function...

Eg:

Code: Select all

<?php
echo Dot2LongIP($_SERVER['REMOTE_ADDR'];
?>
ph4nt4zm
Forum Newbie
Posts: 6
Joined: Mon Nov 14, 2005 2:49 pm

Post 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.
User avatar
trukfixer
Forum Contributor
Posts: 174
Joined: Fri May 21, 2004 3:14 pm
Location: Miami, Florida, USA

Post 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.
ph4nt4zm
Forum Newbie
Posts: 6
Joined: Mon Nov 14, 2005 2:49 pm

Post by ph4nt4zm »

Thanks Guys.
Made it.

Case closed!
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post 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?).
Post Reply