判断项目中当前url是http还是https

发布于 2024-10-17 09:05:15 字数 262 浏览 1 评论 0原文

我有一个 Zend 框架的网站。这里我想识别当前的URL是包含HTTPS还是HTTP?我使用了以下代码

if($_SERVER['HTTPS']==on){ echo "something";}else{ echo "something other";}

,但结果不正确。有没有其他方法可以识别这个? 另外我还有一个问题。 如何使用php获取完整的当前url(包括HTTP/HTTPS)?

请帮助我

提前致谢

I have a website in Zend framework. Here I want to identify whether the current URL contains HTTPS or HTTP? I have used the following code

if($_SERVER['HTTPS']==on){ echo "something";}else{ echo "something other";}

But the result is not correct. Is there is any other way to identify this?
Also I have one more question.
How to get complete current url (including HTTP/HTTPS) using php?

Please help me

Thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

梦里寻她 2024-10-24 09:05:16
if(isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == "on") { 
    echo "something";
} else { 
    echo "something other";
}

请注意 on 应该是一个 string 。

if(isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == "on") { 
    echo "something";
} else { 
    echo "something other";
}

notice the on should be a string .

呆头 2024-10-24 09:05:16

您可以使用 Zend Framework 中已定义的方法,而不是显式使用 $_SERVER 超全局变量。

要确定连接是 HTTP 还是 HTTPS(此代码应进入您的控制器):

if ( $this->getRequest()->isSecure() ) { echo 'https'; } else { echo 'http'; }

要获取完整的当前 url:

$this->getRequest()->getScheme() . '://' . $this->getRequest()->getHttpHost() . $this->getRequest()->getRequestUri();

You could use methods that are already defined in Zend Framework instead of explicitly using $_SERVER superglobals.

To determine if the connection is HTTP or HTTPS (this code should go into your controller):

if ( $this->getRequest()->isSecure() ) { echo 'https'; } else { echo 'http'; }

To get complete current url:

$this->getRequest()->getScheme() . '://' . $this->getRequest()->getHttpHost() . $this->getRequest()->getRequestUri();
錯遇了你 2024-10-24 09:05:16

更好的检查方法是

if (isset($_SERVER['HTTPS']) && $_SEREVER['HTTPS'] != 'off')
{
  //connection is secure do something
}
else
{
  //http is used
}

按照手册中所述

如果脚本设置为非空值
通过HTTPS查询
协议。

注意:请注意,在 IIS 中使用 ISAPI 时,如果

请求不是通过 HTTPS 发出的
协议。

The better way to check is

if (isset($_SERVER['HTTPS']) && $_SEREVER['HTTPS'] != 'off')
{
  //connection is secure do something
}
else
{
  //http is used
}

As stated in manual

Set to a non-empty value if the script
was queried through the HTTPS
protocol.

Note: Note that when using ISAPI with IIS, the value will be off if the

request was not made through the HTTPS
protocol.

牵你的手,一向走下去 2024-10-24 09:05:16

您需要修复检查它应该是

if ($_SERVER['HTTPS'] == 'on')

或尝试以下功能

if(detect_ssl()){ echo "something";}else{ echo "something other";}

function detect_ssl() {
return ($_SERVER['HTTPS'] == 'on' || $_SERVER['HTTPS'] == 1 || $_SERVER['SERVER_PORT'] == 443)
}

you need to fix check it should be

if ($_SERVER['HTTPS'] == 'on')

or try following function

if(detect_ssl()){ echo "something";}else{ echo "something other";}

function detect_ssl() {
return ($_SERVER['HTTPS'] == 'on' || $_SERVER['HTTPS'] == 1 || $_SERVER['SERVER_PORT'] == 443)
}
故事与诗 2024-10-24 09:05:16

这将检查您是否使用 https 或 http 并输出当前 url。

$https = ((!empty($_SERVER['HTTPS'])) && ($_SERVER['HTTPS'] != 'off')) ? true : false;

if($https) {
    $url = "https://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
} else {
    $url = "http://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
}

This will both check if you're using https or http and output the current url.

$https = ((!empty($_SERVER['HTTPS'])) && ($_SERVER['HTTPS'] != 'off')) ? true : false;

if($https) {
    $url = "https://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
} else {
    $url = "http://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文