Help - Search - Members - Calendar
Full Version: [PHP]URL Verification
Pixel2Life Forum > Member Tutorials and Requests > Forum Tutorial Archives > PHP Tutorials
sephet
URL Verification Tutorial

Written By Ben Kippax ::: http://www.layouts4u.co.uk :::


This tutorial will show you how to verify if a URL exists or if its a 404 error.



Step 1



<p><form action="verify.php" method="get">

URl (Don't include http://) <input type="text" name="url"><br/>

<input type="submit" value="send"><br/>
</form></p>



call this file "form_verify.php"



Step 2



<?
$up = @fsockopen("$url", 80, $errno, $errstr, 30);
if($up)
{
echo "$url Can Be found";
}
else
{
echo "<b>Oops! <br>$url Cant Be Found</b>";
}
?>


Call This file "verify.php"





Now, all you have to do is include form_verify.php in a page or keep it as a stand alone script and just link it. It's as simple as that.

For a working example please go to http://www.sephet.co.uk/tuts/form_verify.php


I Hope you enjoy the tutorial.

Regards,
Ben
brent
can there be a way to check if they input the http:// part? just to make sure your visitors read directions...
Jaymz
QUOTE(brent @ Aug 8 2005, 12:10 AM)
can there be a way to check if they input the http:// part? just to make sure your visitors read directions...

Replace Step 2 with this smile.gif

CODE
<?
$url = $_POST['url'];

//REMOVE HTTP
$url = str_replace('http://', '', $_POST['url']);

$up = @fsockopen("$url", 80, $errno, $errstr, 30);
if($up)
{
echo "$url Can Be found";
}
else
{
echo "<b>Oops! <br>$url Cant Be Found</b>";
}
?>


Just in case a host has disabled REGISTER_GLOBALS, step two should originally be this:

CODE
<?

//DEFINE $URL
$url = $_POST['url'];

//GET RID OF THE HTTP
$url = str_replace('http://', '', $_POST['url']);

//TEST URL
$up = @fsockopen("$url", 80, $errno, $errstr, 30);

//IF URL FOUND
if($up)
{
echo "$url Can Be found";
}

//IF 404
else
{

//ECHO RESULTS
echo "<b>Oops! <br>$url Cant Be Found</b>";
}
?>
brent
ok, but shouldn't there be a way to test if they put in the http:// part or not? i'm sorry, just wondering....i'm going to be doing a link submission site/section soon and probably use this tut, plus others to help create this project...thanks for the help and reply
sephet
ok i think this can be acheived using a simple "if" statement. Basically its what Jaymz said

CODE
$url = $_POST['url'];

$url = str_replace('http://', '', $_POST['url']);


that should be the first two lines of the code.

Really this is just saying that if the url contains http:// reaplace it with "nothing".
HaloprO
To determine wheather they entered http:// at the beginning you could use a simple regex
CODE
<?php
$search = "(http):\/\/(.*?)";
if (eregi($search, $_POST['url'])) {
  //success
} else {
 die('You did not enter http:// at the beginning of your url');
}
?>

The way sephet described all you would have to do is put http:// anywhere in the string, like www.google.com/http://
Would result www.google.com/
Jaymz
The way I posted would eliminate http://'s anywhere, further securing the script from installing remote files on a server, as I have seen this done on my host and it's a pain in the butt.
HaloprO
I don't think that's what he wanted to do though, You could just have it disable the use of some extensions..Or display < and > so they can't use XSS, plus I messed up on my script up there, pm me and I'll give you one that works
Jaymz
QUOTE(HaloprO @ Aug 8 2005, 09:46 PM)
I don't think that's what he wanted to do though, You could just have it disable the use of some extensions..Or display < and > so they can't use XSS, plus I messed up on my script up there, pm me and I'll give you one that works

The way I posted would quietly get rid of the http:// if it existed, or continue on it's merry way smile.gif
HaloprO
I know that...I'm not a noob, I just don't think he wants to get rid of it
EDIT: my bad he does want to get rid of it, sorry jaymz tongue.gif
brent
lol....yall are probably going to hate me....i was just wondering how to do it, not really wanting to take your code from you...hehe but thanks for all the info though...and sorry if it caused a little bickering.... victory.gif
Jaymz
You can take my code, I made it for your needs bigwink.gif
sephet
that was a battle to explain that, lolz
downplay
thnx for posting this smile.gif
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2009 Invision Power Services, Inc.