By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,746 Members | 1,928 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,746 IT Pros & Developers. It's quick & easy.

Are there any good solutions for checking the validity of IP address?

P: n/a
xhe
Hi,
I need to program to check the validity of IP address through PHP
Initially I used this one:

$url="http://www.ntc.gov.au/ViewPage.aspx?
page=A02400304500100020";

$fp=fopen($url,"r");
if(!$fp)
{
echo "Failed";
}else
{
echo "success";
}
when I run it, it's output is "Failed",

I then use

$fp=fsockopen($url,80); for second line, and also failed.

This IP address is actually good.
Butwhen I check "www.ntc.gov.au" it is success.

So does that mean by using PHP, we can only check hostname? And if the
IP address is longer and includes the appendix, we can not check it by
PHP?

Or do you have another good solutions?

Thanks in advance.

Frank

Feb 22 '07 #1
Share this Question
Share on Google+
9 Replies


P: n/a
Rik
On Thu, 22 Feb 2007 22:59:39 +0100, xhe <he******@gmail.comwrote:
Hi,
I need to program to check the validity of IP address through PHP
Initially I used this one:

$url="http://www.ntc.gov.au/ViewPage.aspx?
page=A02400304500100020";

$fp=fopen($url,"r");
if(!$fp)
{
echo "Failed";
}else
{
echo "success";
}
when I run it, it's output is "Failed",

I then use

$fp=fsockopen($url,80); for second line, and also failed.

This IP address is actually good.
Butwhen I check "www.ntc.gov.au" it is success.
Where eactly are you checking the validity of an IP adress in you script?
I can see no such effort, heck, I see no IP.
--
Rik Wasmus
Feb 22 '07 #2

P: n/a
xhe
Hi,
The first line is the IP address that I am checking.
It is
$url="http://www.ntc.gov.au/ViewPage.aspx?page=A02400304500100020";

On Feb 22, 5:39 pm, Rik <luiheidsgoe...@hotmail.comwrote:
On Thu, 22 Feb 2007 22:59:39 +0100, xhe <hexuf...@gmail.comwrote:
Hi,
I need to program to check the validity of IP address through PHP
Initially I used this one:
$url="http://www.ntc.gov.au/ViewPage.aspx?
page=A02400304500100020";
$fp=fopen($url,"r");
if(!$fp)
{
echo "Failed";
}else
{
echo "success";
}
when I run it, it's output is "Failed",
I then use
$fp=fsockopen($url,80); for second line, and also failed.
This IP address is actually good.
Butwhen I check "www.ntc.gov.au" it is success.

Where eactly are you checking the validity of an IP adress in you script?
I can see no such effort, heck, I see no IP.
--
Rik Wasmus- Hide quoted text -

- Show quoted text -

Feb 23 '07 #3

P: n/a
Rik
xhe <he******@gmail.comwrote:
The first line is the IP address that I am checking.
It is
$url="http://www.ntc.gov.au/ViewPage.aspx?page=A02400304500100020";
Call me insane, I see no IP address. Do you mean a DNS, or a URL?
DNS:
<http://www.php.net/pareseurl>
<http://www.php.net/gethostbyname>

URL checking should normally be possible with fopen(), if allow_url_fopen
is enabled.
--
Rik Wasmus
Feb 23 '07 #4

P: n/a
xhe wrote:
Hi,
I need to program to check the validity of IP address through PHP
Initially I used this one:

$url="http://www.ntc.gov.au/ViewPage.aspx?
page=A02400304500100020";

$fp=fopen($url,"r");
if(!$fp)
{
echo "Failed";
}else
{
echo "success";
}
when I run it, it's output is "Failed",

I then use

$fp=fsockopen($url,80); for second line, and also failed.

This IP address is actually good.
Butwhen I check "www.ntc.gov.au" it is success.

So does that mean by using PHP, we can only check hostname? And if the
IP address is longer and includes the appendix, we can not check it by
PHP?

Or do you have another good solutions?

Thanks in advance.

Frank
What do you mean by the "validity of the IP address"? For instance,
your fsockopen only works if the target system has a web server running
(same with fopen).
--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Feb 23 '07 #5

P: n/a
xhe
if we use this one
if(fopen("http://www.ntc.gov.au","r"))
{
echo "success";
}else
{
echo "false";
}
result is false.

with fsockopen, we can ONLY check "www.ntc.gov.au", and we can not
check if the whole url is reachable or not.
It seemed that fsockopen is only valid to check host name, while not
the whole ip string.

Any good idea to check the who ip string instead of just host name?

thanks.


On Feb 22, 8:30 pm, Rik <luiheidsgoe...@hotmail.comwrote:
xhe <hexuf...@gmail.comwrote:
The first line is the IP address that I am checking.
It is
$url="http://www.ntc.gov.au/ViewPage.aspx?page=A02400304500100020";

Call me insane, I see no IP address. Do you mean a DNS, or a URL?
DNS:
<http://www.php.net/pareseurl>
<http://www.php.net/gethostbyname>

URL checking should normally be possible with fopen(), if allow_url_fopen
is enabled.
--
Rik Wasmus

Feb 23 '07 #6

P: n/a
xhe kirjoitti:
Hi,
I need to program to check the validity of IP address through PHP
Initially I used this one:

$url="http://www.ntc.gov.au/ViewPage.aspx?
page=A02400304500100020";

$fp=fopen($url,"r");
if(!$fp)
{
echo "Failed";
}else
{
echo "success";
}
when I run it, it's output is "Failed",

I then use

$fp=fsockopen($url,80); for second line, and also failed.

This IP address is actually good.
Butwhen I check "www.ntc.gov.au" it is success.
That's _not_ an IP address. IP is something like 12.34.56.78, four
groups of digits between 0-255 separated with dots (...and then there's
IPv6 address, which is six hexnumbers separated by a semicolon). What
you have there are an url and a domain.

check it out: http://en.wikipedia.org/wiki/IP_address

--
"En ole paha ihminen, mutta omenat ovat elinkeinoni." -Perttu Sirviö
sp**@outolempi.net | Gedoon-S @ IRCnet | rot13(xv***@bhgbyrzcv.arg)
Feb 23 '07 #7

P: n/a
xhe wrote:
On Feb 22, 8:30 pm, Rik <luiheidsgoe...@hotmail.comwrote:
>xhe <hexuf...@gmail.comwrote:
>>The first line is the IP address that I am checking.
It is
$url="http://www.ntc.gov.au/ViewPage.aspx?page=A02400304500100020";
Call me insane, I see no IP address. Do you mean a DNS, or a URL?
DNS:
<http://www.php.net/pareseurl>
<http://www.php.net/gethostbyname>

URL checking should normally be possible with fopen(), if
allow_url_fopen
>is enabled.
--
Rik Wasmus

if we use this one
if(fopen("http://www.ntc.gov.au","r"))
{
echo "success";
}else
{
echo "false";
}
result is false.

with fsockopen, we can ONLY check "www.ntc.gov.au", and we can not
check if the whole url is reachable or not.
It seemed that fsockopen is only valid to check host name, while not
the whole ip string.

Any good idea to check the who ip string instead of just host name?

thanks.
That's because you need to send the request using HTTP headers (if
trying to communicate with a Web server). Also, you're checking if a
resource exists on a server, you're not checking an IP address.
www.ntc.gov.au is a domain.

You should become best friends with Google for a while.

--
Curtis, http://dyersweb.com
Feb 23 '07 #8

P: n/a
On Feb 23, 12:49 am, Curtis <zer0d...@verizon.netwrote:
xhe wrote:
<snip>
>
You should become best friends with Google for a while.

--
Curtis,http://dyersweb.com
/Agreed

But to answer your question (without reading the post that is) of how
to validate an ip address in php ...

<?php
$ip = '192.168.1';
if (($longip = ip2long($ip)) !== false) {
printf("%u<br>", $longip);
if ($ip == long2ip($longip)) {
echo "IP: $ip appears to be ok";
} else {
echo "$ip does not seem to match " .
long2ip($longip);
}
} else {
echo "IP appears to be invalid";
}
?>

For an explanation ...
http://www.jimgrill.com/pages/content/validateip

Which just happened to be the first result on Google for "valid ip
address php".
--Chris F.

Feb 23 '07 #9

P: n/a
On 22 Feb, 21:59, "xhe" <hexuf...@gmail.comwrote:
Hi,
I need to program to check the validity of IP address through PHP
Initially I used this one:

$url="http://www.ntc.gov.au/ViewPage.aspx?
page=A02400304500100020";
$fp=fopen($url,"r");
if(!$fp)
{
echo "Failed";
}else
{
echo "success";
}
when I run it, it's output is "Failed",

I then use

$fp=fsockopen($url,80); for second line, and also failed.

This IP address is actually good.
Butwhen I check "www.ntc.gov.au" it is success.

So does that mean by using PHP, we can only check hostname? And if the
IP address is longer and includes the appendix, we can not check it by
PHP?

Or do you have another good solutions?

Thanks in advance.

Frank
Curtis answered this really but here is some code:

what you really mean is "how can I get php to request a web page to
check it?"
Well you need to get PHP to send all the right HTTP headers, the
traditional and best way is using the cURL library, examples on the
php website, it can handle settings of cookies and even SSL so it is
the best way but heres a quick script that will grab a url, if you use
a proxy put the IP and port at the top, other wise use the website
host and the port as I have don here:

#via a proxy
#$proxy_server = "127.0.0.1";
#$proxy_port = 8080;
#no proxy
$proxy_server = "www.bbc.co.uk";
$proxy_port = 80;

#homepage
$url_path = '/';
#something more adventurous
$url_path = '/go/toolbar/-/radio/d/';

$html = '';
$prox = fsockopen($proxy_server, $proxy_port, $errno, $errstr);
fputs($prox,"GET " . $url_path . " HTTP/1.1\r\n");
fputs($prox,"HOST: www.bbc.co.uk\r\n");
fputs($prox,"User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-
GB; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1\r\n");
fputs($prox,"Accept: text/xml,application/xml,application/xhtml
+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5\r\n");
fputs($prox,"Accept-Language: en-GB,en,q=0.9,pt;q=0.8,fr;q=0.8,\r\n");
fputs($prox,"Accept-Encoding: gzip,deflate\r\n");
fputs($prox,"Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7\r\n");
fputs($prox,"Keep-Alive: 300\r\n");
fputs($prox,"Proxy-Connection: keep-alive\r\n\r\n");
while (!feof ($prox))
{
$curline = fgets($prox, 4096);
if (substr($curline,-2, 2)!="\r\n")
{
$html .= $curline;
}
}
fclose($prox);
echo $html;

By default php doesnt send enough headers to trick the website into
thinking its not a bot. ALthough you can set the user-agent in the
php.ini file, its not enough to fool websites.

The reason why the above script doesnt look very good is that it makes
one request to the homepage.

But if you goto the bbc homepage first in a browser, then some of the
images might get cached and you will then see them if requested by the
html within the output as absolute src.

Hope that helps. Use cURL its better and faster, if you want to know
more download a proxy like paros or fiddlertool and install foxy proxy
for firefox2 and set it to pass your web requests through the proxy.

Feb 23 '07 #10

This discussion thread is closed

Replies have been disabled for this discussion.