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 (27) Add New Comment
Dragon Storm cheat proof2024-09-16 5:16 AM

I do not even understand how I stopped up here, but I thought this submit was once great. I do not recognize who you are but definitely you are going to a well-known blogger if you happen to are not already. Cheers!

Reply
spam2024-09-15 5:13 AM

Way cool! Some very valid points! I appreciate you writing this write-up and also the rest of the website is really good.

Reply
child porn2024-09-15 1:17 AM

Thanks for every other informative blog. Where else could I am getting that kind of information written in such an ideal way? I've a mission that I'm just now running on, and I have been on the look out for such info.

Reply
Filler2024-09-14 11:52 AM

I think the admin of this site is really working hard in support of his site, since here every material is quality based information.

Reply
ikaria juice2024-09-12 11:18 PM

I feel this is one of the such a lot vital information for me. And i'm happy studying your article. But wanna remark on few basic issues, The site style is ideal, the articles is actually excellent : D. Good task, cheers

Reply
child porn2024-09-12 3:02 AM

I read this paragraph fully on the topic of the resemblance of most up-to-date and previous technologies, it's remarkable article.

Reply
buy viagra online2024-09-12 12:32 AM

Keep this going please, great job!

Reply
child porn2024-09-11 10:48 PM

Hey there! This post could not be written any better! Reading through this post reminds me of my old room mate! He always kept chatting about this. I will forward this page to him. Pretty sure he will have a good read. Many thanks for sharing!

Reply
erotic massage Glasgow2024-09-11 9:57 PM

Post writing is also a fun, if you be acquainted with then you can write if not it is complicated to write.

Reply
bahis siteleri2024-09-10 7:47 PM

Howdy are using Wordpress for your blog platform? I'm new to the blog world but I'm trying to get started and create my own. Do you need any coding expertise to make your own blog? Any help would be really appreciated!

Reply
slot deposit qris2024-09-10 4:21 AM

It's going to be ending of mine day, except before end I am reading this great post to increase my know-how.

Reply
Vayam-NGO2024-09-09 1:41 PM

Right away I am going away to do my breakfast, once having my breakfast coming over again to read further news.

Reply
free use mom porn2024-09-08 6:56 PM

Les productions présentes dans le catalogue d'un casino en ligne représentent un critère de choix important à ne pas négliger.

Reply
aldi frozen pork joint2024-09-08 1:28 AM

After looking over a number of the blog posts on your web page, I seriously appreciate your technique of blogging. I saved as a favorite it to my bookmark site list and will be checking back soon. Please visit my web site as well and tell me how you feel.

Reply
aldi frozen pork joint2024-09-08 1:28 AM

After looking over a number of the blog posts on your web page, I seriously appreciate your technique of blogging. I saved as a favorite it to my bookmark site list and will be checking back soon. Please visit my web site as well and tell me how you feel.

Reply
aldi frozen pork joint2024-09-08 1:27 AM

After looking over a number of the blog posts on your web page, I seriously appreciate your technique of blogging. I saved as a favorite it to my bookmark site list and will be checking back soon. Please visit my web site as well and tell me how you feel.

Reply
pearson airport parking lot2024-09-07 9:16 PM

as soon as passengers inform you of personal arrival, pearson airport parking lot lot can proceed to the terminal. how long can you will parking at terminal?

Reply
off site parking bwi2024-09-07 3:55 PM

Parking outside the airport offer additional amenities that will not be found at airfield: 24-hour security on the ground, car washes, valet parking, indoor parking and off site parking bwi airport - are just part of them.

Reply
See it here2024-09-06 6:11 PM

Terrific article! That is the kind of information that are supposed to be shared across the net. Shame on the seek engines for no longer positioning this submit higher! Come on over and seek advice from my site . Thanks =)

Reply
jeeter carts2024-09-06 2:39 PM

When I initially left a comment I seem to have clicked on the -Notify me when new comments are added- checkbox and from now on each time a comment is added I receive 4 emails with the same comment. Perhaps there is a means you can remove me from that service? Thank you!

Reply
ord longterm parking2024-09-06 1:18 PM

decision is up to users! length the points that can receive for long-ord longterm parking are not determined. in what circumstances advantages of xpresspark?

Reply
fort lauderdale daily parking2024-09-06 3:25 AM

one hour of free parking area for person with disabilities in the garage, which is discharged daily/hourly, will facilitate access to the fort lauderdale daily parking lot.

Reply
the hidden wiki2024-09-05 8:41 PM

first of all, this is control quality. within a year, approximately in 2013, silk road was captured, and 27-year-old pirate Roberts, the hidden wiki, was imprisoned.

Reply
child sex2024-09-05 9:03 AM

Good day I am so glad I found your blog page, I really found you by error, while I was researching on Bing for something else, Nonetheless I am here now and would just like to say kudos for a marvelous post and a all round exciting blog (I also love the theme/design), I don’t have time to read through it all at the minute but I have saved it and also included your RSS feeds, so when I have time I will be back to read a lot more, Please do keep up the superb b.

Reply

What i don't realize is actually how you are no longer actually much more smartly-liked than you might be right now. You're very intelligent. You understand thus significantly relating to this topic, made me in my view imagine it from a lot of various angles. Its like men and women don't seem to be involved unless it is something to do with Woman gaga! Your personal stuffs great. At all times deal with it up!

Reply

what makes the smartpark more, than by 80/70/60/50/40% cheaper, providing substantial benefit for philadelphia airport long term parking closed trips.

Reply
Ceylon Cinnamon powder2024-09-04 6:56 PM

Wow, awesome blog layout! How long have you been running a blog for? you made blogging look easy. The whole look of your website is fantastic, as neatly as the content!

Reply