|
Posted by shimmyshack on 02/23/07 18:01
On 22 Feb, 21:59, "xhe" <hexuf...@gmail.com> 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
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.
[Back to original message]
|