如何在codeigniter中读取当前域的URL?

beg*_*ner 1 codeigniter

假设我有一个当前的网址显示 http://localhost.com/welcome/blah_blah,从这个网址我希望只能http://localhost.com/ 进入一个页面.

这该怎么做??

我知道echo current_url();给出了完整的网址.但我只想要http://localhost.com排除controller_name,clasname等.

预期产出

如果我的base_url = http://www.exaple.com/folder¤t_url = http://exaple.com/folder/class/method/blah_blah,那么我想要的域名exaple.com不是www.exaple.com !

我正面临一个问题.

AJR*_*ing 5

您可以使用base_url()URL帮助程序.它会返回没有index.php的网站域config.php

http://ellislab.com/codeigniter/user-guide/helpers/url_helper.html

更新后的更新:

如果您只想要域名而没有子域名(www.),那么您发布的额外信息可以正常使用:

$this->load->helper('url');
$url_parts = parse_url(current_url());
echo str_replace('www.', '', $url_parts['host']);
Run Code Online (Sandbox Code Playgroud)

但是,如果您仍然需要http或https,那么这将完成工作:

$this->load->helper('url');
$url_parts = parse_url(current_url());
echo $url_parts['scheme'] . '://' . str_replace('www.', '', $url_parts['host']);
Run Code Online (Sandbox Code Playgroud)

然而,这只会删除www.子域名,但如果有其他可能的话,可以很容易地进行调整.