Comparing items in lists PHP

Hi all i’m very new to PHP, what i’m attempting to do is check item number 1 of list one against item number 1 of list 2, then item number 2 of list 1 against item number 2 of list 2…etc, below is the code that i have but it’s causing me some errors, i don’t need to check each item of one list against all the items of another, i simply need to check the item in position x against the item in position x on the other list. It’s for redirecting IP’s to my forums that fall within a range, so my lists may look like:

List1:
127.0.0.0
135.1.1.0
…etc

List2:
127.0.0.20
150.2.0.15

So my only compares would be to see if the REMOTE_ADDR falls in the range of item 1 list 1 - item 1 list 2 or item 2 list 1 - item 2 list 2.

Am i making sense?, hope you can help
[php]function bip_redirect($to, $code = ‘301 Permanently Moved’) {
header(“HxxP/1.1 “.$code);
header(“Location: hxxp://$to”);
exit();
}
if ($vbulletin->options[‘banip_range_block_active’])
{
$blk_ip = strtoupper($_SERVER[‘REMOTE_ADDR’]);
$ipstrt = explode(”\r\n”, $vbulletin->options[‘banip_block_start’]);
$ipend = explode("\r\n", $vbulletin->options[‘banip_range_end’]);
$n = sizeof($ipstrt);
$ne = sizeof($ipend);
for ($i=0;$i<$n;$i++;$ia=0;$ia<$ne;$ia++) {
if (stristr($blk_ip >= $ipstrt[$i] && $blk_ip <=$ipend[$ia]))
{
echo “($vbulletin->options[‘banip_message’])”;
bip_redirect($vbulletin->options[‘banip_redirect’]);
}
}
}
[/php]

Your ‘for’ loop is not correct. You can see correct syntax here :http://php.net/manual/en/control-structures.for.php.
If each element of $ipstrt array, have corresponding element in the $ipend array, then you just need to use the same index to access corresponding elements in both arrays. Like this:
[php]
for ($i=0;$i<$n;$i++) {
echo $ipstrt[$i].’ - ‘.$ipend[$i].’
’;
}[/php]

Also, this condition is not correct:
[php]if (stristr($blk_ip >= $ipstrt[$i] && $blk_ip <=$ipend[$ia]))[/php]

I would parse each IP (explode with dot delimiter) and compare each token, to check if IP fall within a range.

Thanks for the reply, i get you on the for loop and half suspected i might try that next, as for your other comment i have no idea how to do what you explained, all the code you see above i’d cobbled together from other things, i though that IF condition would work to check between the two if i used it like this[php]if (stristr($blk_ip >= $ipstrt[$i] && $blk_ip <=$ipend[$i]))
[/php]Could you help sort that part out please?

Ok, this now does not give an error but are you saying it wont work?[php]function bip_redirect($to, $code = ‘301 Permanently Moved’) {
header(“HxxP/1.1 “.$code);
header(“Location: hxxp://$to”);
exit();
}
if ($vbulletin->options[‘banip_range_block_active’])
{
$blk_ip = strtoupper($_SERVER[‘REMOTE_ADDR’]);
$ipstrt = explode(”\r\n”, $vbulletin->options[‘banip_block_start’]);
$ipend = explode("\r\n", $vbulletin->options[‘banip_range_end’]);
$n = sizeof($ipstrt);
for ($i=0;$i<$n;$i++) {
if (stristr($blk_ip >= $ipstrt[$i] && $blk_ip <=$ipend[$i]))
{
echo $vbulletin->options[‘banip_message’];
bip_redirect($vbulletin->options[‘banip_redirect’]);
}
}
}[/php]

Try to replace this condition in your code:
[php]if (stristr($blk_ip >= $ipstrt[$i] && $blk_ip <=$ipend[$i]))[/php]

with this:
[php]if (ip2long($blk_ip) >= ip2long($ipstrt[$i]) && ip2long($blk_ip) <= ip2long($ipend[$i]))[/php]

It doesn’t show a fault, i’ll test it over the next hour or so and post back - thanks for your prompt reply :slight_smile:

actually, I think correct use of ip2long() function for this task would be this:
[php]$ip_start = (float)sprintf("%u",ip2long($ipstrt[$i]));
$ip_end = (float)sprintf("%u",ip2long($ipend[$i]));
$ip = (float)sprintf("%u",ip2long($blk_ip));

if ($ip >= $ip_start and $ip <= $ip_end)
// the rest of code
[/php]

Im confused now, do you mean like this[php]function bip_redirect($to, $code = ‘301 Permanently Moved’) {
header(“HTTP/1.1 “.$code);
header(“Location: http://$to”);
exit();
}
if ($vbulletin->options[‘banip_range_block_active’])
{
$blk_ip = strtoupper($_SERVER[‘REMOTE_ADDR’]);
$ipstrt = explode(”\r\n”, $vbulletin->options[‘banip_block_start’]);
$ipend = explode("\r\n", $vbulletin->options[‘banip_range_end’]);
$n = sizeof($ipstrt);
for ($i=0;$i<$n;$i++) {
$ip_start = (float)sprintf("%u",ip2long($ipstrt[$i]));
$ip_end = (float)sprintf("%u",ip2long($ipend[$i]));
$ip = (float)sprintf("%u",ip2long($blk_ip));
if ($ip >= $ip_start and $ip <= $ip_end)
{
echo $vbulletin->options[‘banip_message’];
bip_redirect($vbulletin->options[‘banip_redirect’]);
}
}
}[/php]

Yes, you got it right.

Thanks once again, just added that, will post back in a while and let you know how it goes :slight_smile:

It appears to be working, well if i range my ip anyway, so thanks again, although if i find it’s not really working and it was just me then i’ll post back :slight_smile:

Hi me again, there may be something i’m missing or maybe i need a wait or delay command but, my echo doesn’t seem to be appearing before the redirect[php]function bip_redirect($to, $code = ‘301 Permanently Moved’)
{
header(“HTTP/1.1 “.$code);
header(“Location: http://$to”);
exit();
}
if ($vbulletin->options[‘banip_range_block_active’])
{
$blk_ip = strtoupper($_SERVER[‘REMOTE_ADDR’]);
$ipstrt = explode(”\r\n”, $vbulletin->options[‘banip_block_start’]);
$ipend = explode("\r\n", $vbulletin->options[‘banip_range_end’]);
$n = sizeof($ipstrt);for ($i=0;$i<$n;$i++)
{
$ip_start = (float)sprintf("%u",ip2long($ipstrt[$i]));
$ip_end = (float)sprintf("%u",ip2long($ipend[$i]));
$ip = (float)sprintf("%u",ip2long($blk_ip));
if ($ip >= $ip_start and $ip <= $ip_end)
{
echo $vbulletin->options[‘banip_message’];
bip_redirect($vbulletin->options[‘banip_redirect’]);
}
}
}[/php]I’m not sure whether it should look like this: [php]echo ($vbulletin->options[‘banip_message’[/php]]);
or like this: [php]echo “($vbulletin->options[‘banip_message’])”; [/php] Or as it appears, maybe it needs a delay between the echo and the redirect, i’d like to display a polite message before they get redirected, thats why i had that echo there or should it be print? - i’m confused again :slight_smile:

Hi, i’ve been researching this and it seems that i could put[php]sleep(10);[/php]in between the items i want a delay in execution like this[php]{echo $vbulletin->options[‘banip_message’];
sleep(10);
bip_redirect($vbulletin->options[‘banip_redirect’]);}[/php]Will that work for everyone? i mean for every browser, my site uses PHP5.2.9

You can not echo any message before redirecting by sending http headers from PHP (check this). If you want to display a message and then, after some delay, redirect user to another page, you need to use client-side technique such as Javascript or meta refresh.

Sponsor our Newsletter | Privacy Policy | Terms of Service