HTTP GET重定向不起作用

| 我有一个页面,用于接收从表单传递的数据。它不能正确重定向所有传递的变量。我认为是因为传递的数据中有空格,所以URL在空格处中断。我试图做一个urlencode,但我无法使其正常工作。我在想我需要对传递的变量进行空格编码,但是需要您的帮助。 这是代码:
<?
$aff = $_GET[\'aff\'] ;
$click_id = $_GET[\'click_id\'] ;
$email = $_GET[\'from\'];
$fname = $_GET[\'name_(awf_first)\'];
$lname = $_GET[\'name_(awf_last)\'];
$zipcode = $_GET[\'custom_zipcode\'];
$address = $_GET[\'custom_address\'];
$phone = $_GET[\'custom_phone_no\'];
$state = $_GET[\'custom_state\'];
$city = $_GET[\'custom_city\'];
$subid = $_GET[\'meta_adtracking\'] ;
$cblink = $_GET[\'cblink\'];
$keyword = $_GET[\'keyword\'] ;
$keyword = eregi_replace(\'[^a-z0-9 ]\', \'2\', $keyword);
?>
<META HTTP-EQUIV=\"refresh\" CONTENT=0;URL=\"http://mywebsite.com/page/?pID=sample&email=<?print $email?>&fname=<?print $fname?>&lname=<?print $lname?>&addr=<?print $address?>&city=<?print $city?>&state=<?print $state?>&zip=<?print $zipcode?>&hphone=<?print $phone?>&mphone=<?print $phone?>&country=US&pubSubID=<?print $subid?>&destURL=http://mywebsite.com/page/testpage.php?pubSubID=[pubSubID]&email=[email]\">
<html>
<body>
</body></html>
    
已邀请:
使用http_build_query
$data = array(\'foo\'=>\'bar\',
              \'baz\'=>\'boom\',
              \'cow\'=>\'milk\',
              \'php\'=>\'hypertext processor\');

echo http_build_query($data) . \"\\n\";
echo http_build_query($data, \'\', \'&amp;\');

?>
The above example will output:
foo=bar&baz=boom&cow=milk&php=hypertext+processor
foo=bar&amp;baz=boom&amp;cow=milk&amp;php=hypertext+processor
对于你的情况
$get_data=$_GET;
echo http_build_query($get_data) . \"\\n\";
echo http_build_query($get_data, \'\', \'&amp;\');
    
这里有一些问题需要解决: 访问其存在性未经验证的数组的索引:如果尝试读取不存在的变量,PHP会引发错误。在读取该变量之前,应使用
isset
array_key_exists
(如果是数组),例如:
if (isset($_GET[\'aff\'])) {
    $aff = $_GET[\'aff\'];
} else {
    $aff = null;
}
您还可以使用条件运算符
?:
来简化此操作:
$aff = isset($_GET[\'aff\']) ? $_GET[\'aff\'] : null;
您需要对URL参数值使用正确的编码:根据应用程序/ x-www-form-urnelcoded的内容类型,使用
urlencode
对值进行编码,或者对于纯百分比编码使用
rawurlencode
,或者–在构建整个查询时–
http_build_query
$query = array(
    \'pID\' => \'sample\',
    \'email\' => isset($_GET[\'from\']) ? $_GET[\'from\'] : null,
    \'fname\' => isset($_GET[\'name_(awf_first)\']) ? $_GET[\'name_(awf_first)\'] : null,
    \'lname\' => isset($_GET[\'name_(awf_last)\']) ? $_GET[\'name_(awf_last)\'] : null,
    \'addr\' => isset($_GET[\'custom_address\']) ? $_GET[\'custom_address\'] : null,
    \'city\' => isset($_GET[\'custom_city\']) ? $_GET[\'custom_city\'] : null,
    \'state\' => isset($_GET[\'custom_state\']) ? $_GET[\'custom_state\'] : null,
    \'zip\' => isset($_GET[\'custom_zipcode\']) ? $_GET[\'custom_zipcode\'] : null,
    \'hphone\' => isset($_GET[\'custom_phone_no\']) ? $_GET[\'custom_phone_no\'] : null,
    \'mphone\' => isset($_GET[\'custom_phone_no\']) ? $_GET[\'custom_phone_no\'] : null,
    \'country\' => \'US\',
    \'pubSubID\' => isset($_GET[\'meta_adtracking\']) ? $_GET[\'meta_adtracking\'] : null,
    \'destURL\' => \'http://mywebsite.com/page/testpage.php?pubSubID=[pubSubID]&email=[email]\'
);
$query = http_build_query($query);
您需要在HTML属性值上使用正确的编码。用
htmlspecialchars
<META HTTP-EQUIV=\"refresh\" CONTENT=\"<?php echo htmlspecialchars(\'0;URL=http://mywebsite.com/page/?\'.$query); ?>\">
    
您需要对所有值进行URL编码。特别是作为“ 14”参数值传递的URL。您可以只转换一次网址(例如,尝试使用此在线工具),因为它在代码中似乎是静态的:
...&destURL= http%3a%2f%2fmywebsite.com%2fpage%2ftestpage.php%3fpubSubID%3d%5bpubSubID%5d%26email%3d%5bemail%5d
    

要回复问题请先登录注册