Jump to content

strange behaviour of "continue 2" - wrong return values


fannnn

Recommended Posts

hi all,

 

i have the following script:

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

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:

int(0)
No match found

 

when called with a matching ip (192.167.1.1) it works fine also:

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?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.