使用PHP的urlencode()
函數(shù)將值編碼后再放入URL中。
該函數(shù)將“奇怪”的字符,如=
,轉(zhuǎn)換為安全放入URL中的格式。你可以像這樣使用它:
Header('Location: /index.php?id=' . urlencode($id))
在URL中傳遞的值中的奇怪字符應(yīng)該使用urlencode()
進行轉(zhuǎn)義。
例如,以下代碼片段:
echo urlencode('dsf13f3343f23/23=');
將給出:
dsf13f3343f23%2F23%3D
作為URL參數(shù),這樣是有效的。
如果你想要構(gòu)建一個包含多個參數(shù)的查詢字符串,請查看http_build_query()
函數(shù)。
例如:
echo http_build_query(array( 'id' => 'dsf13f3343f23/23=', 'a' => 'plop', 'b' => '$^@test', ));
將給出:
id=dsf13f3343f23%2F23%3D&a=plop&b=%24%5E%40test
這個函數(shù)會自動處理轉(zhuǎn)義和參數(shù)的拼接;-)