The following function permits you to get the current page (or permalink) URL with PHP. Not unlike regular expression variations, there are numerous methods to return the current URL... so expect this function to evolve over time.
1
<?php
2
/*
3
Get Current URL With PHP
4
http://www.beliefmedia.com/code/php-snippets/current-url-php
5
*/
6
7
function beliefmedia_get_url() {
8
9
$url = ( isset( $_SERVER['HTTPS'] ) && 'on' === $_SERVER['HTTPS'] ) ? 'https' : 'http';
10
$url .= '://' . $_SERVER['SERVER_NAME'];
11
$url .= in_array( $_SERVER['SERVER_PORT'], array( '80', '443' ) ) ? '' : ":" . $_SERVER['SERVER_PORT'];
12
$url .= $_SERVER['REQUEST_URI'];
13
14
return $url;
15
}
16
17
/* Usage */
18
echo beliefmedia_get_url();
If used on this page, the function would return http://www.beliefmedia.com/code/php-snippets/current-url-php
.