Jump to content

checking if website address is vailid?


runnerjp

Recommended Posts

i have some script set up to see if a website is vaild..

 

what im doing is on my 1st page using a textbox to add a number of websites wuch as

 

website1

website2

 

then using the code to extract them sites from the db and provide links to them...

 

<?php function url($link, $text='')
{
    // Ensure $link is a valid URL        
    $url = url_resolve($link);
    
    $fp = @fopen($url, 'r');
    if (!$fp) {
        $title = (!$text ? $link : $text);
        return "(Broken Link: $link". ($text ? ": {$text}" : "") .")";
    }

    // Grab <title>*</title> from the first "chunk" of data
    $title = url_grab_title($fp);

    // No <title> tag in first chunk...    
    if (!$title) {
        $title = (!$text ? $link : $text);
        return "<a href='$url'>$title</a>";
    }

    // User gave me some text to append to the link title
    if ($text) {    
        $title .= ': '. $text;
    }
    
    return "<a href='$url'>$title</a>";
}
function url_resolve($url)
{
    global $url_site_domain;
    
    if (!preg_match('/^(http|ftp):/', $url)) {
        // assume it's not "server.tld" and instead "/dir/file.ext"
        if (!preg_match('/\.(com|net|org|co|uk|edu|info|biz)/', $url)) {
            $url = $url_site_domain .'/'. $url;    
        }
    }
    return 'http://'. preg_replace('/\/\//', '/', $url);
}

// Fetch a "chunk" of data and look for <title> in it
function url_grab_title($fp)
{
    // How many bytes to grab in one chunk.
    // Most sites seem to have <title> within 512
    $chunk_size = 512;

    $chunk = fread($fp, $chunk_size);
    $chunk = preg_replace("/(\n|\r)/", '', $chunk);

    // Look for <title>(.*?)</title> in the text
    if (preg_match('/<title>(.*?)<\/title>/i', $chunk, $matches)) {
        return $matches[1];
    }

    return null;
}
// Run this to test the code    
function url_test()
{
    print url('hurring.com') ."\n";
    // <a href='http://hurring.com'>Hurring.com</a>
    print url('google.com') ."\n";
    // <a href='http://google.com'>Google</a>
    print url('hurring.com', 'Append me') ."\n";
    // <a href='http://hurring.com'>Hurring.com: Append me</a>
    print url('/code/php/') ."\n";
    // <a href='http://hurring.com/code/php/'>Hurring.com: Code: PHP</a>
    print url('/code/php/', "Append me") ."\n";
    // <a href='http://hurring.com/code/php/'>Hurring.com: Code: PHP: Append me</a>
    print url('groups.yahoo.com/groups/alhkjter/') ."\n";
    // <a href='http://groups.yahoo.com/groups/alhkjter/'>groups.yahoo.com/groups/alhkjter/</a>
    print url('groups.yahoo.com/groups/alhkjter/', 'Pirates') ."\n";
    // <a href='http://groups.yahoo.com/groups/alhkjter/'>Pirates</a>
    print url('garbage') ."\n";
    // (Broken Link: garbage)
    print url('garbage', 'Pirates') ."\n";
    // (Broken Link: garbage: Pirates)
}


mysql_connect("localhost", "runningp", "runnerjp2003") or die(mysql_error());

mysql_select_db("runningp_1") or die(mysql_error());

$code = $_GET['id'];

$geturls = mysql_fetch_assoc(mysql_query("SELECT * from url where id='$code'"));
if (!$geturls)
  {
  die(mysql_error());
  }
foreach(explode("\n",$geturls['url1']) as $line) {
  print url($line) ."\n";?>
<br \>
<?
}
?>

 

the thing is if i seem to add multipul addresses my code only works out 1 address and uses the rest as broken link... cna any 1 aid me in fixing this?

Link to comment
https://forums.phpfreaks.com/topic/145607-checking-if-website-address-is-vailid/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.