Retrieving System Information With patSysinfo - What's in a Name?
(Page 3 of 9 )
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";
Next: Up and at 'Em >>
More PHP Articles
More By icarus, (c) Melonfire