Hi! This post will show you how to get current url and base url in codeigniter. In codeigniter the term, Current URL refers to the complete URL of the current webpage being loaded and Base URL is the domain name or path to the application root (on localhost).
PHP Code igniter comes with URL Helper to assist you in working with application URLs. This helper contains bunch of methods to access URLs and in order to use them, you must first load the URL helper either inside controller or auto load it using autoload.php file.
To load URL Helper use this statement,
$this->load->helper('url');
Or add this below line to 'config/autoload.php'
$autoload['helper'] = array('url');
CodeIgniter Get Current URL:
You have to use current_url()
function to retrieve the current url in codeigniter. This function can be used anywhere inside codeignitor controller, model or view file and returns the full url of the webpage being viewed including the URI Segments (like controller, method name etc).
For example if your current url is,
localhost/themeshop/index.php/wpthemes/shopify
Then current_url() function will return,
localhost/themeshop/index.php/wpthemes/shopify
Usage:
echo current_url();
// output:
// http://localhost/themeshop/index.php/wpthemes/shopify
CodeIgniter Get Base URL:
On the other hand base url is the site's base url (eg., http://example.com) mentioned in 'config/config.php' file. Unlike current url, base url doesn't return URI segments.
To get base url in codeigniter you have to use the base_url()
function.
Usage:
echo base_url();
// output:
// http://localhost/themeshop/
Also the base_url() function takes up a parameter of string or array. So you can pass URI Segments to this function which will then be appended at the end of site's base url and returned.
Usage:
<img src="<?php echo base_url("images/site-logo.png")?>" />
Also Read: Retrieve Data from Database in Codeigniter & AJAX
Don't Miss: How to Get Client IP Address in PHP Codeigniter
So that explains about retrieving current url and base url in php codeigniter. Meet you in the next post :)
0 Response to "CodeIgniter Get Current URL & Base URL"
Posting Komentar