You are here: Blog » Code Snippets » Get URL of Current Page With PHP

Get URL of Current Page With PHP

p

This Get URL of Current Page With PHP code snippet page will describe the methods involved in obtaining the current page url with php. There are many times when it comes in handy to be able to get the current URL via PHP. There are also various components on the URL that you may want to get, I will outline for you the techniques involved in getting the most popular URL components, and also show you how to combine them all to get the full url with php.

Get The Protocol (HTTP vs HTTPS)

This would echo http:// or https://, whichever is set for the current page.

Get Server Name

This would echo the server name, subdomains would be included if there are any. For this it would echo www.clevelandwebdeveloper.com.

Get URI

This would echo everything after your server name. If your URL was www.mysite.com/some/page, it would echo ‘/some/page’.

Get URI Without Querystring

If your URL was www.mysite.com/some/page?id=23, the previous snippet would echo ‘/some/page?id=23’. This snippet would echo ‘/some/page’.

Putting it all together to Get URL of Current Page With PHP

If you wanted the full URL without the querystring, substitute $_SERVER[“REQUEST_URI”] with strtok($_SERVER[“REQUEST_URI”],’?’)

This entry was posted in Code Snippets and tagged by .

Author: justin saad

Justin Saad is first and foremost a husband and a father of four. He is also a web developer and designer who has been making websites for over twenty years. His company is called The Motech Network and his website is ClevelandWebDeveloper.com. In addition to building websites, Justin makes WordPress plugins and likes writing tutorials to help others learn about various aspects of web development.

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">