2

Using Php I would like to extract the current Url of a page including all variables attached to it. $_SERVER['PHP_SELF'] only return the url without the variable . any idea what function I need.

example : www.site.com/?v1=xyz&v2=123

using $_SERVER['PHP_SELF'] I get only : www.site.com as opposed to the whole url. using $_GET[] individually is not an opton since I am not sure what variable are attached to the URL.

thank you

0

5 Answers 5

6

You could output the content of the $_SERVER super-global variable : there are many interesting things in there ;-)


For example, calling a page with an URL like this :

http://localhost/temp/temp.php?a=10&b=glop

And using :

var_dump($_SERVER);

I get at least :

array
  ...
  'HTTP_HOST' => string 'localhost' (length=9)
  ...
  'REQUEST_METHOD' => string 'GET' (length=3)
  'QUERY_STRING' => string 'a=10&b=glop' (length=11)
  'REQUEST_URI' => string '/temp/temp.php?a=10&b=glop' (length=26)
  'SCRIPT_NAME' => string '/temp/temp.php' (length=14)
  'PHP_SELF' => string '/temp/temp.php' (length=14)
  'REQUEST_TIME' => int 1270060299


In there, I suppose at least those could interest you :

  • $_SERVER['QUERY_STRING'] : contains the whole query string ; i.e. the list of all parameters and their values
  • $_SERVER['REQUEST_URI'] : contains the requested URI -- including the parameters
Sign up to request clarification or add additional context in comments.

Comments

3
$request = $_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'].$_SERVER['QUERY_STRING'];

For the entire path and Query strings.

1 Comment

Wouldn't that duplicate the parameters?
0

Use $_SERVER['QUERY_STRING'] to retrieve the variables part.

Cheers

Comments

0

The best solution I have found so far is this one: http://hayageek.com/php-get-current-url/

function getCurrentURL() {
    $currentURL = (@$_SERVER["HTTPS"] == "on") ? "https://" : "http://";
    $currentURL .= $_SERVER["SERVER_NAME"];

    if($_SERVER["SERVER_PORT"] != "80" && $_SERVER["SERVER_PORT"] != "443") {
        $currentURL .= ":".$_SERVER["SERVER_PORT"];
    } 

    $currentURL .= $_SERVER["REQUEST_URI"];
    return $currentURL;
}

Comments

0

Two lines:

$base_url = ( isset($_SERVER['HTTPS']) && $_SERVER['HTTPS']=='on' ? 'https' : 'http' ) . '://' .  $_SERVER['HTTP_HOST'];

url = $base_url . $_SERVER["REQUEST_URI"];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.