开发者

PHP: Get Page URL minus arguments

开发者 https://www.devze.com 2023-02-04 09:27 出处:网络
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 somethin

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


$_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, '?');.


$_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?


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消