-2

I use php and I have an input that the user types in a link to his website. The link should appear for others to view it or open it. I'm testing the input and the problem is the link does not work unless I types either http:// or https:// before the mentioned URL and I think some one might just write www.foo.com without writing the hhtp:// . The link automatically substitutes the http:// or https://with the root of my website ( or at least that's what I guess ), because I'm working with XAMP and the URL appears as localhost/MyFooProjectFile/www.foo.com .

How to make link work without the protocols or substitute the none mentioned protocol with the right one before my user's link ?

Note : I know the question might seem stupid (cause who is the idiot who thinks a link could work without protocol), but what makes me wonder is I write in the address bar of my browser www.foo.co and it would take me there, without me menthionioning the protocols.

PLease note that I do not have the code in the time of writing the question.

LF00
  • 27,015
  • 29
  • 156
  • 295
MaryBaker
  • 667
  • 1
  • 5
  • 8
  • Just check if the link they entered contains the protocol or not. If it doesn't, add it. – rjdown Jul 22 '16 at 23:17
  • Your browser actually adds "http://" in front of any URL you enter in the address bar, since http:// is the most common protocol in a browser (most browser doesn't show the protocol, but if you copy paste the URL in the address bar, you get the protocol as well). When it comes to links on sites, the browser have no idea if you mean a relative or an absolute url unless you explicitly tell it. That's why you need the protocol in links. – M. Eriksson Jul 22 '16 at 23:23
  • You can solve it by doing what @rjdown suggested. – M. Eriksson Jul 22 '16 at 23:25

1 Answers1

0

Duplicated With HTML5 url input validation assume url starts with http://

Trust in the urls user inputed is in danger. You have to filter the urls, parse them, construct them, test them, then embed them in your page.

Not all the URL must have "//"

Community
  • 1
  • 1
LF00
  • 27,015
  • 29
  • 156
  • 295
  • 1
    Well, since the OP said "user types in a link to his **website**", you can assume (or rather require) that there is a "protocol://" as a prefix. And if there isn't, just add "http://". If they require any other protocol, just let them explicitly write it. You're right about validation though. That's a given for all user inputs that should be either stored or displayed. – M. Eriksson Jul 22 '16 at 23:49