Vectorials
Flash Perfection
3D Lessons
Tutorialkit
Markup Tutorials
Learn PHP
network
adv banner
Web Programming  Home Web Programming PHP URL Verification
rss

URL Verification

Author: Andrew Spiziri More by this author


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>";
}
}
?>


Rate this Material: Bad 1 2 3 4 5 Excellent
print this page tell a friend subscribe to newsletter subscribe to rss

Read/Add comments to "URL Verification"

comments  olaf December 31, 2005 says:
URL Verification
Hello, This works good but will only do one part of the job. If someone needs to test if his backlink on a specific page still exists, you need to use some regulare expression like:
:
if (preg_match("/<a(.*)href=["']http://www.domain.com(/?)["'](.*)>(.*)</a>/", $part)) {     $var = true; }
Check the whole function here: (Back) link checker Regards Olaf