首页 文章

PHP获取站点URL协议 - http vs https

提问于
浏览
177

我有SSL并且不知道如何测试它是否在https下运行 . Can you tell me if this is correct?

function siteURL()
{
    $protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
    $domainName = $_SERVER['HTTP_HOST'].'/';
    return $protocol.$domainName;
}
define( 'SITE_URL', siteURL() );

Is it necessary to do it like above or can I just do it like?:

function siteURL()
{
    $protocol = 'http://';
    $domainName = $_SERVER['HTTP_HOST'].'/'
    return $protocol.$domainName;
}
define( 'SITE_URL', siteURL() );

在SSL下,即使锚标记网址使用http,服务器也不会自动将网址转换为https?是否有必要检查协议?

谢谢!

16 回答

  • 6

    这不是自动的 . 你的顶级功能看起来不错 .

  • 4

    我知道现在已经很晚了,虽然有一种更方便的方法可以解决这类问题!上面显示的解决方案非常混乱,如果有人应该检查这个,我会怎么做:

    $protocol = stripos($_SERVER['SERVER_PROTOCOL'],'https') === true ? 'https://' : 'http://';
    

    如果你不喜欢,甚至没有条件

    $protocol = strtolower(substr($_SERVER["SERVER_PROTOCOL"],0,strpos( $_SERVER["SERVER_PROTOCOL"],'/'))).'://';
    

    Have a look at $_SERVER["SERVER_PROTOCOL"]

  • 1

    这适合我

    if (isset($_SERVER['HTTPS']) &&
        ($_SERVER['HTTPS'] == 'on' || $_SERVER['HTTPS'] == 1) ||
        isset($_SERVER['HTTP_X_FORWARDED_PROTO']) &&
        $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https') {
      $protocol = 'https://';
    }
    else {
      $protocol = 'http://';
    }
    
  • 78

    一些变化:

    function siteURL() {
      $protocol = ((!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off') || 
        $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
      $domainName = $_SERVER['HTTP_HOST'];
      return $protocol.$domainName;
    }
    
  • 58

    简短的方法

    $scheme = $_SERVER['REQUEST_SCHEME'] . '://';
    
  • 2

    因为根据我的测试端口号不是一个好习惯,我的解决方案是:

    define('HTTPS', isset($_SERVER['HTTPS']) && filter_var($_SERVER['HTTPS'], FILTER_VALIDATE_BOOLEAN));
    

    如果设置了 $_SERVER['HTTPS'] ,则 HTTPS 常量返回 TRUE ,并且等于"1","true","on"或"yes" . 否则返回FALSE .

  • 6

    对于除IIS以外的任何系统,这足以定义站点自身URL:

    $siteURL='http'.(empty($_SERVER['HTTPS'])?'':'s').'://'.$_SERVER['HTTP_HOST'].'/';
    

    要么

    $siteURL='http'.(empty($_SERVER['HTTPS'])?'':'s').'://'.$_SERVER['SERVER_NAME'].'/';
    

    取决于你到底想要什么:HTTP_HOST vs. SERVER_NAME

  • 2

    使用此服务器变量来获取协议详细信息:

    $scheme = $_SERVER['REQUEST_SCHEME'] . '://';
     echo $scheme; //it gives http:// or https://
    

    请注意,此服务器变量不可靠 . 欲了解更多信息,请访问:Is $_SERVER['REQUEST_SCHEME'] reliable?

  • 1

    如果是代理, SERVER_PORT 可能没有给出正确的值,所以这对我有用 -

    $protocol = ((!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off') || $_SERVER['SERVER_PORT'] == 443 || $_SERVER['HTTP_X_FORWARDED_PORT'] == 443) ? "https://" : "http://"
    
  • 4

    使用Rid Iculous的答案制作了一个函数,该答案适用于我的系统 .

    function site_protocol() {
        if(isset($_SERVER['HTTPS']) && ($_SERVER['HTTPS'] == 'on' || $_SERVER['HTTPS'] == 1) || isset($_SERVER['HTTP_X_FORWARDED_PROTO']) &&  $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https')  return $protocol = 'https://'; else return $protocol = 'http://';
    }
    

    希望能帮助到你

  • 1
    $protocal = 'http';
    if ($_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https' || $_SERVER['HTTPS'] == 'on') {$protocal = 'https';}
    
    
    echo $protocal;
    
  • 16

    我已经测试了most voted answe r和 didn't work for me ,我最终使用了:

    $protocol = isset($_SERVER['HTTPS']) ? 'https://' : 'http://';
    
  • 66

    我知道这是一个老问题,但我今天遇到了这个,因为我需要在我的网站上测试这个 . 看来上面的答案是不必要的复杂 . 要 Build 站点协议,您所要做的就是测试 $_SERVER['HTTPS']

    如果协议使用HTTPS,则 $_SERVER['HTTPS'] 将返回'on' . 如果不是,变量将保持为空 . 例如: // test if HTTPS is being used. If it is, the echo will return '$SSL_test: on'. If not HTTPS, '$SSL_test' will remain empty.
    `$SSL_test = $_SERVER['HTTPS']; echo '<p>$SSL_test: '.$SSL_test.'</p>';

    if($SSL_test == true) {
    echo 'You're using SSL';
    } else {
    echo 'You're not using SSL';
    }`
    `` 您可以使用上述内容轻松,干净地测试HTTPS并相应地实施 . :)

  • 25

    从CodeIgniter中提取:

    if ( ! function_exists('is_https'))
    {
        /**
         * Is HTTPS?
         *
         * Determines if the application is accessed via an encrypted
         * (HTTPS) connection.
         *
         * @return  bool
         */
        function is_https()
        {
            if ( ! empty($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) !== 'off')
            {
                return TRUE;
            }
            elseif (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && strtolower($_SERVER['HTTP_X_FORWARDED_PROTO']) === 'https')
            {
                return TRUE;
            }
            elseif ( ! empty($_SERVER['HTTP_FRONT_END_HTTPS']) && strtolower($_SERVER['HTTP_FRONT_END_HTTPS']) !== 'off')
            {
                return TRUE;
            }
    
            return FALSE;
        }
    }
    
  • 0

    这是https或http的最佳解决方案,使用此方法:

    <?php
    $protocol = '//';  
    $site_url = $protocol.$_SERVER["HTTP_HOST"];
    ?>
    

    但无法显示https或http,因此它仅用于链接您的网站内容,如图片等 .

    如果想要以https重定向您的网站,请在.htaccess文件中添加此代码:

    <IfModule mod_rewrite.c>
     RewriteCond %{HTTP:CF-Visitor} '"scheme":"http"'
     RewriteRule ^(.*)$ https://www.your-domain.com$1 [L]
    </IfModule>
    

    使用您的dowmain名称更改 www.your-domain.com .

  • 0

    这是我怎么做的......这是Rid Iculous的答案的一个版本 shorthand if else 版本...

    $protocol = isset($_SERVER['HTTPS']) && ($_SERVER['HTTPS'] === 'on' || $_SERVER['HTTPS'] === 1) || isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] === 'https' ? 'https' : 'http';
    

相关问题