mbrown Posted February 24, 2009 Share Posted February 24, 2009 if(!in_array($ipaddressubnet,$ipaddresses)) .... That is part of my code portion of a page. I would like to have many variables like $ipaddresssubnet, $ipaddresssubnet1, $ipaddresssubnet2, etc to see if it is in the $ipaddresses array Can I do something like that with the in_array function? Quote Link to comment https://forums.phpfreaks.com/topic/146689-in_array/ Share on other sites More sharing options...
sasa Posted February 24, 2009 Share Posted February 24, 2009 http://hr.php.net/array_intersect Quote Link to comment https://forums.phpfreaks.com/topic/146689-in_array/#findComment-770096 Share on other sites More sharing options...
mbrown Posted February 24, 2009 Author Share Posted February 24, 2009 I understand how this works but not sure completely how it is the right thing for me Quote Link to comment https://forums.phpfreaks.com/topic/146689-in_array/#findComment-770127 Share on other sites More sharing options...
premiso Posted February 24, 2009 Share Posted February 24, 2009 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 />"; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/146689-in_array/#findComment-770135 Share on other sites More sharing options...
mbrown Posted February 24, 2009 Author Share Posted February 24, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/146689-in_array/#findComment-770141 Share on other sites More sharing options...
mbrown Posted February 24, 2009 Author Share Posted February 24, 2009 that code you gave me i get invalid argument supplied for foreach() Quote Link to comment https://forums.phpfreaks.com/topic/146689-in_array/#findComment-770147 Share on other sites More sharing options...
premiso Posted February 24, 2009 Share Posted February 24, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/146689-in_array/#findComment-770154 Share on other sites More sharing options...
mbrown Posted February 24, 2009 Author Share Posted February 24, 2009 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] Quote Link to comment https://forums.phpfreaks.com/topic/146689-in_array/#findComment-770188 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.