27

Tried URI::uri_string() but can't get it to work with the base_url.

URL: http://localhost/dropbox/derrek/shopredux/ahahaha/hihihi
Returns: dropbox/derrek/shopredux/ahahaha/hihihi

but http://localhost/dropbox/derrek/shopredux/ just returns an empty string.

I want the first call to return "ahahaha/hihihi" and the second to return "". Is there such a function?

11 Answers 11

68
// For current url
echo base_url(uri_string());
1
  • 3
    Loading the URL helper is needed.
    – Terry Lin
    Nov 30, 2015 at 13:59
38

If url helper is loaded, use

current_url();

will be better

2
  • which helper ? url helper ? this returns a full path : what bout like to get --> welcome/home , users/login type of urls ? Jan 27, 2015 at 12:20
  • I got the wrong point, sorry. but $_SERVER['REQUEST_URI'] may simply satisfy the requirement above Jan 28, 2015 at 17:23
13

Try to use "uri" segments like:

$this->uri->segment(5);   //To get 'ahahaha'
$this->uri->segment(6);   //To get 'hihihi

form your first URL...You get '' from second URl also for segment(5),segment(6) also because they are empty.

Every segment function counts starts form localhost as '1' and symultaneous segments

1
  • FYI - I found when using CodeIgniter 2.1.3 that the segments start at 0, not 1 as mentioned in the answer. E.g. $this->uri->segment(1) for "http://example.com/first/second" is "first".
    – user9645
    Oct 30, 2013 at 18:57
13

For the parameter or without parameter URLs Use this :

Method 1:

 $currentURL = current_url(); //for simple URL
 $params = $_SERVER['QUERY_STRING']; //for parameters
 $fullURL = $currentURL . '?' . $params; //full URL with parameter

Method 2:

$full_url = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";

Method 3:

base_url(uri_string());
10

I see that this post is old. But in version CI v3 here is the answer:

echo $this->uri->uri_string();

Thanks

0
5
 //if you want to get parameter from url use:
 parse_str($_SERVER['QUERY_STRING'], $_GET);
 //then you can use:
 if(isset($_GET["par"])){
      echo $_GET["par"];
 }
 //if you want to get current page url use:
 $current_url = current_url();
3

Running Latest Code Igniter 3.10

$this->load->helper('uri'); // or you can autoload it in config

print base_url($this->uri->uri_string());
0

I don't know if there is such a function, but with $this->uri->uri_to_assoc() you get an associative array from the $_GET parameters. With this, and the controller you are in, you know how the URL looks like. In you above URL this would mean you would be in the controller dropbox and the array would be something like this:

array("derrek" => "shopredux", "ahahaha" => "hihihi");

With this you should be able to make such a function on your own.

0

In CI v3, you can try:

function partial_uri($start = 0) {
    return join('/',array_slice(get_instance()->uri->segment_array(), $start));
}

This will drop the number of URL segments specified by the $start argument. If your URL is http://localhost/dropbox/derrek/shopredux/ahahaha/hihihi, then:

partial_uri(3);  # returns "ahahaha/hihihi"
0

you can use the some Codeigniter functions and some core functions and make combination to achieve your URL with query string. I found solution of this problem.

base_url($this->uri->uri_string()).strrchr($_SERVER['REQUEST_URI'], "?");

and if you loaded URL helper so you can also do this current_url().strrchr($_SERVER['REQUEST_URI'], "?");

0
<?php $currentMenu =  $this->uri->segment(2); ?>
<ul>
   <li class="nav-item <?= ($currentMenu == 'dashboard') ?  'active' : '' ?>"> 
       <a href="<?= site_url('/admin/dashboard'); ?>" class="nav-link"><i data- 
         feather="pie-chart"></i> Dashboard</a>

   </li>
</ul

this is work for me

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.