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";
}
?>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//$ret = 0;
is uncommented, ths function runs just fine and returns a 0 just as expected:
Code: Select all
int(0)
No match foundCode: 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