【问题标题】:How to get the scheme when no http:// or https:// is being specified?未指定 http:// 或 https:// 时如何获取方案?
【发布时间】:2012-11-25 17:30:38
【问题描述】:

我希望用户能够在他们的公司设置对话框中输入他们的公司网站 URL,但我需要验证这一点。除了典型的清理功能外,我想检查 URL 方案是 http:// 还是 https://,而不假设用户已经输入。

我的函数已经解析输入的 url 以使用正则表达式检测方案,但我想(理想情况下)检查来自服务器 ala file_get_contents 或 parse_url 的 URL 并获取方案,但我不知道我该怎么做去做吧。

【问题讨论】:

  • 这有什么关系?假设 http。如果是 https,服务器会重定向你。

标签: php http url https


【解决方案1】:

看看parse_url()。该方案将在数组的scheme 元素中返回。

编辑 1

也接受部分 URL,parse_url() 会尽力正确解析它们。

如果 URL 中不存在该方案,则 scheme 元素将丢失。

编辑 2

正如@BenediktOlek 所说,您可以使用 cURL 来查询服务器:

$curl = curl_init();
curl_setopt_array(
    $curl,
    array(
        CURLOPT_URL            => 'http://www.example.com/',
        CURLOPT_HEADER         => TRUE,  // Output the response headers
        CURLOPT_RETURNTRANSFER => TRUE,  // Return output as a string
        CURLOPT_NOBODY         => TRUE   // Use request method "HEAD"
    )
);
$curlData = curl_exec($curl);
curl_close($curl);

如果服务器需要 HTTPS 连接并且配置正确,那么它应该返回带有 HTTPS URL 的 Location: 标头。

【讨论】:

  • 不,我已经检查过了。例如,如果您碰巧运行parse_url("www.someurl.com");,那么您将在返回数组中获得的唯一内容是path 组件,但没有返回任何方案:P
  • 好吧,如果字符串没有方案,那么您能做的最好的事情就是做出假设并将其设置为默认值。
  • 我喜欢 cURL 方法。我要测试一下。谢谢! :)
  • 返回的信息太多,但这是一种不错的方法。我想我会使用 no scheme -> http:// 规则,但感谢两者的 cURL 建议:)
  • @JulioMecaHansen 您可以使用explode("\n", $curlData) 将cURL 的响应拆分为一个数组,然后使用foreach() 循环在该数组中查找Location: 标头。然后explode(':', $strHeader)提取URL,parse_url()获取方案。
【解决方案2】:

您可以使用cURL 模块来查询服务器。但我想假设 http 是安全的。如果不允许使用 http,则正确配置的网络服务器应重定向。

More on cURL here.

【讨论】:

    【解决方案3】:

    像这样使用parse_url() 和参数PHP_URL_SCHEME

    $scheme = parse_url( $url, PHP_URL_SCHEME);
    if( !in_array( $scheme, array( 'http', 'https'))){
      // Wrong URL
    }
    // Good URL
    

    评论后:

    if( strncmp( $url, 'http://', 7)){ // 7 = strlen( 'http://')
      // Not secured
    } else if (strncmp( $url, 'https://', 8)) {
      // Secured
    } else if ( strpos($url, '://') !== false){
      // ftp://, sftp:// and other protocols
      // you may do this also by: preg_match and regexp ~^[\w]+://~i - will be more accurate
    } else {
      // any other URL, www.google.com and so on...
      // auto-assue: not secured
    }
    

    【讨论】:

    • 不,它不起作用。此外,我尝试了几种组合,www.someurl.comhttp://www.google.comhttps://www.google.com,其中falsehttphttps 是我得到的答案,但这就是我现在想要的。我的意思是,用户可以插入格式错误的 URL,但我仍然需要检查它是否处于安全或非安全连接下,只是为了使用格式正确的 URL 更新他们的数据库记录。我的意图是不是目标太高了?
    【解决方案4】:

    或者这可能有帮助?

        NSRange range = [urlName rangeOfString:@"http://"];
    //NSLog(@"found or not found");
    if (range.location != NSNotFound)
    {
        //NSLog(@"range is found");
        //range.location is start of substring
        //range.length is length of substring
    } else
    {
        //NSLog(@"range is not found");
        //urlName = @"http://"  urlName;
        //NSString *string = [NSString stringWithFormat:@"%@%@", @"Hello", @"World"];
        urlName = [NSString stringWithFormat:@"%@%@", @"http://",urlName];
        //NSLog(@"NEW urlName......%@", urlName);
    }
    

    【讨论】:

      猜你喜欢
      • 2023-04-04
      • 1970-01-01
      • 2014-10-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-22
      • 1970-01-01
      相关资源
      最近更新 更多