site_url() with HTTPS prefix

2019-09-02 03:12发布

问题:

*UPDATE

When I use site_url() it returns http://mysite.com

Is there a function or an option to get this to return the site URL with HTTPS ?

Example

So site_url( array('use_https' => 'on') ) to get https:mysite.com

回答1:

Not specifically. But you could write your own function or just use:

https://<?php echo $_SERVER['SERVER_NAME']; ?>

or

function url_https($base_url)
{
    return str_replace('http://', 'https://', $base_url);
}


回答2:

1 Hack is to change the codeignite definition of the base url to just // ... // will use what ever protocol is in use right now.

if you use https://example.com your links will use https if you use http://example.com your links will use http

/* Location: ./system/core/Config.php */
if (isset($_SERVER['HTTP_HOST']) && preg_match('/^((\[[0-9a-f:]+\])|(\d{1,3}(\.\d{1,3}){3})|[a-z0-9\-\.]+)(:\d+)?$/i', $_SERVER['HTTP_HOST']))
{
    $base_url = /*(is_https() ? 'https' : 'http').'://'.*/"//".
                $_SERVER['HTTP_HOST'].substr($_SERVER['SCRIPT_NAME'], 0, strpos($_SERVER['SCRIPT_NAME'], basename($_SERVER['SCRIPT_FILENAME'])));
}
else
{
    $base_url = 'http://localhost/';
}


标签: codeigniter