当前 url 的 PHP 问题

发布于 2024-12-15 07:44:20 字数 510 浏览 1 评论 0原文

当用户更改页面语言时,我使用“当前网址”函数来获取当前链接

$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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

雅心素梦 2024-12-22 07:44:20

您可以使用 parse_url 函数,这里是一个示例:

$uri = parse_url( $_SERVER['REQUEST_URI']);
$protocol = !empty($_SERVER['HTTPS']) ? 'https://' : 'http://';
$url = $protocol . $_SERVER['SERVER_NAME'] . $_SERVER['SCRIPT_NAME'] . '?' . ( isset( $uri['query']) ?  $uri['query'] : '');

我没有在您的代码中看到您在哪里获取脚本的文件名,所以我使用了 $_SERVER['SCRIPT_NAME']

编辑:我的错误,我没有看到您需要操作/删除最后一个$_GET参数。下面是一个示例,说明如何使用与上述类似的方法结合 parse_str 来执行此操作。请注意,无论 lang 参数的位置如何,此方法都将起作用,它不必是查询字符串中的最后一个。

$protocol = !empty($_SERVER['HTTPS']) ? 'https://' : 'http://';

$params = array();
if( isset( $_SERVER['QUERY_STRING']) && !empty( $_SERVER['QUERY_STRING']))
{
    parse_str( $_SERVER['QUERY_STRING'], $params);
    $params['lang'] = 'anything';
    // unset( $params['lang']); // This will clear it from the parameters
}

// Now rebuild the new URL
$url = $protocol . $_SERVER['SERVER_NAME'] . $_SERVER['SCRIPT_NAME'] . ( !empty( $params) ? ( '?' . http_build_query( $params)) : '');

感谢 @Yzmir Ramirez 在第二个版本中的改进,消除了对 parse_url 的额外调用。

You can use the parse_url function, here is an example:

$uri = parse_url( $_SERVER['REQUEST_URI']);
$protocol = !empty($_SERVER['HTTPS']) ? 'https://' : 'http://';
$url = $protocol . $_SERVER['SERVER_NAME'] . $_SERVER['SCRIPT_NAME'] . '?' . ( isset( $uri['query']) ?  $uri['query'] : '');

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 the lang parameter, it does not have to be the last one in the query string.

$protocol = !empty($_SERVER['HTTPS']) ? 'https://' : 'http://';

$params = array();
if( isset( $_SERVER['QUERY_STRING']) && !empty( $_SERVER['QUERY_STRING']))
{
    parse_str( $_SERVER['QUERY_STRING'], $params);
    $params['lang'] = 'anything';
    // unset( $params['lang']); // This will clear it from the parameters
}

// Now rebuild the new URL
$url = $protocol . $_SERVER['SERVER_NAME'] . $_SERVER['SCRIPT_NAME'] . ( !empty( $params) ? ( '?' . http_build_query( $params)) : '');

Thanks to @Yzmir Ramirez for an improvement in the second version that eliminates the extraneous call to parse_url.

开始看清了 2024-12-22 07:44:20
$uri = explode('&', '/index.php?id=shop&id2=13&lang=lt');
$uri = $uri[0];
echo $uri; //echos /index.php?id=shop

您将只想使用它

$_SERVER['QUERY_STRING'] 

来获取查询字符串。如果你想保留所有变量,那么只需将 $uri 设置为爆炸,然后使用 foreach 循环遍历它们。

$uri = explode('&', $_SERVER['QUERY_STRING'];
foreach ($uri as $var_val) {
    $var_val = explode('=', $var_val);
    $var = $var_val[0];
    $val = $var_val[1];
}
$uri = explode('&', '/index.php?id=shop&id2=13&lang=lt');
$uri = $uri[0];
echo $uri; //echos /index.php?id=shop

You'll want to use

$_SERVER['QUERY_STRING'] 

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.

$uri = explode('&', $_SERVER['QUERY_STRING'];
foreach ($uri as $var_val) {
    $var_val = explode('=', $var_val);
    $var = $var_val[0];
    $val = $var_val[1];
}
a√萤火虫的光℡ 2024-12-22 07:44:20

试试这个:http://codepad.org/cEKYTAp8

$uri = "http://127.0.0.1/index.php?id=shop&id2=13&lang=lt";

$uri = substr($uri, 0, strrpos($uri, '&'));

var_dump($uri); // output: string(41) "http://127.0.0.1/index.php?id=shop&id2=13"

Try this: http://codepad.org/cEKYTAp8

$uri = "http://127.0.0.1/index.php?id=shop&id2=13&lang=lt";

$uri = substr($uri, 0, strrpos($uri, '&'));

var_dump($uri); // output: string(41) "http://127.0.0.1/index.php?id=shop&id2=13"
吃→可爱长大的 2024-12-22 07:44:20

这是我一直使用的代码,它也支持端口。

$protocol = (!isset($_SERVER["HTTPS"]) || strtolower($_SERVER["HTTPS"]) == "off") ? "http://" : "https://";
$port = ((isset($_SERVER["SERVER_PORT"]) &&
    // http:// protocol defaults to port 80
    (($_SERVER["SERVER_PORT"] != "80" && $protocol == "http://") ||
    // https:// protocol defaults to port 443
    ($_SERVER["SERVER_PORT"] != "443" && $protocol == "https://")) &&
    // Port is not in http host (port is followed by : at end of address)
    strpos($_SERVER["HTTP_HOST"], ":") === false) ? ":" . $_SERVER["SERVER_PORT"] : '');

return $protocol . $_SERVER["HTTP_HOST"] . $port . $_SERVER["REQUEST_URI"];

Here is the code I have always used, it also supports ports.

$protocol = (!isset($_SERVER["HTTPS"]) || strtolower($_SERVER["HTTPS"]) == "off") ? "http://" : "https://";
$port = ((isset($_SERVER["SERVER_PORT"]) &&
    // http:// protocol defaults to port 80
    (($_SERVER["SERVER_PORT"] != "80" && $protocol == "http://") ||
    // https:// protocol defaults to port 443
    ($_SERVER["SERVER_PORT"] != "443" && $protocol == "https://")) &&
    // Port is not in http host (port is followed by : at end of address)
    strpos($_SERVER["HTTP_HOST"], ":") === false) ? ":" . $_SERVER["SERVER_PORT"] : '');

return $protocol . $_SERVER["HTTP_HOST"] . $port . $_SERVER["REQUEST_URI"];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文