1

Say i have a current url showing http://localhost.com/welcome/blah_blah ,from this url i am expecting to get only http://localhost.com/ into a page.

How to do this??

I know echo current_url(); gives full url. But i want only the http://localhost.com excluding controller_name,clasname etc.

Expected output

if my base_url=http://www.exaple.com/folder & current_url=http://exaple.com/folder/class/method/blah_blah ,then i want domain name as exaple.com NOT the www.exaple.com !

I am facing a issue .

Community
  • 1
  • 1
beginner
  • 665
  • 6
  • 14
  • 31

4 Answers4

5

You can use base_url() from the URL helper. It returns the website domain without the index.php as configured in your config.php

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

Updated after additional information:

With the extra information you posted if you simply want the domain name ONLY and no subdomains (www.) then this will work fine:

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

However if you still needed http or https then this would do the job:

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

This however would only remove www. subdomains, but could easily be adapted if there were potentially any others.

AJReading
  • 1,193
  • 20
  • 35
  • Of course you must load the URL helper first `$this->load->helper('url');` – AJReading Mar 29 '14 at 06:47
  • 1
    Sorry! Not helpful. suppose if i have set $base_url="www.example.com/project" then this logic will not work. I asked the current url.... – beginner Mar 29 '14 at 07:18
  • Actually you did not mention this in your initial questions before the edit. I've updated my answer with a another solution. – AJReading Mar 30 '14 at 03:36
3

Add the following function to the url_helper.php,Then call getDomain() like you are calling base_url()

  function getDomain()
  {
    $CI =& get_instance();
    return preg_replace("/^[\w]{2,6}:\/\/([\w\d\.\-]+).*$/","$1", $CI->config->slash_item('base_url'));
  }
Shijin TR
  • 7,516
  • 10
  • 55
  • 122
  • i tried to show using `echo $this->getDomain();` but it shows error `Fatal error: Call to undefined method calculate::getDomain() in D:\xampp\htdocs\ecc\application\controllers\calculate.php on line 33` ?? – beginner Mar 29 '14 at 07:44
  • simply call getDomain() like base_url(),Because you have added that function in a helper – Shijin TR Mar 29 '14 at 08:16
  • i tested your code but its not what i want. You have basically customizes the `base_url()` , similarly i want to customize the `current_url()`. How to do this? For Example: if my base_url=`http://www.exaple.com/folder` & current_url=`http://exaple.com/folder/class/method/blah_blah` ,then i want domain name as `exaple.com` NOT the `www.exaple.com` ! – beginner Mar 29 '14 at 14:56
  • @badtimings Oh,sorry.I think it will help you.we can find any other methods soon – Shijin TR Mar 29 '14 at 18:22
  • oh it's working. use *$this->getDomain();* if using in class otherwise call it directly – Faizan Anwer Ali Rupani Dec 22 '17 at 09:29
2

In php you get it by this function

function siteURL() {
    $protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
    $domainName = $_SERVER['HTTP_HOST'] . '/';
    return $protocol . $domainName;
}
hemc4
  • 1,623
  • 3
  • 18
  • 32
0

Simple use the following code and you will get you current domain name. either its http or https

 $protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
    $domainName = $_SERVER['HTTP_HOST'] . '/';
    $url =  $protocol . $domainName;
Waseem shah
  • 400
  • 5
  • 13