HTTP GET 重定向不起作用

发布于 2024-11-01 16:38:39 字数 1234 浏览 4 评论 0 原文

我有一个接收从表单传递的数据的页面。它无法正确重定向所有传递的变量。我认为这是因为传递的数据中有空格,因此 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>

I have a page that receives data passed from a form. It doesnt properly redirect with all the variables passed. I think its because there are spaces in the data passed so the URL breaks at the space. I tried to do a urlencode but I couldnt get it to work properly. I am thinking I need to urlencode the variables been passed with spaces in them but I need your help.

Here's the code:

<?
$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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

同展鸳鸯锦 2024-11-08 16:38:39

使用 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, '', '&');

?>
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, '', '&');

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, '', '&');

?>
The above example will output:
foo=bar&baz=boom&cow=milk&php=hypertext+processor
foo=bar&baz=boom&cow=milk&php=hypertext+processor

for your case

$get_data=$_GET;
echo http_build_query($get_data) . "\n";
echo http_build_query($get_data, '', '&');
空‖城人不在 2024-11-08 16:38:39

这里需要解决一些问题:

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

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

您还可以使用 条件运算符 ?: 的较短变体:

    $aff = isset($_GET['aff']) ? $_GET['aff'] : null;
  1. 您需要对 URL 参数值使用正确的编码:使用 urlencode 根据到 application/x-www-form-urnelcoded 内容类型rawurlencode 对于普通 百分比编码 或 – 在构建整个查询时 – http_build_query


    <前><代码> $query = 数组(
    'pID' => '样本',
    '电子邮件' => isset($_GET['来自']) ? $_GET['来自'] : 空,
    'fname'=>; isset($_GET['name_(awf_first)']) ? $_GET['name_(awf_first)'] : null,
    'lname'=> isset($_GET['name_(awf_last)']) ? $_GET['name_(awf_last)'] :空,
    '地址' => isset($_GET['custom_address']) ? $_GET['custom_address'] :空,
    '城市' => isset($_GET['custom_city']) ? $_GET['custom_city'] :空,
    '状态' => isset($_GET['custom_state']) ? $_GET['custom_state'] :空,
    '拉链' => isset($_GET['custom_zipcode']) ? $_GET['custom_zipcode'] :空,
    'hphone'=>; isset($_GET['custom_phone_no']) ? $_GET['custom_phone_no'] :空,
    '手机' => isset($_GET['custom_phone_no']) ? $_GET['custom_phone_no'] :空,
    '国家' => '我们',
    'pubSubID' =>; isset($_GET['meta_adtracking']) ? $_GET['meta_adtracking'] :空,
    'destURL'=>; 'http://mywebsite.com/page/testpage.php?pubSubID=[pubSubID]&email=[电子邮件]'
    );
    $query = http_build_query($query);

  2. 您需要对 HTML 属性值使用正确的编码;使用 htmlspecialchars

     

There are some issues that need to be addressed heres:

  1. Accessing indices of arrays whose existence is not verified: PHP throws an error if you try to read a variable that does not exist. You should use isset or array_key_exists (in case of an array) before reading that variable, e.g.:

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

You can also use the conditional operator ?: for a shorter variant of this:

    $aff = isset($_GET['aff']) ? $_GET['aff'] : null;
  1. You need to use proper encoding on the URL parameter values: either use urlencode to encode the value according to the application/x-www-form-urnelcoded content type or rawurlencode for the plain percent-encoding or – as you build the entire query – 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);
    
  2. You need to use proper encoding on the HTML attribute value; use htmlspecialchars:

     <META HTTP-EQUIV="refresh" CONTENT="<?php echo htmlspecialchars('0;URL=http://mywebsite.com/page/?'.$query); ?>">
    
一束光,穿透我孤独的魂 2024-11-08 16:38:39

您需要对所有值进行URL 编码。特别是作为 destURL 参数值传递的 URL。您只需转换一次 URL(例如,尝试 此在线工具),因为它似乎在代码中保持静态:

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

You need to URL-encode all values. In particular the URL that you pass as a value of the destURL param. You can convert the URL just once (e.g., try this online tool), since it seems to be static in your code:

...&destURL= http%3a%2f%2fmywebsite.com%2fpage%2ftestpage.php%3fpubSubID%3d%5bpubSubID%5d%26email%3d%5bemail%5d
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文