Php script prints values but not seen in browser

greetings,

i'm a php and html novice and i figured i'd learn by diving in. i've written a php script that i've placed in the /var/www/html directory. it's supposed to give some relative info about the system when you point a browser at it. if i run the script using "php -q index.php" all the retrieved values are there, however when you point a browser at it the Hardware, Serial Number and Processor(s) values are blank. any suggestions(hints also appreciated) as to why the values are absent in the browser but present when run manually would be appreciated. i used `` rather than shell_exec to save on typing. while i'm sure there are other ways to do this and possibly smarter, you have to start somewhere:

<?php
$hostname = `hostname`;
$manufacturer = `dmidecode | grep Manufacturer | head -1 | cut -d: -f2`;
$model = `dmidecode | grep 'Product Name' | head -1 | cut -d: -f2`;
$serialno = `dmidecode | grep 'Serial Number' | head -1 | cut -d: -f2`;
$location = `cat /etc/snmp/snmpd.conf | grep syslocation | awk '{for (i=2; i<NF; i++) printf \$i " "; print \$NF}'`;
$contact = `echo "Contact Information: me at 734-555-5555"`;
$kernelarch = `uname -srvm`;
$processor = `dmidecode -s processor-version | head -1`;
$opsys = `cat /etc/redhat-release`;
$numprocessor = `cat /proc/cpuinfo | grep -c processor`;
$memtotal = `cat /proc/meminfo | grep MemTotal | awk '{print $2}'`;
$memvaluetotal = `expr $memtotal / 1000`; //i'm sure php can do math but that's for later
$memfree = `cat /proc/meminfo | grep MemFree | awk '{print $2}'`;
$memfreetotal = `expr $memfree / 1000`; //i'm sure php can do math but that's for later
$uptime = `uptime | awk '{print $3, $4}' | tr -d ,`;
?>

<p><b><u><big><?php echo $hostname;?></big></u></b><br/ >
<b>Hardware:</b><?php echo $manufacturer,$model;?><br/ >
<b>Serial Number:</b><?php echo $serialno;?><br/ ><p/ >

<p><b>Location: </b><?php echo $location;?><br/ >
<b>Contact Information: </b><?php echo $contact;?><p/ >

<p><b>Kernel and architecture: </b><?php echo $kernelarch;?><br/ >
<b>Processor(s): </b><?php echo $processor;?><br/ >
<b>Operating system: </b><?php echo $opsys;?><p/ >

<p><b>Processor total: </b><?php echo $numprocessor;?><br/ >
<b>Total memory: </b><?php echo $memvaluetotal;?>Mb<br/ >
<b>Free memory: </b><?php echo $memfreetotal;?>Mb<p/ >

<p><b>UpTime: </b><?php echo $uptime;?><br/ ><p/ >

the browser looks something like this:

t70cfd101 
Hardware:
Serial Number:

Location: York Cab #C205 
Contact Information: me at 734-555-5555

Kernel and architecture: Linux 2.6.18-164.el5 #1 SMP Tue Aug 18 15:51:48 EDT 2009 x86_64 
Processor(s): 
Operating system: Red Hat Enterprise Linux Server release 5.4 (Tikanga)

Processor total: 8 
Total memory: 49456556 Mb
Free memory: 4154896 Mb

UpTime: 459 days 

and running the php script on the command line looks like:

sh: line 1: /: is a directory
sh: line 1: /: is a directory

<p><b><u><big>t70cfd101
</big></u></b><br/ >
<b>Hardware:</b> HP
 ProLiant BL280c G6
<br/ >
<b>Serial Number:</b> USE0293K8C
<br/ ><p/ >

<p><b>Location: </b>York Cab #C205
<br/ >
<b>Contact Information: </b>me at 734-555-5555<p/ >

<p><b>Kernel and architecture: </b>Linux 2.6.18-164.el5 #1 SMP Tue Aug 18 15:51:48 EDT 2009 x86_64
<br/ >
<b>Processor(s): </b>Intel(R) Xeon(R) CPU X5570 @ 2.93GHz
<br/ >
<b>Operating system: </b>Red Hat Enterprise Linux Server release 5.4 (Tikanga)
<p/ >

<p><b>Processor total: </b>8
<br/ >
<b>Total memory: </b>49456556
Mb<br/ >
<b>Free memory: </b>4154888
Mb<p/ >

<p><b>UpTime: </b>459 days
<br/ ><p/ >

thanks in advance.

Add a statement to print/display the PATH variable.
In all likelihood the dmidecode program is not found when the script is executed by the web server.

In short, if you run php script on command line, you will get something like this

[akshay@localhost tmp]$ php -r 'echo `whoami`;'
akshay

At the same time, If you're trying to trigger it from a web interface, you will receive output as

apache

You need to check following

  1. The user running the code (you vs apache)
  2. SELinux (disable SELinux)
  3. PATH
  4. If you are running some time consuming big programs, then max_execution_time or memory_limit in php.ini
    program executed by browser (with apache) and for cli (terminal - without apache) may have different values for max_execution_time or memory_limit - usual php.ini for cli has more memory and it could run for longer period of time
  5. Whether user apache has enough permission to read, write and execute programs/files

thank you all for replying.

akshay,
it didn't go unnoticed that the issue was likely pointing to the execution of dmidecode. i had already disabled selinux and even tried executing /usr/sbin/dmidecode with no luck. it didn't occur to me the command was being executed by apache and not root. so that's the issue, since dmidecode requires access to /dev/mem. so now i need to find a secure way for the apache user to execute dmidecode. or perhaps using sudo. not being a web admin or a php expert i'll have to dig in a little deeper. i suppose the short answer would be to "chmod 4755 /usr/sbin/dmidecode".

thanks.