...

View Full Version : Internet Connectivity



Serex
06-30-2005, 06:17 AM
Hey all. Just wondering if anyone knew of any inbuilt php boolean type functions that could check for inet connectivity. I tried something that i thought i could use for a shortcut but didnt exactly work :)



function checkFile($pathToFile)
{
if(is_file($pathToFile))
{
return true;
}

return false;
}

$chkPath = checkFile("http://www.google.com/index.html");

if ($chkPath)
{
echo "true";
}
else
{
echo "false";
}


thought that if it could check the url for the index.html then there had to be some sort of connection. doesnt look like it works that easy though. I also tried using the is_link but there wasnt much information regarding it that i could find.

any help would be great


Will be using this script locally

marek_mar
06-30-2005, 10:00 AM
The function chkFile does exaxtly what is_file() does... and it returns bool so the last if/esle is pointless too.

Serex
06-30-2005, 03:19 PM
Ok i never actually looked into the is_file function... kinda posted before work and was kinda pseudo. anyway i say down and have looked at it and came up with teh following



$chkPath = "http://www.google.com/index.html";
echo "<strong>Internet: </strong>";
if(is_file($chkPath))
{
echo "connected";
}
else
{
echo "disconnected";
}

doesnt seem to be working so far, i keep getting "disconnected". however if i do


$chkPath = "http://www.google.com/index.html";
echo "<strong>Internet: </strong>";
if(readfile($chkPath))
{
echo "connected";
}
else
{
echo "disconnected";
}

it returns true, hence "connected" but it displays the url itself. so i will have a google page were i dont exactly want it. is there a way around displaying the actual contents?

Fou-Lu
06-30-2005, 05:12 PM
readfile() will return a result to your output buffer.
You can eliminate this and retain it as a string:



ob_start();
readfile($chkpath);
$checkpath = ob_get_contents();
ob_end_clean();

Now, this method isn't really nessessary. Instead of using readfile() use file_get_contents() instead, which can be loaded as a variable, and not directly outputted to the browser:



if (!$file_read = file_get_contents($chkpath))
{
echo 'disconnected';
}

for instance.


I'd thought I'd mention this as well, I don't know quite what it is your doing with it, but if your running via apache, there are several proprietary functions available for you to use which can look up information on a page. apache_lookup_uri for instance will return several values about the url requested -> including the returned 'status'. This can be used to your advantage as well, and I would assume it to probably be faster than using a filesystem command. I'd need to put that to the test though....

Serex
07-01-2005, 06:39 AM
Hey. Thanks for your reply Fou-Lu that helped me out a bit. I have been trying to get pear installed for the past while so i could start using some of the inbuilt functionality. Finally found a bundle that has it working :D

so now i have


<?php
echo "<strong>Internet: </strong>";
$chkPath = "http://www.google.com";

if (@!$file_read = file_get_contents($chkPath))
{
echo 'disconnected';
}
else
{
echo 'connected';
}
?>


originally displayed a nasty error if the url wasnt active so i had to escape that. only thing to work on now is a quicker way of doing it...

thanks


i looked into the apache_lookup_uri that you mentioned but dont think it could help me in any way. below you see the array that it produces. I dont get any varying information for valid/invalid url's that are passed in... well that i can see anyway



stdClass Object
(
[status] => 200
[the_request] => GET /apache_uri.php HTTP/1.1
[method] => GET
[mtime] => 0
[clength] => 0
[chunked] => 0
[no_cache] => 0
[no_local_copy] => 1
[unparsed_uri] => /www.google.com
[uri] => /www.google.com
[filename] => W:/www/www.google.com
[path_info] =>
[allowed] => 0
[sent_bodyct] => 0
[bytes_sent] => 0
[request_time] => 1120196681
)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum