2018-12-10 Pujan Niroula 1 Minute(s) Read

Get Current Page URL in PHP

img-thumbnail current url php.jpg

Sometime during web development period we struggle on little things such as getting current URL. I also struggled to print current URL while coding this site.

We will use reserved variable in PHP named $_SERVER. 

Current URL in PHP

It's easy to get current URL. You can use code below to get current page url in php

$uri = htmlspecialchars($_SERVER['REQUEST_URI']);
$host = $_SERVER['HTTP_HOST'];
if(isset($_SERVER['HTTPS'])){
    $http = 'https://';
} else{
    $http = 'http://';
};
$current_url = $http.$host.$uri;
echo $current_url;
?>

Explanation of Code:

$_SERVER['REQUEST_URI'] return file path just like /current-url-php.php  . I used htmlspecialchars() function to just to protect script from XSS(Cross Site Scripting).

Now we got file path, $_SERVER['HTTP_HOST'] will return the host name like pujann.com.np . I don't have to use htmlspecialchars here because it's server generated. 

The if statement is written to detect the http:// or https://. $_SERVER['HTTPS'] returns a non-empty value if the script was queried through the HTTPS protocol.

Finally we collect all three variable together and print it.

You can use $_SERVER['SCRIPT_NAME'] instead of $_SERVER['REQUEST_URI'] if you just want to get just script URL. $_SERVER['REQUEST_URI'] will display GET parameter also.

Cheers!!!

 

Tags:php, how to,
Comments (21) Add New Comment
JtkMxa2024-06-21 8:22 AM

Thanks for sharing your thoughts on current url. Regards

Reply
Big Bass Bonanza Casino2024-06-19 11:36 PM

this is a way to achieve success, which generates a bet Big Bass Bonanza Casino, on such nuance that they are experience in the sphere emotions and earnings, and also to which a runner pilot must proportional and flexible.

Reply
porn2024-06-19 5:00 AM

Remarkable things here. I'm very satisfied to see your post. Thanks a lot and I am looking forward to touch you. Will you kindly drop me a mail?

Reply
hd porn2024-06-17 5:35 AM

Hello There. I discovered your blog using msn. That is a really smartly written article. I will be sure to bookmark it and return to learn more of your useful information. Thank you for the post. I will definitely comeback.

Reply
Bart2024-06-16 7:34 PM

Hello i am kavin, its my first occasion to commenting anyplace, when i read this article i thought i could also create comment due to this brilliant post.

Reply
porn2024-06-14 11:05 PM

Thanks for the auspicious writeup. It in truth was a leisure account it. Glance advanced to more introduced agreeable from you! By the way, how could we keep up a correspondence?

Reply
diplomaxose2024-06-14 3:57 AM

?????? ???????? ??????????? ??? ????????? ???????????? (?????) ?? ????????? ?????? ?????? ??????? ??????? ? ????? ???????. ?????? ??????: ?????? ??????? ??? ?????? ??????????? ?? ??????. ????? ??????, ?? ???????????? ??????, ??????? ?? ?????? ??????? ??????? ??????? ? ????? ???. ? ????? ?????? ????? 100 ??????????? ???????????? ??????? ??????? ?????? ? ???????. ? ??????????? ?? ?????? ??????? ????????? ??? ??? ???????? ????????? ? ????? ??? ?? ????? ?????????????. - interestbook.ru

Reply
Toney2024-06-13 11:14 AM

I am in fact delighted to glance at this weblog posts which consists of lots of valuable facts, thanks for providing these kinds of data.

Reply
Patty2024-06-13 7:14 AM

What's Taking place i'm new to this, I stumbled upon this I've discovered It absolutely useful and it has helped me out loads. I am hoping to contribute & assist different users like its aided me. Great job.

Reply
porn2024-06-12 4:14 PM

Wow, this post is nice, my sister is analyzing these kinds of things, therefore I am going to inform her.

Reply
reletis nerev usunob emened2024-06-12 3:56 PM

Spot on with this write-up, I truly think this website needs a great deal more attention. I'll probably be back again to read more, thanks for the info!

Reply
Mkrdqe2024-06-12 3:20 PM

It's the best time to make some plans for the future and it's time to be happy. I've read this post and if I could I desire to suggest you few interesting things or suggestions. Perhaps you can write next articles referring to this article. I desire to read more things about it!

Reply
amazon review2024-06-12 10:55 AM

I quite like reading through an article that will make men and women think. Also, many thanks for allowing for me to comment!

Reply
EarnestGolla2024-06-12 3:38 AM

I'm gone to tell my little brother, that he should also pay a quick visit this web site on regular basis to obtain updated from most recent news. skazka.g-talk.ru/viewtopic.php?f=1&t=1679&p=1705?  rem.4nmv.ru/forum/profile.php?action=show&member=5100?  apartamentospuntum.com/nuestros-clientes?  lolipopnews.ru/page/9?  mailorderbridesfinder.com/? 

Reply
ireletis onisac2024-06-11 7:20 PM

I like the valuable info you supply on your articles. I will bookmark your blog and take a look at again right here frequently. I am quite sure I will be told many new stuff right here! Good luck for the next!

Reply
FobertsPalk2024-06-10 9:01 AM

Hi, this weekend is pleasant designed for me, as this time i am reading this impressive informative paragraph here at my house. cn-agent.com/c_feedback/index.asp?page=10?  unirun.ru/page/5/?  delighta.ru/mesta/~shownews/3252/index.html?  forum.pokexgames.pl/member.php?action=profile&uid=45672?  courses.comet.ucar.edu/tag/index.php?tc=1&tag=%D0%93%D0%B0%D1%80%D0%B0%D0%BD%D1%82%D0%B8%D1%8F%20%D0%BF%D1%80%D0%B5%D1%81%D1%82%D0%B8%D0%B6%D0%BD%D0%BE%D0%B3%D0%BE%20%D1%82%D1%80%D1%83%D0%B4%D0%BE%D1%83%D1%81%D1%82%D1%80%D0%BE%D0%B9%D1%81%D1%82%D0%B2%D0%B0&lang=ko? 

Reply
Ismaeleromo2024-06-09 9:45 PM

What's up, yup this piece of writing is really pleasant and I have learned lot of things from it concerning blogging. thanks. onlinekinospace.ru/page/8?  nipponsword.ru/profile.php?lookup=23413?  1001cruise.ru/carnival-cruises/114-news/762-liner-royal-princess-will-be-released-in-the-first-flight.html?  log??tervez?©s.hu/logok/kalibri?  import-moto.com/users/88?wid=3643? 

Reply
StephenAmave2024-06-09 11:58 AM

Wonderful post but I was wanting to know if you could write a litte more on this topic? I'd be very thankful if you could elaborate a little bit more. Appreciate it! rkiyosaki.ru/discussion/8833/?  medhansh.net/blog/category/traffic-violations/?  tritattoo.com/tattoo-ideas/unique-matching-tattoos-for-couples/?  ik21v.ru/?  politikforum.ru/member.php?u=13387008? 

Reply
SlxOdo2024-06-09 12:48 AM

Hi, I think your site might be having browser compatibility issues. When I look at your blog site in Opera, it looks fine but when opening in Internet Explorer, it has some overlapping. I just wanted to give you a quick heads up! Other then that, very good blog!

Reply
fctaht2024-06-09 12:44 AM

Wow that was odd. I just wrote an incredibly long comment but after I clicked submit my comment didn't appear. Grrrr... well I'm not writing all that over again. Anyway, just wanted to say fantastic blog!

Reply
OLanekix2024-06-08 3:13 PM

Remarkable! Its really remarkable post, I have got much clear idea about from this paragraph. newmedtime.ru/page/12?  beta.getonvibe.net/blogs/246/Why-is-the-popularity-of-universities-constantly-declining-nowadays?  zurbazar.ru/auto/ssangyong/?auto/ssangyong&Gorod=7/index.html?  bohn.ru/kapec-putinu/?  tivi24h.com/2011/01/xem-bong-online-bong-truc-tuyen-kenh.html? 

Reply