Best way to allow users to add their website url

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ninethousandfeet
Forum Contributor
Posts: 130
Joined: Tue Mar 10, 2009 4:56 pm

Best way to allow users to add their website url

Post by ninethousandfeet »

Hi,

I want to allow my users to add their website url (if they have one) to their profile on my site. Right now, I have it set up like:
<input type="text" value="http://" />

Couple of problems with this:
1) a user can delete the http:// and type away and then when it is displayed in their profile, the link will not work properly
2) if they enter their website and then go to update it, the update form also has the 'http://' at the start of the value attribute so it appears twice, like http://http://userssite.com

Is there a better way to display the http:// inside of the input box without it being editable? And some what to make certain that the user's website appears as a correct link to their website every time (http:// in front of it, obviously if they enter an incorrect website that is their fault).

Thank you!
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: Best way to allow users to add their website url

Post by jackpf »

You could just check if http://is at the beginning like so:

Code: Select all

if(substr($url, 0, 7) == 'http://')//it's valid
If you don't want them to type "http://" themselves, just display it as text in front of the box, and then prepend it when you're processing it.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: Best way to allow users to add their website url

Post by John Cartwright »

You could also just str_replace() the http:// away (accept the input with or without it), then as jackpf suggests prepend it when saving/displaying.
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: Best way to allow users to add their website url

Post by jackpf »

But there could be a valid "http://" somewhere in the query string 8O

I'd go with substr tbh. Or maybe preg_replace, to only replace it at the start of the string.
ninethousandfeet
Forum Contributor
Posts: 130
Joined: Tue Mar 10, 2009 4:56 pm

Re: Best way to allow users to add their website url

Post by ninethousandfeet »

okay, thank you both for the help. i will play around with it and let you know how it turns out.

thank you!
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: Best way to allow users to add their website url

Post by jackpf »

Cool. Good luck.
Post Reply