The Family Dashboard

I've written a little about PHP before, because I think it's a great utility language for writing quick things you need to do. Plus, it allows you to use a web browser as your interface, and everyone has a web browser. That makes it very convenient for my family, because I can make simple web interfaces for the various things I normally have to do from the command line. (This is extremely useful when I'm gone to a conference and the Plex server needs to be rebooted, or any of a dozen other things need to be done that are hard to explain over the phone.)

My "Family Dashboard" will look different from yours, but the concept is pretty simple. PHP allows you to execute local functions on the server, and so as long as you can create a bash script that does what you need it to do, it can be launched from the "dashboard" you create for your family. Here's a sample dashboard file I've created, so you can see how simple it is to create a custom page that does what you need it to do (see Figure 1 for a screenshot of the dashboard in action):


<html><head><title>My Dashboard</title></head>
<body>
<h3>You need to enter some commands and possibly options,
 ↪or just press a button:<br />
<button onclick="window.location='lj.php?command=weather&
↪option=houston'">Weather</button>
<button onclick="window.location='lj.php?command=bing'">Bing
 ↪Photo</button>
<button onclick="window.location='lj.php?command=uname'">Kernel
 ↪Name</button>
<button onclick="window.location='lj.php?command=time'">Unix
 ↪Time</button>
</h3>

<?php

$command = $_GET['command'];
$option = $_GET['option'];

switch ($command)
{
    case "weather":
        echo file_get_contents("http://wttr.in/$option");
        break;
    case "time":
        echo time() . "  <-- that's how I read time! I'm a robot!";
        break;
    case "bing":
        $json = json_decode(file_get_contents("http://www.bing.com/
↪HPImageArchive.aspx?format=js&idx=0&n=1&mkt=en-US"), TRUE);
        $url = "http://bing.com" . $json['images']['0']['url'];
        echo "Here is the image of the day:\n";
        echo "<img src=$url />";
        break;
    case "uname":
        echo shell_exec("uname -a");
        break;
    default:
        echo "<h1>Press a button!</h1>";

}

?>
</body></html>

Figure 1. My dashboard is simple, but it's just a front end for the code beneath.

First off, copy and paste that code into a file called lj.php and save it onto your local web server. The server needs to have PHP active, but I'll leave that as an exercise for the reader to set up. I've written about installing a LAMP stack before, so it shouldn't be too challenging to get a web server running with PHP support (see my article "PHP for Non-Developers" from the December 2014 issue) Also, naming the file "lj.php" is only important because if you look at the code, it references itself. If you name it something different, just change the references in the HTML/PHP code.

______________________

Shawn Powers is a Linux Journal Associate Editor. You might find him on IRC, Twitter, or training IT pros at CBT Nuggets.