邮政编码检查错误

时间:2014-08-09 06:21:29

标签: php zipcode

我正在尝试为澳大利亚的少数商店构建邮政编码检查程序。基本上,客户输入他们的邮政编码,然后该网站将重定向到适当的本地定价/服务。

我使用this作为基础,但我遇到了问题(也在该网站上的评论中列出),无论输入什么值,由于某种原因,代码将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)明确标记为新南威尔士州

,我似乎无法找出导致它重定向到SA的原因。

2 个答案:

答案 0 :(得分:1)

if ($state = VIC)

这有两个问题。首先=用于分配,而不是用于比较。其次,VIC是一个字符串,应该在引号中,如

if ($state == 'VIC')

修复所有if语句并确保您的利益。

,在开始比较需要调用该函数的状态列表以查看邮政编码所属的状态之前,你不是在任何地方调用findState函数

$state=findState($postcode);

//if($state=="thisthat")

答案 1 :(得分:0)

这是经典的==问题,如果你使用赋值运算符而不是相等运算符。

此外,我建议您在使用exit时使用header redirect,这样可确保下一行代码无法执行。

从部分代码中解释问题。您使用if语句进行比较,然后将值分配给$ state而不是相等。这导致true为if,因此代码if块将被执行。由于您没有exit,因此会在if下解析。

我建议将您的if更改为switch,其中包含break