Put the responsibility back on the user to write a valid URL, and alert them to invalid URLs.
Having said that, you could use parse_url() or some complex regular expressions to determine if the URL is valid, or you could just test the URL.
I found a version of this on codewalkers.com:
<?
// the supplied URL $url = 'http://www.yourURL.com';
// strip 'http://' from the start $url = eregi_replace("^http://",'',trim($url));
// test the URL // it's important that you dont put http:// or it will not work // the 2nd paramater is how long to test the URL b4 timing out $found = fsockopen($url, 10);
// act on it
if($found)
{
echo "URL http://{$url} could not be found, thus is considered invalid";
}
?>
There's also plenty more here: http://www.google.com/search?q=PHP+URL+validation&ie=UTF-8&oe=UTF-8
Seriously, I know it's great to learn everything from scratch, but the wheel has already been invented... you just need to make sure it's the right kind of wheel for your needs :)
Try out a few different approaches, test, test, test, test, and see what works for you.
Justin
On Saturday, July 19, 2003, at 07:54 AM, -{ Rene Brehmer }- wrote:
Hi gang
Finally I've got the time to finish up my guestbook script ... so little left that I can actually see the light at the end of the tunnel now..
The guestbook data is stored in a text file. When opening the guestbook, this file is parsed and the named fields are used to construct different HTML structures according to which optional information signers have entered.
One of the optional fields is an URL, and rather than enforcing a specific format, I'd like to have the script, in as much as possible, to automagically correctify the URL as far as possible without reaching too far into guessing and assumption...
I mean, I want the script to automagically add http:// when needed, turn slashes the right way, and remove excess double slashes ... but I'm uncertain as to what else I can/should make it do automatically, and what I shouldn't ...
Also, I'm not sure which characters and structures are actually permitted/required in a "legal" URL, so I'm not sure what I should make it look for to detect an impossible URL ...
Anyone able to offer some help here?
I know I could've probably just picked up someone's script out there ... but I'm trying to do it on my own, as I figure I'd learn more that way ...
Rene -- Rene Brehmer aka Metalbunny
http://metalbunny.net/ References, tools, and other useful stuff...
-- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
--- [This E-mail scanned for viruses]
-- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php