簡體   English   中英

PHP header_redirect具有多個變量

[英]PHP header_redirect with multiple variables

我正在寫一個PHP腳本,它將基於這樣的鏈接進行重定向:

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

但是,當我運行上面的鏈接時,我僅被重定向到“ other_page.php”,其他變量丟失。

我將如何實現?

您可以使用一些加密和解密技巧來解決此問題,我已經使用base64_encodebase64_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()函數對重定向base64_decode()解碼,並獲得預期的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);

如果您的起始網址是這樣的:

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 :使用rawurlencoderawurldecode

但是,如果您的目標是重定向到$_GET['redirect'] (並忽略URL中的任何其他變量),那么您需要先編碼other_page.php&category=1234&limit=24位將其放入您的起始網址。 這將有效地轉義特殊字符,並使您可以簡單地調用header_redirect(rawurldecode($_GET['redirect']));

假設您的起始網址是:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM