确保 http 出现在网址中
我有人发布他们的网站地址,但发布了变体,例如:
- irsite.com
- www.theirsite.com
- http://theirsite.com< /a>
- http://www.theirsite.com
当我链接到没有 http:// 的地址时将链接视为内部
<a href="theirsite.com">their site</a>
链接,将人们发送至以下内容:http://mysite.com/thiersite.com
我尝试过的另一个选择是链接到类似 mysite.com/?link=theirsite.com 的内容 - 这样我可以进行一些链接跟踪等,然后将人们重定向到该链接,但它有同样的问题:
//do some tracking etc here
$link =$_GET['link'];
header("Location: $link");
I have people posting their website address but variations are posted such as:
- theirsite.com
- www.theirsite.com
- http://theirsite.com
- http://www.theirsite.com
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");
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
这里不需要使用正则表达式。 PHP 内置了 URL 验证。
过滤变量
输出
Please do不是直接跳转到正则表达式进行验证,PHP 内置了很多方法来处理这些场景。
-马修
No need to use regular expressions here. PHP has URL validation built in.
Filter Var
Output
Please do not jump straight to regular expressions for validation, PHP has a lot of methods built in to deal with these scenarios.
-Mathew
默认情况下将“http://”放入字段中,然后使用类似的内容验证 URL,
如果链接未验证,只需向其打印一条消息,说明“您输入的链接无效,请确保它以“http://”开头’”
put "http://" in the field by default, then validate the URL with something like
if link does not validate, just print them a message saying "the link you entered is invalid, make sure it starts with 'http://'"
请注意,
www.site.com
和site.com
之间存在真正的区别,通常两者都有效,但在某些网站上,每个网站都会导致不同的路径(有些网站定义错误)例如,如果没有www
,网站将无法运行)。 所以你不能总是在输入前面加上“www”。另请注意,请处理前置空格,以便
' http://'
不会在前面添加额外的http://
。我的基于 Javascript Regex 的解决方案
您可以验证客户端大小,只需在表单的
onSubmit
上添加类似的代码即可。Please note, there's a real difference between
www.site.com
andsite.com
, usually both works, but on some website each leads to a different path (some badly defined website won't work without thewww
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 additionalhttp://
.My Javascript Regex based solution
You can verify that on the client size, just put a code in similar spirit on the
onSubmit
of your form.我会使用这样的东西:
另一种方法是
parse_url
函数解析给定的 URL,查看缺少哪些部分并添加它们。I would use something like this:
Another way would be the
parse_url
function to parse the given URL, see what parts are missing and add them.我会提供一些验证或卫生。 使用正则表达式查看 http:// 是否以其开头。 如果没有,请抛出验证错误或将 http:// 放在开头。
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.
...或同等语言,以您选择的语言。
...or equivalent, in the language of your choice.
您可以使用正则表达式来测试输入
You could use regular expressions to test input