【问题标题】:PHP - replace http with https in URLPHP - 在 URL 中用 https 替换 http
【发布时间】:2011-07-14 10:34:55
【问题描述】:

一旦用户选中 html 表单中的框,我正在尝试弄清楚如何在 HTTP 之后添加 s。

我的 PHP 中有,

$url = 'http://google.com';

if(!isset($_POST['https'])) { 
  //something here
}

所以基本上,当用户选中名称为“https”的框时,我想将 s 添加到 $url 的 http 使其成为 https://google.com

我对 PHP 知之甚少,如果有人可以向我解释如何进行此操作,这将非常有帮助!谢谢。

【问题讨论】:

    标签: php html checkbox


    【解决方案1】:

    当您考虑不区分大小写时,请使用str_ireplace 函数。

    例子:

    $url = str_ireplace( 'http://', 'https://', $url );
    

    或者,如果您想替换 CDN 文件中的 URL 方案。 例子:

    $url = str_ireplace( 'http:', 'https:', $url );
    

    这... (与'https:'

    <link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/themes/smoothness/jquery-ui.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script>
    

    变成... 'https:' 已被替换)

    <link rel="stylesheet" href="//ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/themes/smoothness/jquery-ui.css">
    <script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script>
    

    【讨论】:

      【解决方案2】:

      不替换包含其他网址的网址的解决方案,例如 http://foo.com/redirect-to/http://newfoo.com

      $desiredScheme = "http"; // convert to this scheme;
      $parsedRedirectUri = parse_url($myCurrentUrl);
      if($parsedRedirectUri['scheme'] !== $desiredScheme) {
          $myCurrentUrl= substr_replace($myCurrentUrl, $desiredScheme, 0, strlen( $parsedRedirectUri['scheme'] ));
      }
      

      【讨论】:

        【解决方案3】:
        $url = preg_replace("/^http:/i", "https:", $url);
        

        【讨论】:

        • 很好的解决方案,但它更适合大小写敏感:$url = preg_replace("/^http:/i", "https:", $url);
        • 我测试了这个解决方案,不幸的是这不起作用,我用“str_replace”尝试了下一个解决方案并得到了结果。
        • str_replace 对我不起作用,但原来的起作用了。也非常感谢这个伟大的解决方案,通过即时自定义的 wordpress 主题解决了一个巨大的问题,否则会强制混合内容通过 SSL 并中断 SSL 连接。
        【解决方案4】:
        $count = 1;
        $url = str_replace("http://", "https://", $url, $count);
        

        注意:直接传1会抛出致命错误(致命错误:只能通过引用传递变量)所以必须通过引用传递最后一个参数。

        【讨论】:

        • 它会抛出一个错误,因为$count 将只包含执行的替换次数。它不是用来设置边界或任何东西的参数。
        【解决方案5】:
        $url = str_replace( 'http://', 'https://', $url );
        

        【讨论】:

          【解决方案6】:

          我不知道您希望在多少页面上发生这种情况,然后用户选中该框,但一个答案是 JavaScript 和 base 标记。

          使用 base 标签,您可以强制使用不同的来源,即您的相对 URL-s 将被解析的对象。

          我在表单中使用它,并且用户勾选了他们填写表单的复选框,所有其他页面都将从 https 站点查看,因此您可以在任何地方使用相对 URL-s,当用户想要更改站点表单或更改为 http(s) 时,只需插入不同的 base 标记即可。

          【讨论】:

            【解决方案7】:

            一种方式:

            $url = '%s//google.com';
            $protocol = 'http:';
            
            if(!isset($_POST['https'])) { 
                $protocol = 'https:';
            }
            
            $url = sprintf($url, $protocol);
            

            【讨论】:

            • 感谢代码,但我无法分解 $url,因为它正在用于需要整个 url 的先前函数中。会不会有别的办法?
            • @usr122212:见@edorian 的回答。
            猜你喜欢
            • 1970-01-01
            • 2018-12-15
            • 2016-09-06
            • 2015-02-15
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2014-06-04
            相关资源
            最近更新 更多