2

This is my current URL structure http://wp.raddyx.in/consultant-dietitians/staff-single/#Fiona%20Brown

I am using echo $url=$_SERVER['REQUEST_URI'];

However it displays /consultant-dietitians/staff-single/ only, where I want to fetch the full URL.

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
Debakant Mohanty
  • 805
  • 1
  • 10
  • 19
  • This sounds like an XY-problem http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem - Please tell us about X, not Y. – Halcyon Sep 11 '13 at 15:06

3 Answers3

3

Use $_SERVER["SERVER_NAME"] to get the host name wp.raddyx.in

Use $_SERVER["HTTPS"] to check for http vs https.

You might need $_SERVER["SERVER_PORT"] too and some other misc things that can appear in a URL (like PHP_AUTH_USER)

You can not get the hash part of the URL (#Fiona%20Brown) since hashes are client-side only. They are not sent to the server.

Relevant manual page: http://php.net/manual/en/reserved.variables.server.php

Halcyon
  • 57,230
  • 10
  • 89
  • 128
2

Use $_SERVER[HTTP_HOST]:

$url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
zavg
  • 10,351
  • 4
  • 44
  • 67
  • 2
    Well using "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]" I only get up to http://wp.raddyx.in/consultant-dietitians/staff-single/ but I want get the string after the # i.e. http://wp.raddyx.in/consultant-dietitians/staff-single/#Fiona%20Brown – Debakant Mohanty Sep 11 '13 at 14:54
  • @novo, `echo "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";` produces `http://localhost/test.php` for me. My PHP version must be magic :) – davidkonrad Sep 11 '13 at 14:54
  • @Novocaine88 It is possible notation – zavg Sep 11 '13 at 14:54
  • @davidkonrad The browser doesn't actually send anything that comes after the hash(#) to the server because it is resolved within the browser. – zavg Sep 11 '13 at 14:56
  • My bad, I assumed the use of quotes within the array e.g. `['HTTP_HOST']` which wouldn't work. In my haste glazed over the fact you didn't do that, in which case this is fine. – Novocaine Sep 11 '13 at 14:58
  • @zavg - doesnt remember claiming that #fragments was included in REQUEST_URI – davidkonrad Sep 11 '13 at 15:01
  • This solution will fail for `https://` – Edson Medina Nov 02 '17 at 12:56
0
echo $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
sikas
  • 5,435
  • 28
  • 75
  • 120