在重定向之前更改 URL 中的字母
Change Letters in URL Before Redirect
我正在尝试抓取当前页面 URL 并在使用 header()
重定向到下一页之前更改其中的一些值
如果我当前的 url 是 http://example.com/abc-def-1-xxx-g.php
我想将 xxx
部分更改为 a
那么我的 url 将是 http://example.com/abc-def-1-a-g.php
这是我在重定向前 xxx 页面上的 header 的内容
header("Location: whattoputhere.php?". $_SERVER['QUERY_STRING']);
试试这个:
$actual_link = "http://".$_SERVER[HTTP_HOST].$_SERVER[REQUEST_URI];
$url = str_replace("xxx", "a", $actual_link);
Header("Location: ".$url);
这应该适合你:
$str = "http://example.com/abc-def-1-xxx-g.php";
$url = str_replace("xxx", "a", $str);
//For example
echo $url;
输出:
http://example.com/abc-def-1-a-g.php
编辑:
要获取当前 URL 使用此:
$str = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
也不要忘记在每个 header 之后使用 exit()
以确保脚本停止执行,如下所示:
header("Location: $url");
exit();
您可以在调用 header() 之前轻松解析字符串。
尝试 parse_str、str_replace 等
并且您有 $_SERVER['QUERY_STRING'] 和 $_SERVER['REQUEST_URI'] 可用,具体取决于您需要解析的内容:
$qs = parse_str(str_replace("$string_I_do_not_want",'',$_SERVER['REQUEST_URI']));
if ($some_value_from_string == $something_to_change) {
$new_value = compute_my_new_value();
}
$new_location = "http://example.com/". $somepage . $my_qs_with_new_value;
header("location: $new_location");
exit;
我正在尝试抓取当前页面 URL 并在使用 header()
如果我当前的 url 是 http://example.com/abc-def-1-xxx-g.php
我想将 xxx
部分更改为 a
那么我的 url 将是 http://example.com/abc-def-1-a-g.php
这是我在重定向前 xxx 页面上的 header 的内容
header("Location: whattoputhere.php?". $_SERVER['QUERY_STRING']);
试试这个:
$actual_link = "http://".$_SERVER[HTTP_HOST].$_SERVER[REQUEST_URI];
$url = str_replace("xxx", "a", $actual_link);
Header("Location: ".$url);
这应该适合你:
$str = "http://example.com/abc-def-1-xxx-g.php";
$url = str_replace("xxx", "a", $str);
//For example
echo $url;
输出:
http://example.com/abc-def-1-a-g.php
编辑:
要获取当前 URL 使用此:
$str = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
也不要忘记在每个 header 之后使用 exit()
以确保脚本停止执行,如下所示:
header("Location: $url");
exit();
您可以在调用 header() 之前轻松解析字符串。
尝试 parse_str、str_replace 等
并且您有 $_SERVER['QUERY_STRING'] 和 $_SERVER['REQUEST_URI'] 可用,具体取决于您需要解析的内容:
$qs = parse_str(str_replace("$string_I_do_not_want",'',$_SERVER['REQUEST_URI']));
if ($some_value_from_string == $something_to_change) {
$new_value = compute_my_new_value();
}
$new_location = "http://example.com/". $somepage . $my_qs_with_new_value;
header("location: $new_location");
exit;