473,394 Members | 1,965 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

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

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
9 2943
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
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: unndunn | last post by:
I'm trying to design a regular expression that matches (using preg_match()) when a string is a well-formed Email address. So far I have this: /^+@+\.{2,4}$/i I got that from...
4
by: vishal | last post by:
how can i verify the email address entered by client??? is there any readily available function for that in php or mysql????? else suggest me some links for verifying email address enetered...
16
by: jacob navia | last post by:
Valid pointers have two states. Either empty (NULL), or filled with an address that must be at a valid address. Valid addresses are: 1) The current global context. The first byte of the data...
3
by: Andras Tantos | last post by:
Hi! I had a discussion in the other day about the usefullness and validity of the following (non-std) C extension in PICC: static bit BitVar @ 0x20*8+0; This would mean that a static...
7
by: pinkfloydhomer | last post by:
When a function is given a pointer, it is tempting to do an assert on the pointer to see if it is not NULL. But even if it is not NULL, it could still be invalid or wrong. What we really want to...
43
by: Sensei | last post by:
Hi! I'm thinking about a good programming style, pros and cons of some topics. Of course, this has nothing to do with indentation... Students are now java-dependent (too bad) and I need some...
351
by: CBFalconer | last post by:
We often find hidden, and totally unnecessary, assumptions being made in code. The following leans heavily on one particular example, which happens to be in C. However similar things can (and...
0
by: rdemyan | last post by:
Is there a way to check the validity of front-end table links on a backend file where all permissions to data have been revoked. To get at the data, the front end uses RWOP queries. What I'm...
10
by: frakie | last post by:
Hi 'body, is there a method to check if a pointer is pointing a freed memory location?
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.