views:

251

answers:

7

I have people posting their website address but variations are posted such as:

When I link to an address without http:// it takes the link as internal

<a href="theirsite.com">their site</a>

sending people to something like: http://mysite.com/thiersite.com

Another option I've tried is linking to something like mysite.com/?link=theirsite.com - This way I can do some link tracking etc then redirect people to the link but it has the same problem:

//do some tracking etc here
$link =$_GET['link'];
header("Location: $link");
+1  A: 

I would provide some validation or sanitation. Use a regex to see if http:// starts with it. If it doesn't, either throw an validation error or put http:// at the start.

Daniel A. White
+1  A: 
if not "://" in users_url:
    users_url = "http://" + users_url

...or equivalent, in the language of your choice.

RichieHindle
+1  A: 

You could use regular expressions to test input

Regex exp = new Regex(
    @"http://(www\.)?([^\.]+)\.com",
    RegexOptions.IgnoreCase);
Jon
Hopefully the .com is just an example :D
OregonGhost
+3  A: 

put "http://" in the field by default, then validate the URL with something like

if(eregi("^((http|https)://)?([[:alnum:]-])+(\.)([[:alnum:]]){2,4}([[:alnum:]/+=%&_.~?-]*)$", stripslashes(trim($_POST['link'])))){
    //link is valid
}

if link does not validate, just print them a message saying "the link you entered is invalid, make sure it starts with 'http://'"

Tony
I like how this will also take care of the cases when people write "no" or "not yet" or stuff like that.
Fredrik Mörk
+1 For the default value for the URL field. But I wouldn’t use `eregi`.
Gumbo
good point with people posting "no" or "not yet"...
Peter
No need to use regular expressions in this case. PHP has built in URL validation using the "filter" extension. See my answer below.
The Pixel Developer
ereg is dead, you should use PCRE instead.
vartec
+2  A: 

I would use something like this:

$link = str_replace(array("\r", "\n"), '', trim($link));
if (!preg_match('/^https?:\/\//', $link)) {
    $link = 'http://'.$link;
}
header('Location: '.$link);

Another way would be the parse_url function to parse the given URL, see what parts are missing and add them.

Gumbo
might need to consider trailing space as noted by Elazar and also there was a small typo repalce. Cheers.
Peter
Have you noticed the `trim`?
Gumbo
oops, silly me!
Peter
+2  A: 

Please note, there's a real difference between www.site.com and site.com, usually both works, but on some website each leads to a different path (some badly defined website won't work without the www for instance). So You can't always prepend 'www' to the input.

Another note, do handle prepending space, so that ' http://' would not be prepended with additional http://.

My Javascript Regex based solution

'http://'+field.replace(/^ *http:\/\//,'')

You can verify that on the client size, just put a code in similar spirit on the onSubmit of your form.

Elazar Leibovich
+5  A: 

No need to use regular expressions here. PHP has URL validation built in.

Filter Var

var_dump((bool) filter_var('http://www.website.com', FILTER_VALIDATE_URL, FILTER_FLAG_HOST_REQUIRED));
var_dump((bool) filter_var('http://website.com', FILTER_VALIDATE_URL, FILTER_FLAG_HOST_REQUIRED));
var_dump((bool) filter_var('www.website.com', FILTER_VALIDATE_URL, FILTER_FLAG_HOST_REQUIRED));
var_dump((bool) filter_var('website.com', FILTER_VALIDATE_URL, FILTER_FLAG_HOST_REQUIRED));

Output

bool(true)
bool(true)
bool(false)
bool(false)

Please do not jump straight to regular expressions for validation, PHP has a lot of methods built in to deal with these scenarios.

-Mathew

The Pixel Developer
But regular expressions are language independent.
Gumbo
To be fair, he posted it with a PHP tag and I've given him a PHP answer. It's a non-issue.
The Pixel Developer