How to Get Full URL With Query Strings in Codeigniter?

If you use current_url(), it will return the full URL (including segments) of the page being currently viewed.But if your URL contains query string or parameters, it won't include those query strings in the full URL.
For example, if your URL was this:
http://example.com/blog?q=test&name=facebook
The function would return:
http://example.com/blog //The url without the query strings.
Solution:
You can modify the Codeigniter's core URL helper or extend the URL helper to your needs. I prefer to add new function for this keeping the native function as it is.
Create a MY_url_helper.php inside 'application/helpers' folder.
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

function current_full_url()
{
    $CI =& get_instance();

    $url = $CI->config->site_url($CI->uri->uri_string());
    return $_SERVER['QUERY_STRING'] ? $url.'?'.$_SERVER['QUERY_STRING'] : $url;
}


/* End of file MY_url_helper.php */
/* Location: ./application/helpers/MY_url_helper.php */
Now you can use current_full_url() to view Full URL with query strings. The function would return:
http://example.com/blog?q=test&name=facebook

Comments

Post a Comment

Popular Posts