Jump to content

Link Checker + Time it took


jjmusicpro

Recommended Posts

Ok, so i looked around and didnt find anything on this, so here i am!

 

I have a table named activeurls, with 2 colums, url_id and url. I have around 300 records of urls...

 

examples of these would be www.joesplace.com/someplace.html etc..

 

I wanted to know how to run a script that will check the url to see if the page loads, or times out etc. plus the time it took to respond.

These can just be kicked out on the same page...

 

I dont know where to start....

 

Here is what i have, i dont know how to run each url through the loop, and get the time it took for it to load the page.

For now, i will only care about the pages not repsonding, or broken links for error codes.

 

ANy ideas?

<head></head>
<body>
<form method="post">
Check Url Status:<br>
<input type="submit" name="submit">
</form>
<?php
if($_SERVER['REQUEST_METHOD'] == "POST"){ 

$query = "SELECT * from activeurls";
$result2 = mysql_query($query); 

if($count==""){ 
echo "Done checking";
}else{ 
while($row = mysql_fetch_array($result, MYSQL_ASSOC)){ 
$url_id = $row['url_id'];
$url = $row['url'];
echo "URLID . $url_id . $url";

}
?>
</body>

Link to comment
Share on other sites

lol, phishy, fishy, similar yet different.

 

PM'ing someone about a question or problem you have isn't the way to ask a question, this is a PHP Forum for asking questions to a large crowd, not singling out the professionals to get a quick answer, and double posting isn't the right way to go about it either.

 

Regards ACE

Link to comment
Share on other sites

Okay heres a soultion, not 100% perfect but works well

<?php
$URLarray = array(
"http://uk2.php.net", 
"http://gfdghdsfghsdgfhsdghj.net", 
"http://www.phpfreaks.com/"	);

echo "<table>\n";
echo "  <tr>\n";
echo "      <td>\n";
echo "URL"; 
echo "      </td>\n";
echo "      <td>\n";
echo "Time"; 
echo "      </td>\n";
echo "  </tr>\n";

foreach($URLarray as $URL)
{
echo "  <tr>\n";
$mtime = microtime(); 
$mtime = explode(' ', $mtime); 
$mtime = $mtime[1] + $mtime[0]; 
$starttime = $mtime; 
if( url_exists($URL) )
{
	$data = file_get_contents($URL);
	unset($data);//dump	
	$mtime = microtime(); 
	$mtime = explode(" ", $mtime); 
	$mtime = $mtime[1] + $mtime[0]; 
	$endtime = $mtime; 
	$totaltime = ($endtime - $starttime); 
	$UrlResults[$URL] = array("Access" => true, "Time"=>$totaltime); 
	echo "      <td>\n";
	echo "$URL"; 
	echo "      </td>\n";
	echo "      <td>\n";
	echo "$totaltime seconds."; 
	echo "      </td>\n";
}else{
	$UrlResults[$URL] = array("Access" => false); 
	echo "      <td>\n";
	echo "$URL"; 
	echo "      </td>\n";
	echo "      <td>\n";
	echo "Failed"; 
	echo "      </td>\n";
}
echo "  </tr>\n";
flush();
}
echo "</table>\n";

//Results from loop (if you want)
//foreach($UrlResults as $URLs => $Tests)
//{
//	echo "$URLs: ";
//	if($Tests['Access'])
//	{
//		echo "Works access time:".$Tests['Time'];
//	}else{
//		echo "Failed";
//	}
//	echo "<br />";
//}


function url_exists($strURL)
{
    $resURL = curl_init();
    curl_setopt($resURL, CURLOPT_URL, $strURL);
curl_setopt($resURL, CURLOPT_BINARYTRANSFER, 1);
    curl_setopt($resURL, CURLOPT_HEADERFUNCTION, 'readHeader');
    curl_setopt($resURL, CURLOPT_FAILONERROR, 1);

    $x = curl_exec($resURL);

    $intReturnCode = curl_getinfo($resURL, CURLINFO_HTTP_CODE);
    curl_close ($resURL);

    if ($intReturnCode != 200 && $intReturnCode != 302 && $intReturnCode != 304) {
       return false;
    }else{
        return true ;
    }
} 
function readHeader($data)
{
return $data;
}
?>

 

that will be $15 please, Just kidding

 

the flaw is it downloads the html not the images,

of course you can remove the

		$data = file_get_contents($URL);
	unset($data);//dump	

code to check the resolved rate instead of page download time, that will also work..

 

i hope this help :)

Link to comment
Share on other sites

Thanks MadTechie!  ;D

 

I tried running it, however it just sits there and thinks....

 

Is there a way to...if the site does not load within 8 seconds then count it as a 404 error?

 

Alos, is there a way for this to display the results as it does them?

 

Thanks again...

Link to comment
Share on other sites

When i run the script, it just keeps going and going,  never stops lol...

 

<head>Test Cheker 1.0v</head>
<body>

<?php
$URLarray = array(
"http://www.phpfreaks.com/"	);

echo "<table>\n";
echo "  <tr>\n";
echo "      <td>\n";
echo "URL"; 
echo "      </td>\n";
echo "      <td>\n";
echo "Time"; 
echo "      </td>\n";
echo "  </tr>\n";

foreach($URLarray as $URL)
{
echo "  <tr>\n";
$mtime = microtime(); 
$mtime = explode(' ', $mtime); 
$mtime = $mtime[1] + $mtime[0]; 
$starttime = $mtime; 
if( url_exists($URL) )
{
	$data = file_get_contents($URL);
	unset($data);//dump	
	$mtime = microtime(); 
	$mtime = explode(" ", $mtime); 
	$mtime = $mtime[1] + $mtime[0]; 
	$endtime = $mtime; 
	$totaltime = ($endtime - $starttime); 
	$UrlResults[$URL] = array("Access" => true, "Time"=>$totaltime); 
	echo "      <td>\n";
	echo "$URL"; 
	echo "      </td>\n";
	echo "      <td>\n";
	echo "$totaltime seconds."; 
	echo "      </td>\n";
}else{
	$UrlResults[$URL] = array("Access" => false); 
	echo "      <td>\n";
	echo "$URL"; 
	echo "      </td>\n";
	echo "      <td>\n";
	echo "Failed"; 
	echo "      </td>\n";
}
echo "  </tr>\n";
flush();
}
echo "</table>\n";

//Results from loop (if you want)
//foreach($UrlResults as $URLs => $Tests)
//{
//	echo "$URLs: ";
//	if($Tests['Access'])
//	{
//		echo "Works access time:".$Tests['Time'];
//	}else{
//		echo "Failed";
//	}
//	echo "<br />";
//}


function url_exists($strURL)
{
    $resURL = curl_init();
    curl_setopt($resURL, CURLOPT_URL, $strURL);
curl_setopt($resURL, CURLOPT_BINARYTRANSFER, 1);
    curl_setopt($resURL, CURLOPT_HEADERFUNCTION, 'readHeader');
    curl_setopt($resURL, CURLOPT_FAILONERROR, 1);

    $x = curl_exec($resURL);

    $intReturnCode = curl_getinfo($resURL, CURLINFO_HTTP_CODE);
    curl_close ($resURL);

    if ($intReturnCode != 200 && $intReturnCode != 302 && $intReturnCode != 304) {
       return false;
    }else{
        return true ;
    }
} 
function readHeader($data)
{
return $data;
}
?> 
</body>

Link to comment
Share on other sites

it should display them as they are done, (hence the flush();)

 

try changing

flush();

to

ob_flush();
flush();

as for the timeout

it depends on whats the slow part,

comment out the following code

	$data = file_get_contents($URL);
	unset($data);//dump	

see if its quicker

as for the curl

try adding this

curl_setopt($resURL, CURLOPT_TIMEOUT, 1); 

 

 

 

Works for me

<head>Test Cheker 1.0v</head>
<body>
<?php
$URLarray = array(
"http://www.phpfreaks.com/"	);


echo "<table>\n";
echo "  <tr>\n";
echo "      <td>\n";
echo "URL"; 
echo "      </td>\n";
echo "      <td>\n";
echo "Time"; 
echo "      </td>\n";
echo "  </tr>\n";

foreach($URLarray as $URL)
{
echo "  <tr>\n";
$mtime = microtime(); 
$mtime = explode(' ', $mtime); 
$mtime = $mtime[1] + $mtime[0]; 
$starttime = $mtime; 
if( url_exists($URL) )
{
	$data = file_get_contents($URL);
	unset($data);//dump	
	$mtime = microtime(); 
	$mtime = explode(" ", $mtime); 
	$mtime = $mtime[1] + $mtime[0]; 
	$endtime = $mtime; 
	$totaltime = ($endtime - $starttime); 
	$UrlResults[$URL] = array("Access" => true, "Time"=>$totaltime); 
	echo "      <td>\n";
	echo "$URL"; 
	echo "      </td>\n";
	echo "      <td>\n";
	echo "$totaltime seconds."; 
	echo "      </td>\n";
}else{
	$UrlResults[$URL] = array("Access" => false); 
	echo "      <td>\n";
	echo "$URL"; 
	echo "      </td>\n";
	echo "      <td>\n";
	echo "Failed"; 
	echo "      </td>\n";
}
echo "  </tr>\n";
flush();
}
echo "</table>\n";

//Results from loop (if you want)
//foreach($UrlResults as $URLs => $Tests)
//{
//	echo "$URLs: ";
//	if($Tests['Access'])
//	{
//		echo "Works access time:".$Tests['Time'];
//	}else{
//		echo "Failed";
//	}
//	echo "<br />";
//}


function url_exists($strURL)
{
    $resURL = curl_init();
    curl_setopt($resURL, CURLOPT_URL, $strURL);
curl_setopt($resURL, CURLOPT_BINARYTRANSFER, 1);
    curl_setopt($resURL, CURLOPT_HEADERFUNCTION, 'readHeader');
    curl_setopt($resURL, CURLOPT_FAILONERROR, 1);
curl_setopt($resURL, CURLOPT_TIMEOUT, 1); 

    $x = curl_exec($resURL);

    $intReturnCode = curl_getinfo($resURL, CURLINFO_HTTP_CODE);
    curl_close ($resURL);

    if ($intReturnCode != 200 && $intReturnCode != 302 && $intReturnCode != 304) {
       return false;
    }else{
        return true ;
    }
} 
function readHeader($data)
{
return $data;
}
?>

</body>

Link to comment
Share on other sites

i try to run the script with multiple sites to check but it just comes up blank...

 

http://neckter.com/linkchecker.php

 

<head><title>Test Cheker 1.0v</title></head>
<body>
<b>Test Urls 1.0v</b><p>
<?php
$URLarray = array(
"http://www.phpfreaks.com/"	);
"http://www.ebay.com/"	);
"http://www.myspace.com/"	);


echo "<table>\n";
echo "  <tr>\n";
echo "      <td>\n";
echo "URL"; 
echo "      </td>\n";
echo "      <td>\n";
echo "Time"; 
echo "      </td>\n";
echo "  </tr>\n";

foreach($URLarray as $URL)
{
echo "  <tr>\n";
$mtime = microtime(); 
$mtime = explode(' ', $mtime); 
$mtime = $mtime[1] + $mtime[0]; 
$starttime = $mtime; 
if( url_exists($URL) )
{
	$data = file_get_contents($URL);
	unset($data);//dump	
	$mtime = microtime(); 
	$mtime = explode(" ", $mtime); 
	$mtime = $mtime[1] + $mtime[0]; 
	$endtime = $mtime; 
	$totaltime = ($endtime - $starttime); 
	$UrlResults[$URL] = array("Access" => true, "Time"=>$totaltime); 
	echo "      <td>\n";
	echo "$URL"; 
	echo "      </td>\n";
	echo "      <td>\n";
	echo "$totaltime seconds."; 
	echo "      </td>\n";
}else{
	$UrlResults[$URL] = array("Access" => false); 
	echo "      <td>\n";
	echo "$URL"; 
	echo "      </td>\n";
	echo "      <td>\n";
	echo "Failed"; 
	echo "      </td>\n";
}
echo "  </tr>\n";
flush();
}
echo "</table>\n";

//Results from loop (if you want)
//foreach($UrlResults as $URLs => $Tests)
//{
//	echo "$URLs: ";
//	if($Tests['Access'])
//	{
//		echo "Works access time:".$Tests['Time'];
//	}else{
//		echo "Failed";
//	}
//	echo "<br />";
//}


function url_exists($strURL)
{
    $resURL = curl_init();
    curl_setopt($resURL, CURLOPT_URL, $strURL);
curl_setopt($resURL, CURLOPT_BINARYTRANSFER, 1);
    curl_setopt($resURL, CURLOPT_HEADERFUNCTION, 'readHeader');
    curl_setopt($resURL, CURLOPT_FAILONERROR, 1);
curl_setopt($resURL, CURLOPT_TIMEOUT, 1); 

    $x = curl_exec($resURL);

    $intReturnCode = curl_getinfo($resURL, CURLINFO_HTTP_CODE);
    curl_close ($resURL);

    if ($intReturnCode != 200 && $intReturnCode != 302 && $intReturnCode != 304) {
       return false;
    }else{
        return true ;
    }
} 
function readHeader($data)
{
return $data;
}
?>

</body>

Link to comment
Share on other sites

OK done

please note the multiple sites (no comma on the last one)

 

<head>Test Cheker 1.0v</head>
<script language="javascript">
function display(id ,str)
{
document.getElementById(id).innerHTML = str;
}
</script>
<body>
<?php
$URLarray = array(
"http://www.phpfreaks.com/",
"http://www.ebay.com/",
"http://www.myspace.com/"
	);


echo "<table>\n";
echo "  <tr>\n";
echo "      <td>\n";
echo "URL"; 
echo "      </td>\n";
echo "      <td>\n";
echo "Time"; 
echo "      </td>\n";
echo "  </tr>\n";

//URLS
foreach($URLarray as $K => $URL)
{
echo "  <tr>\n";

echo "      <td>\n";
echo "			$URL"; 
echo "      </td>\n";
echo "      <td>\n";
echo "			<div id='$K'>please Wait</div>"; 
echo "      </td>\n";

echo "  </tr>\n";
ob_flush();
flush();
}
echo "</table>\n";


//Times
foreach($URLarray as $K => $URL)
{
$mtime = microtime(); 
$mtime = explode(' ', $mtime); 
$mtime = $mtime[1] + $mtime[0]; 
$starttime = $mtime; 
if( url_exists($URL) )
{
	$data = file_get_contents($URL);
	unset($data);//dump	
	$mtime = microtime(); 
	$mtime = explode(" ", $mtime); 
	$mtime = $mtime[1] + $mtime[0]; 
	$endtime = $mtime; 
	$totaltime = ($endtime - $starttime); 
	$UrlResults[$URL] = array("Access" => true, "Time"=>$totaltime); 
	echo "			<script language='javascript'>display('$K', '$totaltime seconds');</script>"; 
}else{
	$UrlResults[$URL] = array("Access" => false); 
	echo "			<script language='javascript'>display('$K', 'Failed');</script>"; 
}
ob_flush();
flush();
}


function url_exists($strURL)
{
    $resURL = curl_init();
    curl_setopt($resURL, CURLOPT_URL, $strURL);
curl_setopt($resURL, CURLOPT_BINARYTRANSFER, 1);
    curl_setopt($resURL, CURLOPT_HEADERFUNCTION, 'readHeader');
    curl_setopt($resURL, CURLOPT_FAILONERROR, 1);
curl_setopt($resURL, CURLOPT_TIMEOUT, 1); 

    $x = curl_exec($resURL);

    $intReturnCode = curl_getinfo($resURL, CURLINFO_HTTP_CODE);
    curl_close ($resURL);

    if ($intReturnCode != 200 && $intReturnCode != 302 && $intReturnCode != 304) {
       return false;
    }else{
        return true ;
    }
} 
function readHeader($data)
{
return $data;
}
?>

</body>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.