Jump to content

in_array


mbrown

Recommended Posts

array_search may help you, you can pass in an array and check if the values are in the array, if they are it returns you the indexes.

 

<?php
$ipSearch = array('192.168.0.1', '192.168.0.2', '192.168.9.1');
$ipaddresses = array('192.168.0.3', '192.168.0.2', '192.168.1.1', '192.168.9.1');

$found = array_search($ipSearch, $ipaddresses);

if (count($found) > 0) {
    foreach ($found as $key) {
           echo "{$ipaddresses[$key]} was found at index {$key}.<br />";
    }
}
?>

Link to comment
https://forums.phpfreaks.com/topic/146689-in_array/#findComment-770135
Share on other sites

array_search may help you, you can pass in an array and check if the values are in the array, if they are it returns you the indexes.

 

<?php
$ipSearch = array('192.168.0.1', '192.168.0.2', '192.168.9.1');
$ipaddresses = array('192.168.0.3', '192.168.0.2', '192.168.1.1', '192.168.9.1');

$found = array_search($ipSearch, $ipaddresses);

if (count($found) > 0) {
    foreach ($found as $key) {
           echo "{$ipaddresses[$key]} was found at index {$key}.<br />";
    }
}
?>

 

so if the ips are in $ipSearch are in $ipaddress?

 

I will be doing it by some ip addresses so certain people can access it off campus but my issue is doing it by subnet at least right now bc some of the subnets are are between 3 and 7 characters xxx.xxx.

 

That is what I am trying to battle right now.

Link to comment
https://forums.phpfreaks.com/topic/146689-in_array/#findComment-770141
Share on other sites

array_search may help you, you can pass in an array and check if the values are in the array, if they are it returns you the indexes.

 

 

The above is bad. My internet connection died out so I could not fix it.

 

<?php
$ipSearch = array('192.168.0.1', '192.168.0.2', '192.168.9.1');
$ipaddresses = array('192.168.0.3', '192.168.0.2', '192.168.1.1', '192.168.9.1');

$found = array_intersect($ipSearch, $ipaddresses);

if ($found !== false) {
foreach ($found as $val) {
	echo "{$val} was in both arrays.<br />";
}
}

die();
?>

 

array_intersect_keys should give you the indexes if that is what you are after.

Link to comment
https://forums.phpfreaks.com/topic/146689-in_array/#findComment-770154
Share on other sites

so i understand this fully, $ipsearch are the ones that you want to test and $ipaddresses are the okay ones so i could do the following

 

 

<?php
$ipaddress = $_SERVER['REMOTE_ADDR'];
$ipSearch = array('$ipaddress');
$ipaddresses = array('192.168.0.3', '192.168.0.2', '192.168.1.1', '192.168.9.1');

$found = array_intersect($ipSearch, $ipaddresses);

if ($found == false) 
{
  echo "You can not access this page based on your IP Address";
}

die();

?>

[/code]

Link to comment
https://forums.phpfreaks.com/topic/146689-in_array/#findComment-770188
Share on other sites

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.