Dacto
Dacto

Reputation: 2911

PHP: Get Page URL minus arguments

How do i get the URL of the current page minus all of the get arguments (?blah=2&blah4=90...) I know i can get the full URL with $_SERVER['REQUEST_URI'] but i was wondering if there was something that more fit my needs.

Or should i just do strpos ? and substr to chop of the arguments? ( i imagine that a $_SERVER var would be more efficient - if one exists)

Thanks

Upvotes: 0

Views: 3202

Answers (4)

AleOtero93
AleOtero93

Reputation: 498

You can use this, is pretty simple:

$url = explode("?",$_SERVER[REQUEST_URI]);
$url = "http://$_SERVER[HTTP_HOST]/" . $url[0]; 

With exploding the URL you will have in $url[0] the "first part" and in $url[1] all the arguments.

Upvotes: 0

orlp
orlp

Reputation: 117771

$_SERVER['REQUEST_URL']

Sometimes answers are easy :) I found the solution here: http://php.net/manual/en/reserved.variables.server.php by CTRL+F'ing for "query".

EDIT

As matchu said in the comments, not all servers support REQUEST_URL. In that case I would use the much less elegant strtok($url, '?');.

Upvotes: 5

Santiago Alessandri
Santiago Alessandri

Reputation: 6855

You can use the parse_url function and then concat the parts you need(scheme, host, port, user, pass and path)

Upvotes: 0

Jason
Jason

Reputation: 15378

$_SERVER['REQUEST_URL']

should do the trick...

It's listed here in the comments: http://php.net/manual/en/reserved.variables.server.php

Don't know why it isn't in the doco?

Upvotes: 0

Related Questions