您当前的位置:易学堂 > EM&TP

Emlog全站开启https教程

时间:2019-02-12 16:51:32

https支持已集成到Emlog 6.0,以下内容仅适用于Emlog 5.x用户,在进行修改之前,请先确定服务器已正确配置https,并且备份所有文件,防止遇到兼容性问题。

4089372.jpg

1. /include/lib/option.php

请将以下内容粘贴到 get function 的 default 判断分支之前 (在Emlog 5.3.1下是第43行)

case 'blogurl':
    return realUrl();
    break;

2. /include/lib/function.base.php

请将以下内容粘贴到文件的末尾

/**
 * 获取当前访问的base url
 */
function realUrl() {
    static $real_url = NULL;
    
    if ($real_url !== NULL) {
        return $real_url;
    }
 
    $emlog_path = EMLOG_ROOT . DIRECTORY_SEPARATOR;
    $script_path = pathinfo($_SERVER['SCRIPT_NAME'], PATHINFO_DIRNAME);
    $script_path = str_replace('\\', '/', $script_path);
    $path_element = explode('/', $script_path);
    
    $this_match = '';
    $best_match = '';
    
    $current_deep = 0;
    $max_deep = count($path_element);
    
    while($current_deep < $max_deep) {
        $this_match = $this_match . $path_element[$current_deep] . DIRECTORY_SEPARATOR;
        
        if (substr($emlog_path, strlen($this_match) * (-1)) === $this_match) {
            $best_match = $this_match;
        }
        
        $current_deep++;
    }
    
    $best_match = str_replace(DIRECTORY_SEPARATOR, '/', $best_match);
    $real_url  = $_SERVER['HTTPS'] === 'on' ? 'https://' : 'http://';
    $real_url .= $_SERVER["SERVER_NAME"];
    $real_url .= in_array($_SERVER['SERVER_PORT'], array(80, 443)) ? '' : ':' . $_SERVER['SERVER_PORT'];
    $real_url .= $best_match;
    
    return $real_url;
}

3. /init.php

请用以下代码覆盖同名的define (在Emlog 5.3.1下是第39行)

define('DYNAMIC_BLOGURL', Option::get("blogurl"));

好了,大功告成,试试用https访问吧!

附加:如果要http访问就强制转向https的话,就在你的模板下的header.php中增加以下代码(不唯一):

if(!((isset($_SERVER['HTTPS'])&&$_SERVER['HTTPS']=='on')||(isset($_SERVER['HTTP_X_FORWARDED_PROTO'])&&$_SERVER['HTTP_X_FORWARDED_PROTO']=='https'))){
    Header("HTTP/1.1 301 Moved Permanently");
    header('Location: https://'.$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI']);
}
标签: HTTPSEmlog