PHP header_redirect 有多个变量
PHP header_redirect with multiple variables
我正在编写一个 PHP 脚本,它将基于 link 重定向,如下所示:
header_redirect($_GET['redirect']);
而 URL 是:
https://www.url.com/index.php?change_language=english&redirect=other_page.php?category=1234&limit=24
如您所见,实际页面为
https://www.url.com/index.php?change_language=english
然后重定向到另一个具有多个变量的页面,例如:
&redirect=other_page.php?category=1234&limit=24
但是,当我 运行 上面的 link 时,我只被重定向到 "other_page.php" 并且其他变量都丢失了。
我该如何实现?
你可以使用一些加密和解密技巧来解决这个问题,我已经使用 base64_encode
和 base64_decode()
函数来解决你的问题。
第 1 步:在您的 html 页面中
<?php $redirectUrl = base64_encode('other_page.php?category=1234&limit=24'); ?>
<a href="index.php?change_language=english&redirect=<?php echo $redirectUrl;?>">Link1 </a>
第 2 步: 在您的 header_redirect()
函数中,您可以使用 base64_decode()
函数解码重定向字符串并获得预期的字符串。
function header_redirect($redirect){
$redirect = base64_decode($redirect); // you can get the expected redirected url with query string
//your redirect script
}
取决于您要做什么。
选项 1:使用 http_build_query.
尝试:
$target=$_GET['redirect'];
unset($_GET['redirect']);
$query_str=http_build_query($_GET);
header_redirect($target.'?'.$query_str);
如果你的起始 URL 是这样的:
https://www.url.com/index.php?change_language=english&redirect=other_page.php&category=1234&limit=24
然后您将被重定向到:
https://www.url.com/other_page.php?change_language=english&category=1234&limit=24
选项 2:使用 rawurlencode and rawurldecode.
但是,如果您的目标是重定向到存储在 $_GET['redirect']
中的任何内容(并忽略 URL 中的任何其他变量),那么您需要对 other_page.php&category=1234&limit=24
位,然后再将其放入起始 URL。这将有效地转义特殊字符并允许您简单地调用 header_redirect(rawurldecode($_GET['redirect']));
.
假设您的起始 URL 是:
https://www.url.com/index.php?change_language=english&redirect=other_page.php%3Fcategory%3D1234%26limit%3D24
然后您将被重定向到:
https://www.url.com/other_page.php?category=1234&limit=24
我正在编写一个 PHP 脚本,它将基于 link 重定向,如下所示:
header_redirect($_GET['redirect']);
而 URL 是:
https://www.url.com/index.php?change_language=english&redirect=other_page.php?category=1234&limit=24
如您所见,实际页面为
https://www.url.com/index.php?change_language=english
然后重定向到另一个具有多个变量的页面,例如:
&redirect=other_page.php?category=1234&limit=24
但是,当我 运行 上面的 link 时,我只被重定向到 "other_page.php" 并且其他变量都丢失了。
我该如何实现?
你可以使用一些加密和解密技巧来解决这个问题,我已经使用 base64_encode
和 base64_decode()
函数来解决你的问题。
第 1 步:在您的 html 页面中
<?php $redirectUrl = base64_encode('other_page.php?category=1234&limit=24'); ?>
<a href="index.php?change_language=english&redirect=<?php echo $redirectUrl;?>">Link1 </a>
第 2 步: 在您的 header_redirect()
函数中,您可以使用 base64_decode()
函数解码重定向字符串并获得预期的字符串。
function header_redirect($redirect){
$redirect = base64_decode($redirect); // you can get the expected redirected url with query string
//your redirect script
}
取决于您要做什么。
选项 1:使用 http_build_query.
尝试:
$target=$_GET['redirect'];
unset($_GET['redirect']);
$query_str=http_build_query($_GET);
header_redirect($target.'?'.$query_str);
如果你的起始 URL 是这样的:
https://www.url.com/index.php?change_language=english&redirect=other_page.php&category=1234&limit=24
然后您将被重定向到:
https://www.url.com/other_page.php?change_language=english&category=1234&limit=24
选项 2:使用 rawurlencode and rawurldecode.
但是,如果您的目标是重定向到存储在 $_GET['redirect']
中的任何内容(并忽略 URL 中的任何其他变量),那么您需要对 other_page.php&category=1234&limit=24
位,然后再将其放入起始 URL。这将有效地转义特殊字符并允许您简单地调用 header_redirect(rawurldecode($_GET['redirect']));
.
假设您的起始 URL 是:
https://www.url.com/index.php?change_language=english&redirect=other_page.php%3Fcategory%3D1234%26limit%3D24
然后您将被重定向到:
https://www.url.com/other_page.php?category=1234&limit=24