Home » Php » PHP Get URL with Parameter

PHP Get URL with Parameter

Posted by: admin April 23, 2020 Leave a comment


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


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).

How to&Answers:

Finally found this method:


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


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"];


$_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'];


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


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