Jump to content

in_array


mbrown

Recommended Posts

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?

Link to comment
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 />";
    }
}
?>

Link to comment
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
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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.