简体   繁体   English

有关网址验证的快速问题

[英]a quick question about url validation

I don't really want to use curl or anything complex like that. 我真的不想使用curl或类似的东西。 I was looking for a simple way to check if the url (User will enter the url and it is then stored in my MySQL database) is valid and that it is a valid url ie the domain exists. 我在寻找一种简单的方法来检查url(用户将输入该url,然后将其存储在我的MySQL数据库中)是否有效,并且它是有效的url,即域存在。 Is fopen a good solution? fopen是一个好的解决方案吗?

Any tutorials or tips on this would be greatly appreciated. 任何对此的教程或技巧将不胜感激。

CURL isn't that complex. CURL并不那么复杂。 To get the http status of a url: 要获取网址的http状态:

$ch = curl_init('http://www.yahoo.com/');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_exec($ch);
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if($status >= 200 && $status < 400) {
    // url is valid
}

A regex or filter would validate that the url is valid but not that it exists. 正则表达式或过滤器将验证该URL有效,但不存在。 A DNS lookup validates that the domain exists, but not the url. DNS查找将验证域是否存在,而不是URL。

First, validate that it is a valid URL using filter_var() : 首先,使用filter_var()确认它是有效的URL:

<?php

$url = "http://www.example.com/foo.php";

if(!filter_var($url, FILTER_VALIDATE_URL))
{
  die('Invalid URL');
}

Next, parse the URL with parse_url() and ensure that it is HTTP(S): 接下来,使用parse_url()解析URL并确保它是HTTP(S):

$p_url = parse_url($url);
if(!$p_url) // couldn't parse URL, since parse_url() cannot recognize it
{
  die('Invalid URL');
}
if($p_url['scheme'] != 'http' && $p_url['scheme'] != 'https')
{
  die('Invalid protocol (only HTTP(S) is supported)');
}

Lastly, check that the host exists and that you can connect to it. 最后,检查主机是否存在并且可以连接到它。 I choose to use fsockopen() here, since it would check the hostname and port, while not actually sending an HTTP request. 我选择在这里使用fsockopen() ,因为它会检查主机名和端口,而实际上并不发送HTTP请求。

$fp = fsockopen($p_url['host'], (isset($p_url['port']) ? $p_url['port'] : 80));
if($fp)
{
  echo 'Valid URL';
  fclose($fp); // Remember to close the file pointer
}else{
  echo 'Invalid server';
}

Note that you might want to avoid using this method (depending on what you want your application to do), as if the server is down, this would result in Invalid server , even though the server might exist. 请注意,您可能希望避免使用此方法(取决于您希望应用程序执行的操作),就像服务器关闭一样,即使服务器可能存在,这也会导致Invalid server An alternative solution that will only check the hostname from the DNS servers, and not connect at all, is using gethostbyaddr() and gethostbyname() : 只能从DNS服务器检查主机名而根本不连接的替代解决方案是使用gethostbyaddr()gethostbyname()

if(@gethostbyaddr($p_url['host'])) // IP addresses passes this test
{
  echo 'Valid URL';
}else{
  $host = $p_url['host'];
  $addr = gethostbyname($host);
  if(!$addr) // Invalid domain name
  {
    echo 'Invalid domain name';
  }else if($host == $addr) // Domain name could not be resolved (i.e. does not exist)
  {
    echo 'Invalid domain name';
  }else{
    echo 'Valid URL';
  }
}

Links: 链接:

You should try filter_var 您应该尝试filter_var

<?php
$url = "http://www.example.com";

if(!filter_var($url, FILTER_VALIDATE_URL))
{
  echo "URL is not valid";
}
else
{
  echo "URL is valid";
}
?> 

Read the filter_var manual 阅读filter_var手册

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM