Codeigniter – How To Get & Check Current URL in Controller or View

Hello all, welcome to KDJ Guru blog. From this post, I will teach you how to get the current URL from the Codeigniter website and how to check it with if function. I hope, this tutorial will be very helpful for you. This trick only works with CodeIgniter developers. If you don’t know about Codeigniter, sorry this is not for you.

First you must load the URL helper in autoload.php (application->config->autoload.php) or in the any controller or any view. if you are going to load URL helper in a view or a controller, use the following code line.

[code]$this->load->helper(‘url’); [/code]

Now we just have to get the current URL. for that, I’m making a variable and store the url in that variable.

[code]$currentUrl = current_url();[/code]

Now i can print it anywhere that i want like following.

[code]echo $currentUrl;[/code]

Anyways, now i need to check that URL with if function.

[code]if (strpos(current_url(), “cat”)!==false){ echo “hello”}else{echo “bello”}[/code]

Above code will check whether the URL includes “cat” word or not. if yes, it will print hello. else it will print bello.

However, I hope this tutorial will help you for your issue. If you have any question, please leave your comment below and I will reply as soon as possible. Have a nice day.