44

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

0

6 Answers 6

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

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

Sign up to request clarification or add additional context in comments.

3 Comments

This works in your case, but if the url path was 'deeper' ( www.someweb.com/somewhere/somepage.php?id=10 for example ), it would still only return the last part: 'somepage.php?id=10'
Your code return last part only example.com/page/article and return article, how to make so it should return page/article
@RajeevRanjanSharma maybe you could use REQUEST_URI and implode by / and return 2 last array?
33

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

Comments

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

3 Comments

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)
if you are using more than one paramater, then use create array like, id,id2,id3 and use while loop..
There is a way, use $_SERVER['PHP_SELF']."?".$_SERVER['QUERY_STRING']
1
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

1 Comment

Nope. it doesn't works. using basename finally. Thanks for answer
0

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

Comments

0

Here's a very thorough function I use to get the full URL:

function get_full_url( bool $include_query = true ) {
    $protocol = ( ! empty( $_SERVER['HTTPS'] ) && strtolower( $_SERVER['HTTPS'] ) !== 'off' )
        || ( isset( $_SERVER['HTTP_X_FORWARDED_PROTO'] ) && strtolower( $_SERVER['HTTP_X_FORWARDED_PROTO'] ) === 'https' )
        || ( ! empty( $_SERVER['HTTP_FRONT_END_HTTPS'] ) && strtolower( $_SERVER['HTTP_FRONT_END_HTTPS'] ) !== 'off' )
        || ( isset( $_SERVER['SERVER_PORT'] ) && intval( $_SERVER['SERVER_PORT'] ) === 443 ) ? 'https' : 'http';
    $uri = $protocol . '://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
    return $include_query ? $uri : str_replace( '?' . $_SERVER['QUERY_STRING'], '', $uri );
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.