24, July 2008

How to validate urls? - webmaster forum

 
Webdigity webmaster forums
This forum shares its ad revenue with its members!
[ Home | Help | Search | Forum's Shop | Archive | Login | Register | Webmaster Directory ]
Webdigity Webmaster Forums  >  Web Development  >  PhP
Topic: How to validate urls?
« previous next »
Pages: [1] Print

Author Topic: How to validate urls?  (Read 923 times)
I am a metal monkey!
Administrator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 7974
40801 credits
Members referred : 3



« on: Jun 23, 2006, 04:38:49 PM »

I am writing a link exchange script, and I would like to check if the URLs that people submit are valid.

Can you give me an idea how to do this?

Thanks in advance.

Trial and Error my two best teachers Cool
Join us @ facebook Visit through proxy

Last blog : MIA - Where Nick and Tim
Global Moderator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 6280
38506 credits
Members referred : 374


It's time to use PHP5!


« Reply #1 on: Jun 23, 2006, 05:00:54 PM »

try this function:
Code:
<?php
function 
check_url($url) {
$url_parts parse_url($url);
if (array_key_exists("host"$url_parts)) {
$insert_url $url_parts['host'];
$tested_host gethostbyname($this->insert_url);
if ($tested_host <> $insert_url) {
return true;
} else {
return false;
}
} else {
return false;
}
}



Last blog : 4th of July Lottery from TemplateMonster.com
I am a metal monkey!
Administrator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 7974
40801 credits
Members referred : 3



« Reply #2 on: Jun 23, 2006, 05:03:24 PM »

Wow that's great. Thanks Smiley

Trial and Error my two best teachers Cool
Join us @ facebook Visit through proxy

Last blog : MIA - Where Nick and Tim
Global Moderator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 6280
38506 credits
Members referred : 374


It's time to use PHP5!


« Reply #3 on: Jun 23, 2006, 05:04:15 PM »

Wow that's great. Thanks Smiley
It's a part of my own link exchange script
Wink


Last blog : 4th of July Lottery from TemplateMonster.com
Global Moderator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 6280
38506 credits
Members referred : 374


It's time to use PHP5!


« Reply #4 on: Jun 23, 2006, 05:06:50 PM »

and use some regex first:
Code:
<?php 
if (preg_match("/^http\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\S*)?$/i"$url)) {
  
//
}

otherwise it takes some time until the error is collected


Last blog : 4th of July Lottery from TemplateMonster.com
I am a metal monkey!
Administrator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 7974
40801 credits
Members referred : 3



« Reply #5 on: Jun 23, 2006, 05:08:46 PM »

BTW here is a version to use outside a class, with error reporting = on :

Code:
<?php
function check_url($url
{
if (preg_match("/^http\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\S*)?$/i"$url))
{
$url_parts = @parse_url($url);
if (@array_key_exists("host"$url_parts)) 
{
$insert_url $url_parts['host'];
$tested_host gethostbyname($url);
if ($tested_host <> $insert_url)
return true;
else
return false;
} else {
return false;
}
}else
return false;
 }

Thanks again Smiley

Trial and Error my two best teachers Cool
Join us @ facebook Visit through proxy

Last blog : MIA - Where Nick and Tim
What a dork
*
Posts: 16
116 credits
Members referred : 1


« Reply #6 on: Jun 24, 2006, 02:39:50 AM »

Check a url is live with socket (more secure)

Code:
<?php
function check_url_is_live($url
{
  if((
$ar=parse_url($url)) && ($fp=@fsockopen($ar[host],80,$errno,$errstr,2))) 
  { 
     
fclose($fp);
     return 
true;
  }
  else
     return 
false;
}
?>

Global Moderator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 6280
38506 credits
Members referred : 374


It's time to use PHP5!


« Reply #7 on: Jun 24, 2006, 09:04:26 AM »

@thek,

I saw this  method before but how about speed, and if the given host was redirected. And why is this moresecure please explain?

An what if the site is really busy then is an existing URL = false.

but there are many ways to rome Cheesy


Last blog : 4th of July Lottery from TemplateMonster.com
I am a metal monkey!
Administrator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 7974
40801 credits
Members referred : 3



« Reply #8 on: Jun 24, 2006, 12:12:21 PM »

What about CURL?

I guess it would be also faster than socket

Trial and Error my two best teachers Cool
Join us @ facebook Visit through proxy

Last blog : MIA - Where Nick and Tim
Global Moderator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 6280
38506 credits
Members referred : 374


It's time to use PHP5!


« Reply #9 on: Jun 24, 2006, 12:16:26 PM »

Yes Curl is the power  Cool

but again to validate an existing URL the first function is enough, only if you need to know that a URL is active you have to open the remote URL.



Last blog : 4th of July Lottery from TemplateMonster.com
What a dork
*
Posts: 16
116 credits
Members referred : 1


« Reply #10 on: Jun 24, 2006, 06:33:36 PM »

Function curl execute php stream...
Php stream use socket for url opening...
So socket is more advanced than curl!
Global Moderator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 6280
38506 credits
Members referred : 374


It's time to use PHP5!


« Reply #11 on: Jun 24, 2006, 06:44:21 PM »

Function curl execute php stream...
Php stream use socket for url opening...
So socket is more advanced than curl!


But opening the file to check if the url is valid is like shooting with canon balls on little chicken...


Last blog : 4th of July Lottery from TemplateMonster.com
What a dork
*
Posts: 16
116 credits
Members referred : 1


« Reply #12 on: Jun 24, 2006, 06:49:44 PM »

Only open a connection, like ping...
No read file data...
If ulr is down, wait only 2 seconds...
I am a metal monkey!
Administrator
Community Supporter ?
Jedai Sword Master
*****
Gender: Male
Posts: 7974
40801 credits
Members referred : 3



« Reply #13 on: Jun 25, 2006, 07:47:32 PM »

Thanks for the help guys.

Interesting thread Smiley

Trial and Error my two best teachers Cool
Join us @ facebook Visit through proxy

Last blog : MIA - Where Nick and Tim
Trackback URI for this entry : http://www.webdigity.com/trackback.php?topic=3016
Tags : php curl Bookmark this thread : Digg Del.icio.us Dzone more....

Topic sponsors:
Get a permanent link here for $1.99!


Pages: [1] Print 
Webdigity Webmaster Forums  >  Web Development  >  PhP
Topic: How to validate urls?
« previous next »
Jump to:
User Area
Welcome, Guest. Please login or register.
Did you miss your activation email?
Jul 24, 2008, 11:08:20 AM





Login with username, password and session length

Donate to our community, and get a permanent link back to your site!

Donate to our community, and get a permanent link back to your site!


Forum Statistics
Total Posts: 35.712
Total Topics: 7.376
Total Members: 3.706
Tutorials : 56
Resources : 143
Designs : 220
Latest Member: FortuneBeach

20 Guests, 5 Users online :

12 users online today:



Readers

Web Design Gallery · Whois Lookup · Pagerank · Tag Browsing · Lo-fi version · Syndication · Webmaster forum history · Advertise
Developed by HumanWorks © 2005 - 2008 Webdigity webmaster community · sublime directory
Webdigity Webmaster Forums | Powered by SMF 1.0.12. © 2001-2005, Lewis Media. All Rights Reserved.