[codeigniter] CodeIgniter -> Get current URL relative to base url

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?

This question is related to codeigniter function url

The answer is


// For current url
echo base_url(uri_string());

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"

Running Latest Code Igniter 3.10

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

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

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'], "?");


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.


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


If url helper is loaded, use

current_url();

will be better


 //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();

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

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

Thanks


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());

Examples related to codeigniter

DataTables: Cannot read property 'length' of undefined How to set time zone in codeigniter? php codeigniter count rows CodeIgniter: 404 Page Not Found on Live Server Only variable references should be returned by reference - Codeigniter How to pass a parameter like title, summary and image in a Facebook sharer URL How to JOIN three tables in Codeigniter how to get the base url in javascript How to get id from URL in codeigniter? How to disable PHP Error reporting in CodeIgniter?

Examples related to function

$http.get(...).success is not a function Function to calculate R2 (R-squared) in R How to Call a Function inside a Render in React/Jsx How does Python return multiple values from a function? Default optional parameter in Swift function How to have multiple conditions for one if statement in python Uncaught TypeError: .indexOf is not a function Proper use of const for defining functions in JavaScript Run php function on button click includes() not working in all browsers

Examples related to url

What is the difference between URL parameters and query strings? Allow Access-Control-Allow-Origin header using HTML5 fetch API File URL "Not allowed to load local resource" in the Internet Browser Slack URL to open a channel from browser Getting absolute URLs using ASP.NET Core How do I load an HTTP URL with App Transport Security enabled in iOS 9? Adding form action in html in laravel React-router urls don't work when refreshing or writing manually URL for public Amazon S3 bucket How can I append a query parameter to an existing URL?