Jump to content

[SOLVED] accessing arrays loop


mrjgx

Recommended Posts

help guys. i have a problem here. whenever i tried to access the array list, this problem occurs:

 

Warning: Invalid argument supplied for foreach() in f:\mddd\www\altec\screen.php on line 120

 

//to query network client's running software

 

 

$detection = @ snmpwalk($ip_add, "public", ".1.3.6.1.2.1.25.4.2.1.2", 5);

 

 

foreach ($detection as $val) {

   

 

if ($val == "firefox.exe") {

 

echo "firefox is opened";

 

}

 

 

elseif ($val == "IEXPLORE.EXE") {

 

echo "Internet Explorer is opened";

 

}

 

 

 

}

 

 

is there something wrong? it seems that i couldnt match the array's character using the if else condition..

 

thx in advance

 

Link to comment
Share on other sites

thanks for your reply. i cannot make it into an array because it is a function that is needed.

 

$detection = array($ip_add, "public", ".1.3.6.1.2.1.25.4.2.1.2", 5);

 

i need to use the function snmpwalk to get the list of running software names.

 

i can echo $val<BR> and it will print out all the running names like this:

 

"System Idle Process"

"System"

"MDM.EXE"

"MioNetManager.exe"

"Ymsgr_tray.exe"

"avgcc.exe"

"ati2evxx.exe"

"explorer.exe"

"smss.exe"

"atiptaxx.exe"

"sm56hlpr.exe"

"csrss.exe"

"googletalk.exe"

"winlogon.exe"

"services.exe"

"lsass.exe"

"SOUNDMAN.EXE"

"ati2evxx.exe"

"svchost.exe"

"svchost.exe"

"DATALA~1.EXE"

"svchost.exe"

"TRAYAP~1.EXE"

"Apache.exe"

"SearchProtection.exe"

"daemon.exe"

"svchost.exe"

"Dreamweaver.exe"

"PhiBtn.exe"

"svchost.exe"

"Tray900.exe"

"AppleMobileDeviceService.exe"

"ctfmon.exe"

"spoolsv.exe"

"avgamsvr.exe"

"Apache.exe"

"WLANUTL.exe"

"avgupsvc.exe"

"avgemc.exe"

"DkService.exe"

"SERVIC~1.EXE"

"ijplmsvc.exe"

"sqlservr.exe"

"MioNet.exe"

"mysqld.exe"

"snmp.exe"

"svchost.exe"

"firefox.exe"

"wdfmgr.exe"

"IEXPLORE.EXE"

"alg.exe"

"wscntfy.exe"

"svchost.exe"

 

 

i need to know how to match the list of names using the loop? is there a way to do that??

 

thanks

Link to comment
Share on other sites

ok now i finally got no errors. the code runs well, only thing that i need to know is how to match the arrays from snmpwalk function into the if-else condition.

 

by using this code:

 

foreach ($detection as $val) {

 

    if ($val == "firefox.exe") {

 

      echo "firefox is opened";

 

  }

     

  elseif    ($val == "IEXPLORE.EXE") { 

 

      echo "Internet Explorer is opened";

     

  }

 

}

 

but the echo doesnt appear, meaning that it cannot match the array names. is there a way to access the arrays 1 by 1 and match the strings using if-else condition?

 

thank you.

Link to comment
Share on other sites

you can try that......

<?php

$val= snmpwalk($ip_add, "public", ".1.3.6.1.2.1.25.4.2.1.2", 5);

     if (in_array("firefox.exe",$val)) {
   
      echo "firefox is opened";
   
   }
     
   elseif (in_array("IEXPLORE.EXE",$val)) {   
   
      echo "Internet Explorer is opened";
     
   }

}
?>

 

you sure the elseif is even valid say you use both i do...

Link to comment
Share on other sites

Thanks for the reply..but i get this error message instead.

 

Warning: in_array(): Wrong datatype for second argument in f:\mddd\www\altec\screen.php on line 120

Warning: in_array(): Wrong datatype for second argument in f:\mddd\www\altec\screen.php on line ....

Warning: in_array(): Wrong datatype for second argument in f:\mddd\www\altec\screen.php on line ....

Warning: in_array(): Wrong datatype for second argument in f:\mddd\www\altec\screen.php on line ...n

 

is there a way to rectify that?

 

thanks

Link to comment
Share on other sites

are you sure that your snmpwalk function is returning the results you are expecting?

 

use print_r to view the contents of the array:

 

echo '<pre>' . print_r($detection, true) . '</pre>';

 

if it is an array, then use some normalization on the data returned to make sure it's matching your string:

 

if (in_array("firefox.exe",strtolower(trim($val)))) {
echo "firefox is opened";
} else if (in_array("iexplore.exe",strtolower(trim($val)))) {   
echo "Internet Explorer is opened";
}

Link to comment
Share on other sites

hey thanks..the print_r function will displays this list:

 

Array

(

    [0] => "System Idle Process"

    [1] => "System"

    [2] => "MDM.EXE"

    [3] => "MioNetManager.exe"

    [4] => "Ymsgr_tray.exe"

    [5] => "avgcc.exe"

    [6] => "ati2evxx.exe"

    [7] => "explorer.exe"

    [8] => "smss.exe"

    [9] => "atiptaxx.exe"

    [10] => "sm56hlpr.exe"

    [11] => "csrss.exe"

    [12] => "googletalk.exe"

    [13] => "winlogon.exe"

    [14] => "services.exe"

    [15] => "lsass.exe"

    [16] => "SOUNDMAN.EXE"

    [17] => "ati2evxx.exe"

    [18] => "svchost.exe"

    [19] => "svchost.exe"

    [20] => "DATALA~1.EXE"

    [21] => "svchost.exe"

    [22] => "TRAYAP~1.EXE"

    [23] => "Apache.exe"

    [24] => "SearchProtection.exe"

    [25] => "daemon.exe"

    [26] => "svchost.exe"

    [27] => "Dreamweaver.exe"

    [28] => "PhiBtn.exe"

    [29] => "svchost.exe"

    [30] => "Tray900.exe"

    [31] => "AppleMobileDeviceService.exe"

    [32] => "ctfmon.exe"

    [33] => "spoolsv.exe"

    [34] => "avgamsvr.exe"

    [35] => "Apache.exe"

    [36] => "WLANUTL.exe"

    [37] => "avgupsvc.exe"

    [38] => "avgemc.exe"

    [39] => "DkService.exe"

    [40] => "SERVIC~1.EXE"

    [41] => "ijplmsvc.exe"

    [42] => "sqlservr.exe"

    [43] => "MioNet.exe"

    [44] => "mysqld.exe"

    [45] => "snmp.exe"

    [46] => "svchost.exe"

    [47] => "firefox.exe"

    [48] => "wdfmgr.exe"

    [49] => "IEXPLORE.EXE"

    [50] => "alg.exe"

    [51] => "wscntfy.exe"

    [52] => "svchost.exe"

)

 

 

but the normalization on the data returned this error message:

 

Warning: in_array(): Wrong datatype for second argument in f:\mddd\www\altec\screen.php on line 124

 

 

i need to know how to excerpt the array which have firefox or iexplorer words in it..is there a way?

thx guys

 

 

 

Link to comment
Share on other sites

Try this:

<?php
$val= snmpwalk($ip_add, "public", ".1.3.6.1.2.1.25.4.2.1.2", 5);
foreach ($val as $exe)
    switch ($val) {
          case 'IEXPLORE.EXE':
              echo "Internet Explorer is opened";
              break;
          case 'firefox.exe':
              echo "firefox is opened";
              break;
    }
?>

 

Ken

Link to comment
Share on other sites

Thanks kenrbnsn..your switch code is kind same with my original switch code that i made..

 

finally i found the solutions to this problem...i just need to put the " ".

 

this is the working code:

 

foreach ($val as $exe)

 

    switch ($exe) {

         

  case '"IEXPLORE.EXE"':

              echo "Internet Explorer is opened<BR>";

                break;

       

 

case '"firefox.exe"':

            echo "firefox is opened<BR>";

            break;

 

 

    }

 

voila..all the problems solved..i can access each items in the array list...

 

MILLION THANKS to all helpers. i'm so happy  :D  ;D

 

 

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.