{"id":585,"date":"2012-10-16T18:03:27","date_gmt":"2012-10-16T18:03:27","guid":{"rendered":"http:\/\/ecodebook.wordpress.com\/2012\/10\/16\/redirect\/"},"modified":"2012-10-16T18:03:27","modified_gmt":"2012-10-16T18:03:27","slug":"how-to-get-the-current-page-url","status":"publish","type":"post","link":"https:\/\/blog.mycarts.shop\/?p=585","title":{"rendered":"PHP: How to Get the Current Page URL"},"content":{"rendered":"<p>Sometimes, you might want to get the current page URL that is shown in the browser URL window. For example if you want to let your visitors submit a blog post to Digg you need to get that same exact URL. There are plenty of other reasons as well. Here is how you can do that.<\/p>\n<p>Add the following code to a page:<\/p>\n<p>[code language=&#8221;php&#8221;]<br \/>\nfunction curPageURL() {<br \/>\n $pageURL = &#8216;http&#8217;;<br \/>\n if ($_SERVER[&quot;HTTPS&quot;] == &quot;on&quot;) {$pageURL .= &quot;s&quot;;}<br \/>\n $pageURL .= &quot;:\/\/&quot;;<br \/>\n if ($_SERVER[&quot;SERVER_PORT&quot;] != &quot;80&quot;) {<br \/>\n  $pageURL .= $_SERVER[&quot;SERVER_NAME&quot;].&quot;:&quot;.$_SERVER[&quot;SERVER_PORT&quot;].$_SERVER[&quot;REQUEST_URI&quot;];<br \/>\n } else {<br \/>\n  $pageURL .= $_SERVER[&quot;SERVER_NAME&quot;].$_SERVER[&quot;REQUEST_URI&quot;];<br \/>\n }<br \/>\n return $pageURL;<br \/>\n}<br \/>\n[\/code]<\/p>\n<p>You can now get the current page URL using the line:<\/p>\n<p>[code language=&#8221;php&#8221;]<br \/>\necho curPageURL();<br \/>\n[\/code]<\/p>\n<p>Sometimes it is needed to get the page name only. The following example shows how to do it:<\/p>\n<p>[code language=&#8221;php&#8221;]<br \/>\nfunction curPageName() {<br \/>\n return substr($_SERVER[&quot;SCRIPT_NAME&quot;],strrpos($_SERVER[&quot;SCRIPT_NAME&quot;],&quot;\/&quot;)+1);<br \/>\n}<\/p>\n<p>echo &quot;The current page name is &quot;.curPageName();<br \/>\n[\/code]<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Sometimes, you might want to get the current page URL that is shown in the browser URL window. For example if you want to let your visitors submit a blog post to Digg you need to get that same exact URL. There are plenty of other reasons as well. Here is how you can do&hellip;<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[10],"tags":[],"class_list":["post-585","post","type-post","status-publish","format-standard","hentry","category-php"],"_links":{"self":[{"href":"https:\/\/blog.mycarts.shop\/index.php?rest_route=\/wp\/v2\/posts\/585","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.mycarts.shop\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.mycarts.shop\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.mycarts.shop\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.mycarts.shop\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=585"}],"version-history":[{"count":0,"href":"https:\/\/blog.mycarts.shop\/index.php?rest_route=\/wp\/v2\/posts\/585\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.mycarts.shop\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=585"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.mycarts.shop\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=585"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.mycarts.shop\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=585"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}