HTTP GET重定向不工作



我有一个接收来自表单的数据的页面。它不能正确地重定向所有传入的变量。我认为这是因为在传递的数据中有空格,所以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>

use 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;');

这里有一些问题需要解决:

  1. 访问未验证存在的数组的索引:如果试图读取不存在的变量,PHP会抛出错误。在读取该变量之前,您应该使用issetarray_key_exists(如果是数组),例如:

     if (isset($_GET['aff'])) {
         $aff = $_GET['aff'];
     } else {
         $aff = null;
     }
    

您也可以使用条件运算符?:来表示它的较短的变体:

    $aff = isset($_GET['aff']) ? $_GET['aff'] : null;
  • 您需要对URL参数值使用适当的编码:使用urlencode根据application/x-www-form-urnelcoded内容类型或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编码。特别是作为destURL参数值传递的URL。你可以只转换URL一次(例如,尝试这个在线工具),因为它在你的代码中似乎是静态的:

    ...&destURL= http%3a%2f%2fmywebsite.com%2fpage%2ftestpage.php%3fpubSubID%3d%5bpubSubID%5d%26email%3d%5bemail%5d
    

    最新更新