HomePHP Page 3 - Retrieving System Information With patSysinfo
What's in a Name? - PHP
Linux file structure contains within it a special area called /proc. Now, some believe that there's black magic in that directory. For those who know better than to fear the /proc, there awaits much good magic, in the form of server info. Looking for a way to retrieve real-time server information and display it to users in your Web application? Today's your lucky day! Take a look at the patSysinfo PHP class, which lets you do that and a whole lot more.
Now that the hard sell is over and you're (hopefully) all set up with patSysinfo, let's take a simple example to see how it works.
<?php // include class include("patSysinfo.php");
// instantiate object $sys = new patSysinfo();
// get hostname $name = $sys->getHostName();
// get host IP address $ip = $sys->getHostIp();
// output values echo "This system is named $name and has IP address $ip"; ? >
Here's the output:
This system is named olympus.local.net and has IP address 192.168.0.2
Let's dissect this to see how it works.
1. The first step is, obviously, to include all the relevant files for the class to work.
// include class include("patSysinfo.php");
Once that's done, I can safely create an object of the patSysinfo class.
// instantiate object $sys = new patSysinfo();
This object instance will serve as the central source for all system information, allowing me to do all kinds of nifty things with it.
2. Next, the object's getHostName() and getHostIp() methods are used to obtain the system's host name and IP address respectively.
// get host name $name = $sys->getHostName();
// get host IP address $ip = $sys->getHostIp();
Most of the patSysinfo API calls look like this: the word "get" followed by the information that is required. In the example above, the object first looks up the file "/proc/sys/kernel/hostname" for the current hostname, and then obtains the IP address using the host name and the gethostbyname() PHP function
4. Finally, all that's left is to actually use the data retrieved by the methods - in this case, print it all to the standard output device.
// output values echo "This system is named $name and has IP address $ip";