当前 url 的 PHP 问题
当用户更改页面语言时,我使用“当前网址”函数来获取当前链接
$uri = explode('&', $_SERVER['REQUEST_URI']);
$uri = $uri[0];
$url = (!empty($_SERVER['HTTPS'])) ? "https://".$_SERVER['SERVER_NAME'].$uri : "http://".$_SERVER['SERVER_NAME'].$uri;
问题是,当我有这样的链接时:
http://127.0.0.1/index.php?id=shop&id2=13&lang=lt
id2
当然会消失。对此我能做什么?如果id2
设置为使用explode与第二个&
或类似的东西,这是可能的吗?
I use the "Current url" function to get the current link when user changing page language
$uri = explode('&', $_SERVER['REQUEST_URI']);
$uri = $uri[0];
$url = (!empty($_SERVER['HTTPS'])) ? "https://".$_SERVER['SERVER_NAME'].$uri : "http://".$_SERVER['SERVER_NAME'].$uri;
Problem is, when I have a link like this:
http://127.0.0.1/index.php?id=shop&id2=13&lang=lt
id2
, of course, disappears. What can I do about this? It is possible if id2
is set to use explode with a second &
or something like this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以使用 parse_url 函数,这里是一个示例:
我没有在您的代码中看到您在哪里获取脚本的文件名,所以我使用了
$_SERVER['SCRIPT_NAME']
。编辑:我的错误,我没有看到您需要操作/删除最后一个
$_GET
参数。下面是一个示例,说明如何使用与上述类似的方法结合 parse_str 来执行此操作。请注意,无论lang
参数的位置如何,此方法都将起作用,它不必是查询字符串中的最后一个。感谢 @Yzmir Ramirez 在第二个版本中的改进,消除了对
parse_url
的额外调用。You can use the parse_url function, here is an example:
I did not see in your code where you get the script's filename, so I used
$_SERVER['SCRIPT_NAME']
.Edit: My mistake, I did not see that you need to manipulate / remove the last
$_GET
parameter. Here is an example on how to do that using a method similar to the above in conjunction with parse_str. Note that this method will work regardless of the location of thelang
parameter, it does not have to be the last one in the query string.Thanks to @Yzmir Ramirez for an improvement in the second version that eliminates the extraneous call to
parse_url
.您将只想使用它
来获取查询字符串。如果你想保留所有变量,那么只需将 $uri 设置为爆炸,然后使用 foreach 循环遍历它们。
You'll want to use
to get just the query string. And if you want to keep all the variables, then just keep $uri set to the explode then cycle through them with a foreach.
试试这个:http://codepad.org/cEKYTAp8
Try this: http://codepad.org/cEKYTAp8
这是我一直使用的代码,它也支持端口。
Here is the code I have always used, it also supports ports.