1

I would like to the whole of the URL including the _GET variable names and values, for example www.mywebsite.com/store.php?department=MENS

The code I have used below only gives me the URL without the _GET variable part.

$url = $_SERVER['SERVER_NAME']; 
$page = $_SERVER['PHP_SELF'];
$page = $_POST['url'];
echo "http://".$url.$page; 

All I would like is to be able to copy that URL exactly how it is.

0

4 Answers 4

4

try this function

  public function getURL()
     {
        $protocol = @$_SERVER['HTTPS'] == 'on' ? 'https' : 'http';

        return  $protocol.'://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
     }

taken from [this article I wrote][1]

use it like

echo getURL();

see if works for you [1]: http://jaspreetchahal.org/how-do-you-get-current-browser-url-with-php/

1
  • 2
    To be completely safe, you should also check if SERVER_PORT is something other than 80. Feb 20, 2012 at 2:45
2
function currenturl() {
$pageURL = 'http';
if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
$pageURL .= "://";
if ($_SERVER["SERVER_PORT"] != "80") {
$pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
} else {
$pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
}
return $pageURL;
}

simply do $url=currenturl(); that's it

2
  • Upvote. Good call on the port, as well. Inline if statements could help a lot in making this cleaner, by the way - for example $pageurl = ($_SERVER["HTTPS"] == "on") ? 'https' : 'http'; Feb 20, 2012 at 2:16
  • Yup, it looks nicer that way. I actually use inline ifs wherever it's human as I have thing against loads of code. But I edited a bit just to make it easier to read
    – user1189402
    Feb 20, 2012 at 3:40
0
$url = (!empty($_SERVER['HTTPS']))
    ? "https://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'] 
    : "http://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
0

like this one..

$(function() {
$('.ajax-link').click( function() {
var link=$(this).attr('href');
     $.post( "savedl.php",{name:link}, 
     function(data) {
        window.location.href=link;
     });
     return false; // don't follow the link!

  });
});

sample is i have the url in the link

<a href='' class="ajax-link" id="url_name">url </a>

then you can get the value on savedl.php using $_POST['name']

Not the answer you're looking for? Browse other questions tagged or ask your own question.