【发布时间】:2014-08-09 06:21:29
【问题描述】:
我正在尝试为澳大利亚的少数商店构建邮政编码检查器。基本上,客户输入他们的邮政编码,然后网站将重定向到适当的本地定价/服务。
我使用this 作为基础,但我遇到了一个问题(由该站点上的一个 cmets 也列出),无论出于某种原因输入什么值,代码都将 zip 标记为 SA,即使它完全不同。
我使用的代码如下。
<?php
// v1.0 Postcode Checker Just iStuff
// Copyright Ben Green 2014
// Redirect Customers to Appropriate Local Pricing and Services
// Sets cookie upon entering postcode, will then remember postcode for 3 days on customer's system
if (preg_match('#^\d+$#', $_POST['cf_postcode'])):
else:
print "Please enter your postcode correctly.";
endif;
$postcode = $_POST['cf_postcode'];
function findState($postcode) {
$ranges = array(
'NSW' => array(
1000, 1999,
2000, 2599,
2619, 2898,
2921, 2999
),
'ACT' => array(
200, 299,
2600, 2618,
2900, 2920
),
'VIC' => array(
3000, 3999,
8000, 8999
),
'QLD' => array(
4000, 4999,
9000, 9999
),
'SA' => array(
5000, 5999
),
'WA' => array(
6000, 6797,
6800, 6999
),
'TAS' => array(
7000, 7999
),
'NT' => array(
800, 999
)
);
$exceptions = array(
0800 => 'NT',
872 => 'NT',
2540 => 'NSW',
2611 => 'ACT',
2620 => 'NSW',
3500 => 'VIC',
3585 => 'VIC',
3586 => 'VIC',
3644 => 'VIC',
3707 => 'VIC',
2899 => 'NSW',
6798 => 'WA',
6799 => 'WA',
7151 => 'TAS'
);
$postcode = intval($postcode);
if ( array_key_exists($postcode, $exceptions) ) {
return $exceptions[$postcode];
}
foreach ($ranges as $state => $range)
{
$c = count($range);
for ($i = 0; $i < $c; $i+=2) {
$min = $range[$i];
$max = $range[$i+1];
if ( $postcode >= $min && $postcode <= $max ) {
return $state;
}
}
}
return null;
}
//Redirect for NT
if ($state = NT)
{
header( "Location: http://www.justistuff.com.au/ntrepairs.php" );
setcookie("postcode", $postcode, time()+259200);
}
//Redirect for QLD
if ($state = QLD)
{
header( "Location: http://www.justistuff.com.au/mailin.php" );
setcookie("postcode", $postcode, time()+259200);
}
//Redirect for VIC
if ($state = VIC)
{
header( "Location: http://www.justistuff.com.au/mailin.php" );
setcookie("postcode", $postcode, time()+259200);
}
//Redirect for ACT
if ($state = ACT)
{
header( "Location: http://www.justistuff.com.au/mailin.php" );
setcookie("postcode", $postcode, time()+259200);
}
//Redirect for NSW
if ($state = NSW)
{
header( "Location: http://www.justistuff.com.au/mailin.php" );
setcookie("postcode", $postcode, time()+259200);
}
//Redirect for WA
if ($state = WA)
{
header( "Location: http://www.justistuff.com.au/mailin.php" );
setcookie("postcode", $postcode, time()+259200);
}
//Redirect for TAS
if ($state = TAS)
{
header( "Location: http://www.justistuff.com.au/mailin.php" );
setcookie("postcode", $postcode, time()+259200);
}
//Redirect for SA
if ($state = SA)
{
header( "Location: http://www.justistuff.com.au/sarepairs.php" );
setcookie("postcode", $postcode, time()+259200);
}
即使输入的值(例如 2142)被明确标记为 NSW,我似乎也无法弄清楚是什么导致它重定向到 SA
【问题讨论】:
-
您是否在启用警告的情况下运行此代码,例如检查 php 日志中的消息。例如,我不知道你在这条线上的意思是什么
if ($state = NT)是 NT 在某处定义的吗?? -
也做这个,以便其他人可以运行它,你在哪里调用你的函数我看不出我怎么能尝试这个也考虑简化你的例子。我不知道重定向与您的问题有什么关系?!
-
我还没有运行任何调试(仍在探索 PHP 的世界),我认为 NT 是在代码最初根据输入的邮政编码计算状态时进一步定义的,然后基于在那个选择是SA,NT,WA,QLD等。我一定是错的!
-
嗨,Brandin,您建议我将它发布到哪里以便其他人可以运行它?谢谢
-
检查日志并不是真正的调试,它是你如何检查脚本中的错误。基本上,您的目标应该是删除所有警告和通知。它看起来不会运行,因为我没有看到你在哪里调用你的函数 findState