2

I am trying to get the entire page URL as a string in PHP - so, if the requested URL is ./foo.php?arg1=test&arg2=test2, I get "./foo.php?arg1=test&arg2=test2".

I know that I can get the ./foo.php part from $_SERVER and the variables from $_GET, but I was wondering if there's an easy way to do it in just one fell swoop.

TIA.

benjy
  • 4,664
  • 7
  • 38
  • 43
  • possible duplicate of [Get the full URL in PHP](http://stackoverflow.com/questions/6768793/get-the-full-url-in-php) – Hasan Fathi Jul 25 '15 at 13:55

3 Answers3

6
$url = (isset($_SERVER['HTTPS']) == 'on' ? 'https' : 'http').'://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];

This should return the full url based on what was typed in the address bar.

Sean Vieira
  • 155,703
  • 32
  • 311
  • 293
Ben Rowe
  • 28,406
  • 6
  • 55
  • 75
  • It is missing a closing paren (after the isset call) and a period before concating the :// but after those changes it does return the full URL. – niczak Dec 28 '10 at 19:22
4

If I open the following URL in my browser :

http://tests/temp/temp.php?a=145&b=glop

The following piece of code :

var_dump($_SERVER['REQUEST_URI']);

Gives me :

string '/temp/temp.php?a=145&b=glop' (length=27)

So, $_SERVER['REQUEST_URI'] might be what you are looking for...

Pascal MARTIN
  • 395,085
  • 80
  • 655
  • 663
0

$_SERVER['REQUEST_URI']

http://au2.php.net/manual/en/reserved.variables.server.php

Tres
  • 5,604
  • 3
  • 19
  • 17