As a sysadmin, I end up doing some simple ad-hoc programming every once in a while. I'm trying to learn as I go along, so in general, is there anything in the code below that jumps out at you as being bad practise or otherwise unnecessary?
Specifically, the 3 if statements at the end feels like I'm duplicating code unnecessarily. Is there any way to shorten it further without going overboard with complexity?
<?php
define('TAKEN', 'Match: One');
define('AVAIL', 'Match: No Matches');
define('DATAMINE', 'Data mining count exceeded');
$ch = curl_init("http://co.za/cgi-bin/whois.sh?Domain=example");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
$output = curl_exec($ch);
function search_whois($findit) {
global $output;
if (strpos($output, $findit) === false)
return false;
if (is_int(strpos($output, $findit)))
return true;
}
if (search_whois(TAKEN))
echo "Domain is taken.\n";
if (search_whois(AVAIL))
echo "Domain is available.\n";
if (search_whois(DATAMINE))
echo "Blocked for datamining, try again later.\n";
// var_dump($output);
?>
Don't Repeat Yourself (DRY): Using DRY or Do not Repeat Yourself principle, you make sure that you stay away from duplicate code as often as you can. Rather you replace the duplicate code with abstractions or use data normalization. To reduce duplicity in a function, one can use loops and trees.
Having to change the same code multiple times harms your cycle time. If you have to apply a change in multiple places, then implementing that change will take longer. If the duplication is pervasive enough, it'll lead to a decreased delivery speed.
If you want to keep a consistent html structure for the sidebar on each page, use php. You would create a file called sidebar. php, and use the include method to import the same code into each page. If you want to change it later, edit the single sidebar.
You're not repeating unnecessarily, but I was confused because search_whois doesn't take a domain.
I'd reorganize so search_whois is self-contained
function search_whois($domain) {
$ch = curl_init("http://co.za/cgi-bin/whois.sh?Domain=$domain");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
$output = curl_exec($ch);
if (strpos($output, AVAIL) >= 0) {
echo "Domain is available.\n"
return true;
}
if (strpos($output, TAKEN) >= 0)
echo "Domain is taken.\n";
else if (strpos($output, DATAMINE) >= 0)
echo "Blocked for datamining, try again later.\n"
return false;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With