我有一个页面接收从表单传递的数据。它没有正确地重定向传递的所有变量。我认为这是因为传递的数据中有空格,因此URL在空格处断开。我试图做一个urlencode但我无法让它正常工作。我想我需要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>
答案 0 :(得分:1)
$data = array('foo'=>'bar',
'baz'=>'boom',
'cow'=>'milk',
'php'=>'hypertext processor');
echo http_build_query($data) . "\n";
echo http_build_query($data, '', '&');
?>
The above example will output:
foo=bar&baz=boom&cow=milk&php=hypertext+processor
foo=bar&baz=boom&cow=milk&php=hypertext+processor
适合您的情况
$get_data=$_GET;
echo http_build_query($get_data) . "\n";
echo http_build_query($get_data, '', '&');
答案 1 :(得分:1)
有些问题需要解决:
访问未验证存在的数组的索引:如果您尝试读取不存在的变量,则PHP会引发错误。在读取该变量之前,您应该使用isset
或array_key_exists
(如果是数组),例如:
if (isset($_GET['aff'])) {
$aff = $_GET['aff'];
} else {
$aff = null;
}
您还可以使用conditional operator ?:
作为更短的变体:
$aff = isset($_GET['aff']) ? $_GET['aff'] : null;
您需要对网址参数值使用正确的编码:使用urlencode
根据application/x-www-form-urnelcoded content type或rawurlencode
对普通percent-encoding进行编码或者 - 在构建整个查询时 - 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); ?>">
答案 2 :(得分:0)
您需要URL-encode所有值。特别是您传递的URL作为destURL
参数的值。您只需转换一次网址(例如,尝试this online tool),因为它在您的代码中似乎是静态的:
...&destURL= http%3a%2f%2fmywebsite.com%2fpage%2ftestpage.php%3fpubSubID%3d%5bpubSubID%5d%26email%3d%5bemail%5d