NamePros
Welcome, Guest! Ready to make a name for yourself in the domain business? We welcome both the hobbyist and professional domainer to join the discussion as part of the NamePros community.

Click here to create your profile to start earning reputation for posting, and trader ratings for buying & selling in our free e-marketplace. Build your trader rating with each successful sale. Our system has tracked over 100,000 sales and counting!
FAQ & TOS Register Search Today's Posts Mark Forums Read

Go Back   NamePros.com > Website Development Discussion Forums > Programming > CODE
Reload this Page Check if a URL exists.

CODE This forum is for posting code snippets and example scripts that aren't quite tutorials, but could be useful for others. You may post code snippets and/or completed scripts that you've written and want to share here.

Advanced Search
9 members in live chat ~  


Closed Thread
 
LinkBack Thread Tools
Old 04-11-2007, 12:52 PM THREAD STARTER               #1 (permalink)
Tech Support
Join Date: Mar 2005
Posts: 4,944
Eric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatness
 

Member of the Month
MOTM September 2005
Save a Life Child Abuse 9/11/01 :: Never Forget Baby Health Marrow Donor Program AIDS/HIV Breast Cancer Animal Rescue Cystic Fibrosis Ethan Allen Fund Animal Cruelty Ethan Allen Fund Ethan Allen Fund Baby Health Cancer Alzheimer's Protect Our Planet Cancer Survivorship SIDS Child Abuse Diabetes Protect Our Planet Multiple Sclerosis Autism Adoption Special Olympics

Check if a URL exists.


Howdy.

This simple function will check if a url is valid (going by parse_url()) and if it's 'online' - by seeing if it returns a 302, 301, or 200 status code.

EDIT: 3/4/2008, updated to more efficient.

PHP Code:
<?php

function is_valid_url($url)
{
    
$url = @parse_url($url);
????: NamePros.com http://www.namepros.com/code/315565-check-if-a-url-exists.html

    if (!
$url)
    {
        return 
false;
    }

    
$url array_map('trim'$url);
    
$url['port'] = (!isset($url['port'])) ? 80 : (int)$url['port'];

    
$path = (isset($url['path'])) ? $url['path'] : '/';
    
$path .= (isset($url['query'])) ? "?$url[query]'';

    if (isset(
$url['host']) AND $url['host'] != gethostbyname($url['host']))
????: NamePros.com http://www.namepros.com/showthread.php?t=315565
    {
        if (
PHP_VERSION >= 5)
        {
            
$headers implode(''get_headers("$url[scheme]://$url[host]:$url[port]$path"));
        }
        else
        {
            
$fp fsockopen($url['host'], $url['port'], $errno$errstr30);

            if (!
$fp)
            {
                return 
false;
            }
            
fputs($fp"HEAD $path HTTP/1.1\r\nHost: $url[host]\r\n\r\n");
            
$headers fread($fp4096);
            
fclose($fp);
        }
        return (bool)
preg_match('#^HTTP/.*\s+[(200|301|302)]+\s#i'$headers);
    }
    return 
false;
}

?>
Last edited by SecondVersion; 03-04-2008 at 05:29 PM.
Eric is offline  
Old 04-15-2007, 05:04 PM   #2 (permalink)
Senior Member
 
bobby9101's Avatar
Join Date: Jun 2005
Posts: 1,478
bobby9101 has much to be proud ofbobby9101 has much to be proud ofbobby9101 has much to be proud ofbobby9101 has much to be proud ofbobby9101 has much to be proud ofbobby9101 has much to be proud ofbobby9101 has much to be proud ofbobby9101 has much to be proud ofbobby9101 has much to be proud of
 



I needed this a little while ago, and here is what I have:
PHP Code:
function url_exists($url) {
$headers get_headers($url);
    return 
preg_match('/^HTTP\/\d\.\d\s+(200|301|302)/'$headers[0]);
????: NamePros.com http://www.namepros.com/showthread.php?t=315565

then use:
if ($headers[0] == 1) {
code
}
bobby9101 is offline  
Old 04-15-2007, 05:17 PM THREAD STARTER               #3 (permalink)
Tech Support
Join Date: Mar 2005
Posts: 4,944
Eric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatness
 

Member of the Month
MOTM September 2005
Save a Life Child Abuse 9/11/01 :: Never Forget Baby Health Marrow Donor Program AIDS/HIV Breast Cancer Animal Rescue Cystic Fibrosis Ethan Allen Fund Animal Cruelty Ethan Allen Fund Ethan Allen Fund Baby Health Cancer Alzheimer's Protect Our Planet Cancer Survivorship SIDS Child Abuse Diabetes Protect Our Planet Multiple Sclerosis Autism Adoption Special Olympics
Originally Posted by bobby9101
I needed this a little while ago, and here is what I have:
PHP Code:
function url_exists($url) {
????: NamePros.com http://www.namepros.com/showthread.php?t=315565
$headers get_headers($url);
    return 
preg_match('/^HTTP\/\d\.\d\s+(200|301|302)/'$headers[0]);

then use:
if ($headers[0] == 1) {
code
}
Not everyone has PHP5
Eric is offline  
Old 03-04-2008, 05:28 PM THREAD STARTER               #4 (permalink)
Tech Support
Join Date: Mar 2005
Posts: 4,944
Eric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatness
 

Member of the Month
MOTM September 2005
Save a Life Child Abuse 9/11/01 :: Never Forget Baby Health Marrow Donor Program AIDS/HIV Breast Cancer Animal Rescue Cystic Fibrosis Ethan Allen Fund Animal Cruelty Ethan Allen Fund Ethan Allen Fund Baby Health Cancer Alzheimer's Protect Our Planet Cancer Survivorship SIDS Child Abuse Diabetes Protect Our Planet Multiple Sclerosis Autism Adoption Special Olympics
Updated the code, check first post.
Eric is offline  
Old 04-10-2008, 05:26 AM   #5 (permalink)
NamePros Regular
Join Date: Feb 2005
Posts: 578
abcde is a jewel in the roughabcde is a jewel in the roughabcde is a jewel in the rough
 



Originally Posted by SecondVersion
Updated the code, check first post.
I don't get it, how to use that code above? sorry, im a newbie with php
abcde is offline  
Old 04-10-2008, 04:49 PM   #6 (permalink)
Senior Member
Join Date: Apr 2005
Location: Joliet, Illinois
Posts: 1,177
RageD is a splendid one to beholdRageD is a splendid one to beholdRageD is a splendid one to beholdRageD is a splendid one to beholdRageD is a splendid one to beholdRageD is a splendid one to beholdRageD is a splendid one to beholdRageD is a splendid one to behold
 


Child Abuse
Originally Posted by abcde
I don't get it, how to use that code above? sorry, im a newbie with php
First, you need a webhost which supports PHP or to install it on your own PC. But on your own PC you need to also install a webserver and it's a real hassle so it's easier to have a webhost with this.
????: NamePros.com http://www.namepros.com/showthread.php?t=315565

Then, input this script in to a file called file.php (or whatever_you_want.php) and save and close the file.

Now in SecondVersion's script he created the function called $is_valid_url($url); so what you'll want to do to see if a URL exists: (within the same file that the function is in [or if it's included for that matter])

PHP Code:
<?php
// Test script. Function created by SecondVersion
function is_valid_url($url)
{
    
$url = @parse_url($url);

    if (!
$url)
    {
        return 
false;
    }

    
$url array_map('trim'$url);
    
$url['port'] = (!isset($url['port'])) ? 80 : (int)$url['port'];

    
$path = (isset($url['path'])) ? $url['path'] : '/';
    
$path .= (isset($url['query'])) ? "?$url[query]'';

    if (isset(
$url['host']) AND $url['host'] != gethostbyname($url['host']))
    {
        if (
PHP_VERSION >= 5)
        {
            
$headers implode(''get_headers("$url[scheme]://$url[host]:$url[port]$path"));
        }
        else
        {
            
$fp fsockopen($url['host'], $url['port'], $errno$errstr30);

            if (!
$fp)
            {
                return 
false;
????: NamePros.com http://www.namepros.com/showthread.php?t=315565
            }
            
fputs($fp"HEAD $path HTTP/1.1\r\nHost: $url[host]\r\n\r\n");
            
$headers fread($fp4096);
            
fclose($fp);
        }
        return (bool)
preg_match('#^HTTP/.*\s+[(200|301|302)]+\s#i'$headers);
    }
    return 
false;
}

if(
is_valid_url("http://namepros.com"))
{
    echo(
"w00t for Namepros.com! It's alive!");
}else{
    echo(
"Oh no! Where did NamePros go!?! :(");
}

?>
-RageD
RageD is offline  
Old 04-17-2008, 05:11 AM THREAD STARTER               #7 (permalink)
Tech Support
Join Date: Mar 2005
Posts: 4,944
Eric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatness
 

Member of the Month
MOTM September 2005
Save a Life Child Abuse 9/11/01 :: Never Forget Baby Health Marrow Donor Program AIDS/HIV Breast Cancer Animal Rescue Cystic Fibrosis Ethan Allen Fund Animal Cruelty Ethan Allen Fund Ethan Allen Fund Baby Health Cancer Alzheimer's Protect Our Planet Cancer Survivorship SIDS Child Abuse Diabetes Protect Our Planet Multiple Sclerosis Autism Adoption Special Olympics
Thanks for answering that for me, RageD.
Eric is offline  
Old 06-30-2008, 07:42 PM   #8 (permalink)
Account Suspended
Join Date: Jun 2008
Posts: 156
itnashvillecom has a spectacular aura aboutitnashvillecom has a spectacular aura about
 



why not just:

$var = file_get_contents($url);
if ($var == TRUE) {
// url is ok
} else {
// url is invalid
}
itnashvillecom is offline  
Old 06-30-2008, 08:31 PM THREAD STARTER               #9 (permalink)
Tech Support
Join Date: Mar 2005
Posts: 4,944
Eric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatnessEric Has achieved greatness
 

Member of the Month
MOTM September 2005
Save a Life Child Abuse 9/11/01 :: Never Forget Baby Health Marrow Donor Program AIDS/HIV Breast Cancer Animal Rescue Cystic Fibrosis Ethan Allen Fund Animal Cruelty Ethan Allen Fund Ethan Allen Fund Baby Health Cancer Alzheimer's Protect Our Planet Cancer Survivorship SIDS Child Abuse Diabetes Protect Our Planet Multiple Sclerosis Autism Adoption Special Olympics
Originally Posted by itnashvillecom
why not just:

$var = file_get_contents($url);
if ($var == TRUE) {
// url is ok
} else {
// url is invalid
????: NamePros.com http://www.namepros.com/showthread.php?t=315565
}
What would be the point of getting the URL's content? That'd be useless and inefficient. This function does the same without the burden of gathering content.
Eric is offline  
Old 07-11-2008, 10:33 PM   #10 (permalink)
Account Suspended
Join Date: Jun 2008
Posts: 156
itnashvillecom has a spectacular aura aboutitnashvillecom has a spectacular aura about
 



Just set max length to 1 to keep from grabbing the whole thing.. Beats using a gigantic function, overkill to me. Much less processing time and lower load.
Last edited by itnashvillecom; 07-11-2008 at 10:37 PM.
itnashvillecom is offline  
Closed Thread


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools


 
All times are GMT -7. The time now is 03:46 PM.

Domain name forum recommended by Domaining.com Powered by: vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.6.0 Ad Management plugin by RedTyger