1

all!

So, I have this code:

<?php
$clienthwid = $_POST["clienthwid"];
$clientip = $_POST["clientip"];
$hwid_logs = "hwid_log.txt";
$ip_logs = "ip_log.txt";
$handle_ip = fopen($ip_logs, 'a') or die("404 file not found");
$handle_hwid = fopen($hwid_logs, 'a') or die("404 file not found");
$client_whitelist = array (
    // put hwids and ip here
    "hwid" => "123456789", "12345678",
    "ip" => "123.456.789", "123.456.788",
);
//check if client hwid or ip is in the array
if (in_array($clienthwid, $client_whitelist)) {
    echo "TRUE";
    fwrite($handle_hwid, $clienthwid."\n");
    fwrite($handle_ip, $clientip."\n");
} else {
    echo "FALSE";
    fwrite($handle_hwid, $clienthwid."\n");
    fwrite($handle_ip, $clientip."\n");
}
?>

So, for the

in_array($clienthwid, $client_whitelist);

I would like to know how to do this

in_array($clienthwid and $clientip, $client_whitelist)

How do I check if two variables are in an array?

Marvin Fischer
  • 2,552
  • 3
  • 23
  • 34
Ryan
  • 29
  • 3
  • Is your client whitelist array in the correct format? Should it not have sub arrays to group hwid and ip values? – Progrock Jun 17 '16 at 10:18
  • Possible duplicate of [this](http://stackoverflow.com/a/7542708/5447994) – Thamilhan Jun 17 '16 at 10:20
  • Possible duplicate of [in\_array multiple values](http://stackoverflow.com/questions/7542694/in-array-multiple-values) – gre_gor Jun 23 '16 at 16:40

3 Answers3

2

Just use two in_array statements.

in_array($clienthwid, $client_whitelist) && in_array($clientip, $client_whitelist)

This would only be true if both are in $client_whitelist. If you want to determine if at least on is in there use the || or operator.

//check if client hwid or ip is in the array
if (in_array($clienthwid, $client_whitelist) || in_array($clientip, $client_whitelist)) {
    echo "TRUE";
    fwrite($handle_hwid, $clienthwid."\n");
    fwrite($handle_ip, $clientip."\n");
} else {
    echo "FALSE";
    fwrite($handle_hwid, $clienthwid."\n");
    fwrite($handle_ip, $clientip."\n");
}
cb0
  • 8,415
  • 9
  • 52
  • 80
1

Using array_intersect

$array = array("AA","BB","CC","DD");
$check1 = array("AA","CC");
$check2 = array("EE","FF");

if(array_intersect($array, $check1)) {
    echo "Exist";
}else{
    echo "Not exist"; 
}

if(array_intersect($array, $check2)) {
    echo "Exist";
}else{
    echo "Not exist"; 
}

Refer: 3v4l

zakhefron
  • 1,403
  • 1
  • 9
  • 13
0

The function bellow will check if all given items are in the target array. It makes use of a foreach and is fairly simple.

if (multiple_in_array(array('onething', 'anotherthing'), $array)) {
    // 'onething' and 'anotherthing' are in array
}

function multiple_in_array($needles = array(), $haystack = array())
{
    foreach ($needles as $needle) {
        if (!in_array($needle, $haystack)) {
            return false;
        }
    }
    return true;
}
Peter
  • 8,776
  • 6
  • 62
  • 95