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

Read Time1 Minute, 9 Seconds

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.

$this->load->helper('url');

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

$currentUrl = current_url();

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

echo $currentUrl;

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

if (strpos(current_url(), "cat")!==false){ echo "hello"}else{echo "bello"}

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.

0 0

About Post Author

Kavidu Jayakody

Kavidu Dilshan Jayakody is the founder and the author of KDJ Blog. He is a skilled web developer and graphic designer. In addition, he has earned his degree BSc (Hons) Computing, UK. He is working as a tutor of an institute and as a part-time blogger, he will help you all to make your problems solved.
0 %
Happy
0 %
Sad
0 %
Excited
0 %
Angry
0 %
Surprise

Leave a Comment

Your email address will not be published. Required fields are marked *

KDJ Singlish 1.1.0 for PC/Android/Google Chrome
Download Now

Scroll to Top