PHP - 在 URL 中用 https 替换 http

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5289272/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 20:52:59  来源:igfitidea点击:

PHP - replace http with https in URL

phphtmlcheckbox

提问by Kevin Jung

I am trying to figure out how to add s after HTTP once a user checks a box in the html form.

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

I have in my PHP,

我在我的 PHP 中有,

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

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

So basically, when the user checks a box with the name="https" i want to add s to $url's http making it https://google.com.

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

I have little knowledge on PHP and if someone can explain to me how to go about doing this, this would be really helpful! thanks.

我对 PHP 知之甚少,如果有人可以向我解释如何去做,这将非常有帮助!谢谢。

回答by Jakub Hampl

$url = preg_replace("/^http:/i", "https:", $url);

回答by tivnet

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

回答by Felix Kling

One way:

单程:

$url = '%s//google.com';
$protocol = 'http:';

if(!isset($_POST['https'])) { 
    $protocol = 'https:';
}

$url = sprintf($url, $protocol);

回答by vbence

I don't know on how many pages you want this to happen onward the user checks the box, but one answer is JavaScript and the basetag.

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

With the basetag, you can force a different origin, what your relative URL-s will be resolved against.

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

I you are using it ina form, and the user ticks the checkbox them sumbits the form, all other pages will be viewed from the httpssite, so you can use relative URL-s everywhere, just insert a different basetag when the user wants to change the site form or to http(s).

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

回答by FaZ

A solution that doesn't replace urls which contain other urls, for instance http://foo.com/redirect-to/http://newfoo.com

不替换包含其他网址的网址的解决方案,例如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'] ));
}

回答by NaxBuda

When you consider case-insensitive, then use str_ireplacefunction.

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

Example:

例子:

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

Alternatively, incase you ant to replace URL scheme in CDN files. Example:

或者,如果你用蚂蚁来替换 CDN 文件中的 URL 方案。例子:

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

This... (with 'https:')

这... (与'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>

Becomes... ('https:'has been replaced)

变成... '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>

回答by Chetan Patel

$count = 1;
$url = str_replace("http://", "https://", $url, $count);

Note :Passing 1 directly will throw fatal error (Fatal error: Only variables can be passed by reference) so you have to pass it last parameter by reference.

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