SEARCH
Newsletter
Subscribe to get design
tips, latest trends, free
stuff and more.
It doesn't look like an e-mail address

hosting

  Web Programming PHP URL Verification

URL Verification

AndrewSpiziri PHP Dec 29, 2005

URL VerificationDo you have a big list of links in your database that you want to display but only want to show ones that are still in existance? Well theres a very easy method for doing so. All you need is the fsockopen() function. Here is an example:

<?
$testURL = @fsockopen("www.FreeTemplateDesigns.com", 80, $errno, $errstr, 30);
if($testURL)
{
echo "<a href="http://www.FreeTemplateDesigns.com">Free Template Designs</a>";
}
>?>

In this example you can see the fsocketopen() function being called as a test on the url. If it can connect to it, it exists and prints. Otherwise, nothing happens. now lets say you wanted to do this with a large list of links from your database. It's just as simple.

<?php
$link = mysql_connect('localhost', 'username', 'password');
mysql_select_db('database', $link);

$query = mysql_query("SELECT * FROM links");
while($row = mysql_fetch_array($query))
{
$testURL = @fsockopen("www.FreeTemplateDesigns.com", 80, $errno, $errstr, 30);
if($testURL)
{
echo "<a href="http://$row[url]">$row[name]</a><br>";
}
}
?>

   
subscribe to newsletter