35

I want to get a URL and its parameters. Example:

www.someweb.com/somepage.php?id=10

How to get only somepage.php?id=10?

I'm already tried REQUEST_URI and PHP_SELF but it only gives me www.someweb.com/somepage.php or somepage.php. What I want to is just the page name and its parameter(s).

FOX 9000
  • 123
  • 1
  • 1
  • 6
Cross Vander
  • 1,857
  • 2
  • 18
  • 32

5 Answers5

70
basename($_SERVER['REQUEST_URI']);

This will return all URLs with page name. (e.g.: index.php?id=1&name=rr&class=10).

Trenton McKinney
  • 43,885
  • 25
  • 111
  • 113
Cross Vander
  • 1,857
  • 2
  • 18
  • 32
26

for complette URL with protocol, servername and parameters:

 $base_url = ( isset($_SERVER['HTTPS']) && $_SERVER['HTTPS']=='on' ? 'https' : 'http' ) . '://' .  $_SERVER['HTTP_HOST'];
 $url = $base_url . $_SERVER["REQUEST_URI"];
miralong
  • 729
  • 7
  • 11
8

$_SERVER['PHP_SELF'] for the page name and $_GET['id'] for a specific parameter.

try print_r($_GET); to print out all the parameters.

for your request echo $_SERVER['PHP_SELF']."?id=".$_GET['id'];

return all the parameters echo $_SERVER['PHP_SELF']."?".$_SERVER['QUERY_STRING'];

Mohd Moe
  • 577
  • 3
  • 9
  • there's no way without use $_GET[id]? Because sometimes I use 2 or 3 parameters too. (I just think to make it simpler than $_GET, but if it only the way to make what I want, it's okay) – Cross Vander Nov 17 '12 at 02:55
  • if you are using more than one paramater, then use create array like, id,id2,id3 and use while loop.. – samayo Nov 17 '12 at 02:58
  • There is a way, use `$_SERVER['PHP_SELF']."?".$_SERVER['QUERY_STRING']` – Mohd Moe Nov 17 '12 at 03:00
0
function curPageName() {
 return substr($_SERVER["SCRIPT_NAME"],strrpos($_SERVER["SCRIPT_NAME"],"/")+1);
}
echo "The current page is ".curPageName()."?".$_SERVER['QUERY_STRING'];

This will get you page name , it will get the string after the last slash

Leon Armstrong
  • 1,182
  • 2
  • 14
  • 36
0

Here's probably what you are looking for: php-get-url-query-string. You can combine it with other suggested $_SERVER parameters.

Community
  • 1
  • 1
Kosta
  • 1,777
  • 1
  • 14
  • 21