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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用 http_build_query
适合您的情况
use http_build_query
for your case
这里需要解决一些问题:
访问未验证是否存在的数组的索引:如果尝试读取不存在的变量,PHP 会抛出错误。您应该使用
isset
或array_key_exists
(如果是数组)在读取该变量之前,例如:您还可以使用 条件运算符
?:
的较短变体:您需要对 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);
您需要对 HTML 属性值使用正确的编码;使用
htmlspecialchars
:There are some issues that need to be addressed heres:
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
orarray_key_exists
(in case of an array) before reading that variable, e.g.:You can also use the conditional operator
?:
for a shorter variant of this: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 orrawurlencode
for the plain percent-encoding or – as you build the entire query –http_build_query
:You need to use proper encoding on the HTML attribute value; use
htmlspecialchars
:您需要对所有值进行URL 编码。特别是作为
destURL
参数值传递的 URL。您只需转换一次 URL(例如,尝试 此在线工具),因为它似乎在代码中保持静态: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: