I'm trying to filter some logs like I need them and tried to it dynamic. I have some domains and I'm trying to filter some things from it, and it all works like I want it - but now I changed the domain name and now my code doesn't work anymore. It says one variable isn't defined.
$sp_bots = shell_exec("grep bot | awk '{print $12}' /var/www/laravel/logs/vhosts/zahnmedizinisches-fachzentrum-berlin.de.log");
$array_sp_bots = explode("
", $sp_bots);
$all_bots = array();
foreach($array_sp_bots as $bots){
if(strpos($bots, "bot")){
$all_bots[] = $bots;
}
}
# count values of strings in array
if (!empty( $all_bots )) {
$bots = array_count_values($all_bots);
arsort($bots);
$mostOccuring = max(array_count_values($all_bots));
$bot_keys = array_keys($bots);
#number of total bots
$count_bots = count($all_bots);
}
and in my returns:
return view('/domains/data', [
'count_bots' => $count_bots,
'bot_keys' => $bot_keys,
'mostOccuring' => $mostOccuring,
]);
but all three variables in my return are undefined.. anybody knows why?
You have to initialize the array as an empty array before the loop:
$all_bots = array(); //init the empty array
foreach($array_sp_bots as $bots)
{
if(strpos($bots, "bot"))
{
$all_bots[] = $bots; //here you can add elements to the array
}
}
in your case, if the loop does not execute at least one time, the variable $all_bots
will be undefined
EDIT
After the loop, to handle the case when the array is empty do somthing like this:
//if there is some element in all_bots...
if ( ! empty( $all_bots ) )
{
# count values of strings in array
$bots = array_count_values($all_bots);
arsort($bots);
$mostOccuring = max(array_count_values($all_bots));
$bot_keys = array_keys($bots);
#number of total bots
$count_bots = count($all_bots);
}
//handle the case the variable all_bots is empty
else
{
$bots = 0;
$count_bots = 0;
$bot_keys = 0;
$mostOccuring = 0;
}
EDIT2
You have the variables undefined in your return because when all $all_bots
is empty they are not set. Check my edit above, i have added them to the if statement. But you have to handle this case in your application according to your needs, think this way: What these variables should contain when $all_bots
is empty? Then assign the values to the variables in the if statement
It is happening because after changing the domain it is not executing inside the loop. Try with -
$all_bots= array(); // Define an empty array
foreach($array_sp_bots as $bots){
if(strpos($bots, "bot")){
$all_bots[] = $bots;
}
}
# count values of strings in array
$bots = array_count_values($all_bots);
If the $array_sp_bots
is empty then it would not execute the loop & $all_bots
would not be defined. For that case the count would be 0
.
Or may might want to add some check for that -
if(empty($all_bots)) {
// Some error message
} else {
# count values of strings in array
$bots = array_count_values($all_bots);
arsort($bots);
$mostOccuring = max(array_count_values($all_bots));
$bot_keys = array_keys($bots);
#number of total bots
$count_bots = count($all_bots);
}