Page 1 of 1

strange behaviour of "continue 2" - wrong return values

Posted: Tue Jun 16, 2009 11:29 am
by fannnn
hi all,

i have the following script:

Code: Select all

<?php
function get_baseurl($ip) {
    $ip_parts = explode('.', $ip);
 
    $data = array('192.168.1.1:url.or.ip.of.the.server1.com',
        '192.167.*.*:url.or.ip.of.the.server3.com',
        '192.169.1.1-192.255.255.255:url.or.ip.of.the.server4.com');
 
    foreach ($data as $line) {
        $line_parts = explode(':', $line);
 
        $range = $line_parts[0];
        $baseurl = $line_parts[1];
 
        if (strpos($range, '*')) {
            $range_parts = explode('.', $range);
 
            for ($i = 0; $i < 4; $i++) {
                if ($range_parts[$i] == '*') {
                    continue;
                } else if ($ip_parts[$i] != $range_parts[$i]) {
                    continue 2;
                }
            }
 
            return $baseurl;
        }
    }
 
    //$ret = 0;
    return 0;
}
 
$ip = $_SERVER['REMOTE_ADDR'];
$baseurl = get_baseurl($ip);
 
var_dump($baseurl);
 
if (!$baseurl) {
    echo "No match found";
} else {
    echo "Remote IP: $ip<br>Redirecting to: $baseurl";
}
 
?>
it is very much stripped down to the essentials. its purpose is to take an array of ip-server-mappings and then check for a given ip, which mapping it conforms to. this is done by iterating over the items of the mapping-array (which come from a text file originally) and checking for each line, whether the ip matches the definition. ive stripped out all the working parts, the part left is the one dealing with wildcards.
please take a look for yourself. when you run the script, and you provide an ip that doesnt match the definitions (127.0.0.1), the function get_baseurl() should return a 0. yet it returns an obscure value, which is easily seen in the output by using var_dump(). this is the actual output of the script, when calling it from localhost:

Code: Select all

int(38811264)
Remote IP: 127.0.0.1
Redirecting to: 38785352
i have no clue whatsoever how this is caused. to make things even stranger: when the line
//$ret = 0;
is uncommented, ths function runs just fine and returns a 0 just as expected:

Code: Select all

int(0)
No match found
when called with a matching ip (192.167.1.1) it works fine also:

Code: Select all

string(28) "url.or.ip.of.the.server3.com"
Remote IP: 192.167.1.1
Redirecting to: url.or.ip.of.the.server3.com
any ideas?

Re: strange behaviour of "continue 2" - wrong return values

Posted: Tue Jun 16, 2009 3:04 pm
by Eric!
Your code works correctly for me. I just changed the one line for $ip for testing.
$ip = '127.0.0.1';

I left the rest alone.