How do you get current browser URL with PHP

Once again PHP does not supply any core function for this. And its not that its too difficult to achieve this.

Just write a simple 4 liner function and you are done.

here is what i came up with, I had a requirement to return the entire URL as base64 encoded string so I’ve made that part of the function below

Updated function includes port number. Use the function below (recommended)

 

As you can see that URI and HOST are two global values for global array $_SERVER so you have full control over what part of your URL you want encoding on if at all.

By default function will return plain URL without any encoding. Encoding is handy when you are passing your URL in Ajax request’s query string.

Hope this helps!

 

Cheers

 

.

VN:F [1.9.22_1171]
Rating: 4.0/5 (2 votes cast)
VN:F [1.9.22_1171]
Rating: +1 (from 1 vote)
How do you get current browser URL with PHP, 4.0 out of 5 based on 2 ratings

Speak Your Mind

*

CommentLuv badge