PHP - 在URL中将http替换为https [英] PHP - replace http with https in URL

查看:3189
本文介绍了PHP - 在URL中将http替换为https的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我在我的PHP中,

我尝试找出如何在HTTP后一旦用户检查html表单中的框, >

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

if(!isset($ _ POST ['https'])){
//这里的
}

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



我对PHP了解不多,如果有人可以向我解释如何去这样做,这将是真正有用的!感谢。

解决方案


我只是使用一个简单的 str_replace()

  $ url = str_replace(http,https,$ url,1); 

第四个参数1指示它只替换http一次,打破您的网址。





无法删除,因为它已被接受, href =http://stackoverflow.com/a/5289289/285578> @Jakub Hampl's answer。


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

I have in my 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.

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.

解决方案

I'd just use a simple str_replace()

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

The 4th parameter "1" tells it to only replace "http" one time so it doesn't break your url.

Can't delete this because it's accepted but see @Jakub Hampl's answer.

这篇关于PHP - 在URL中将http替换为https的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆