HiveBrain v1.2.0
Get Started
← Back to all entries
snippetphpMinor

Collect, filter and display archive logs on demand

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
collectlogsfilteranddisplaydemandarchive

Problem

Wrote a php site to gather/filter/search daily archived logs from the archive directory using bash cat and grep commands.

-
How can I make the code more readable?

-
How can I follow conventions better ? (functions/indentation/comments/ using php and html together etc)

-
Is there a different approach to this problem I should try?

UI:

CODE:


```
####### Get params for query : check both Post and Get ################
####### This part needs to go in to a function later #############

$isset=0; #used to check if at least one of the mandatory fields were checked - avoids returning the whole logfilefor fair players
$host = "";
if(isset($_POST['host'])){ #higher priority for post
$host = $_POST['host'];
$isset++;
}elseif(isset($_GET['host'])){ #check get only if post wasn't set
$host = $_GET['host'];
$isset++;
}
$service = "";
if(isset($_POST['service'])){
$service = $_POST['service'];
$isset++;
}elseif(isset($_GET['service'])){
$service = $_GET['service'];
$isset++;
}
$preserve = "";
if(isset($_POST['preserve'])){
$preserve = $_POST['preserve'];
}
# start input of 8 checkbox buttons
$ok = "";
if(isset($_POST['ok'])){
$ok = $_POST['ok'];
}elseif(isset($_GET['ok'])){
$ok = $_GET['ok'];
}
$warn = "";
if(isset($_POST['warn'])){
$warn = $_POST['warn'];
}elseif(isset($_GET['warn'])){
$warn = $_GET['warn'];
}
$crit = "";
if(isset($_POST['crit'])){
$crit = $_POST['crit'];
}elseif(isset($_GET['crit'])){
$crit = $_GET['crit'];
}
$Curr = "";
if(isset($_POST['curr'])){
$curr = $_POST['curr'];
}elseif(isset($_GET['curr'])){
$curr = $_GET['curr'];
}
$alert = "";
if(isset($_POST['alert'])){
$alert = $_POST['alert'];
}elseif(isset($_GET['alert'])){
$alert = $_GET['alert'];
}
$notif = "";
if(isset($_POST['notif'])){
$notif = $_POST['notif'];
}elseif(isset($_GET['notif'])){
$notif = $_GET['notif'];
}
$comm = "";
if(isset($_POST['comm'])){
$comm = $_POST['comm'];
}elseif(isset($_GET['

Solution

Instead of

$host = "";
if(isset($_POST['host'])){ #higher priority for post
    $host = $_POST['host'];
    $isset++;
}elseif(isset($_GET['host'])){ #check get only if post wasn't set
    $host = $_GET['host'];
    $isset++;
}


you might want to try something along the lines of

$host = (isset($_POST['host']) ? $_POST['host'] : '');
$host = (empty($host)          ? (isset($_GET['host']) ? $_GET['host'] : '')  : $host);


Do the same for the other parameters then, to check that you have at least one of the required parameters, use:

$isset = !empty($host) OR !empty($service);


Which lets you test to see if you have at least one of them using:

if ($isset)

Code Snippets

$host = "";
if(isset($_POST['host'])){ #higher priority for post
    $host = $_POST['host'];
    $isset++;
}elseif(isset($_GET['host'])){ #check get only if post wasn't set
    $host = $_GET['host'];
    $isset++;
}
$host = (isset($_POST['host']) ? $_POST['host'] : '');
$host = (empty($host)          ? (isset($_GET['host']) ? $_GET['host'] : '')  : $host);
$isset = !empty($host) OR !empty($service);
if ($isset)

Context

StackExchange Code Review Q#138927, answer score: 2

Revisions (0)

No revisions yet.