Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 904
  • Last Modified:

Windows / Apache / PHP 5.2 -Retreve Windows computer name

I have an application which I run on a number of different servers and my laptop.
It has a simple config.php script, for things such as MySQL server credentials.

What I want to do is detect which Windows computer the application is using via the Windows environement variable.

IE %COMPUTERNAME%

But the PHP $_ENV array is empty how do i get the windows host name ???
0
Matthew_Way
Asked:
Matthew_Way
  • 8
  • 7
  • 3
  • +1
1 Solution
 
petr_hlucinCommented:
Read it directly from registry.
<?php
  exec("reg query HKLM\SYSTEM\ControlSet001\Control\ComputerName\ComputerName /v ComputerName", $out);
  $reg_line = explode("\t", trim($out[4]));
  echo $reg_line[2]; 
?>

Open in new window

0
 
petr_hlucinCommented:
Sorry, CurrentControlSet is probably better for you.
<?php
  exec("reg query HKLM\SYSTEM\ControlSet001\Control\ComputerName\ComputerName /v ComputerName", $out);
  $reg_line = explode("\t", trim($out[4]));
  echo $reg_line[2]; 
?>

Open in new window

0
 
Matthew_WayAuthor Commented:
I don't want to run an exec, need something fast.

Why dont my Windows environment vars show up in PHP ??
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
petr_hlucinCommented:
0
 
fiboCommented:
You might also use the superglobals in $_SERVER http://www.php.net/manual/en/reserved.variables.server.php

$_SERVER['SERVER_NAME'] will give you the name of the server running the php script (I once had a problem with an Apache server which was poorly configured and did not return its 'canonical name', and solved this by the server IP address $_SERVER['SERVER_ADDR'])
and
$_SERVER['REMOTE_ADDR'] will give you the IP address of the web-client (getting the name with 'REMOTE_HOST' would be great but usually does not work because the reverse-DNS search is not enabled by default, and creates a heavy penalty on the server performance)

The $_SERVER should work for all web servers, even if they are not running on windows.
0
 
Matthew_WayAuthor Commented:
fibo:
I'm also running a number of vhosts on all the servers, with aliases.

So will PHP return the $_SERVER['SERVER_NAME']
As the VHOST name or the Alias that they connect via ?
0
 
Matthew_WayAuthor Commented:
I set
variables_order = "EGPCS"
in php.ini I now get a populated $_ENV array but none of the Windows environment info such as %COMPUTERNAME% come through.
I do get the $_SERVER stuff in $_ENV though, where as before $_ENV was empty.

Software versions;
WindowsXP SP2, Apache 2.2.4 , PHP 5.2.4
0
 
fiboCommented:
<<fibo:
So will PHP return the $_SERVER['SERVER_NAME']
As the VHOST name or the Alias that they connect via ?>>
I presume that you found the answer in you (freshly received!) $_ENV, and that it is the alias.

Do you need the Windows 'computername' wich of course might be different from the hostname... but for your problem hostname is probably fine..
0
 
EgonFreemanCommented:
If computername/dnsname is what You need, instantiate a COM object, connect to WMI and "SELECT DNSHostName,Name FROM Win32_ComputerSystem". Whichever You need. ;-) [try to remember WMI doesn't like semicolons at the end, even though it is SQL ;P]

More info on what's in Win32_ComputerName on MSDN:

http://msdn.microsoft.com/en-us/library/aa394102.aspx


[this post for the sake of completeness]
0
 
Matthew_WayAuthor Commented:
All these solutions are too slow, I need some thing quick!
0
 
fiboCommented:
if you run a phpinfo(), which $_SERVER variable is giving you the windows computername?
0
 
Matthew_WayAuthor Commented:
When I run a CLI version of PHP I get the following;
_SERVER["COMPUTERNAME"] => BKK-LP21

Which is exactly what I want.
But when I run PHP under Apache, I don't get a computer name at all !
not in _SERVER or _ENV

I've had to resort to putting the computer name in a config file.
I wanted to have a "config.php" script with something like this in it

if( $_SERVER['COMPUTERNAME'] == 'PRODUCTION' ){
define( DB_PASSWORD = 'abcd123' );
} else {
define( DB_PASSWORD = 'xyz789');
}

As config.php is run everytime before anything else it needs to be as fast as possible!
0
 
fiboCommented:
In a similar situation, I used $_SESSION:
- I will use $_SESSION['mycomputer']
- if it is empty or not defined , ie you get true for ( '' == trim( ' ' . @$_SESSION['mycomputer']) ), then find the computername by any appropriate mean, even if it takes long, then place it in $_SESSION['mycomputer']
- if it has a value, just use it
0
 
Matthew_WayAuthor Commented:
I can see your point.
But i use a custom session handler and want to load the 'config'  file.
0
 
fiboCommented:
>>But i use a custom session handler
even then, you are probably using session_start() otherwise, unless you pass some cookie/ session info as a GET thru the urls of your site, you have no way to build sessions.... so adding a variable to $_SESSION is not a major change, specifically if it helps you to get a performant way to solve your problem

>>and want to load the 'config'  file.
loading the config has a time/cpu cost... independent from the method you are using for finding the computername
0
 
fiboCommented:
>>But i use a custom session handler
even then, you are probably using session_start() otherwise, unless you pass some cookie/ session info as a GET thru the urls of your site, you have no way to build sessions.... so adding a variable to $_SESSION is not a major change, specifically if it helps you to get a performant way to solve your problem

>>and want to load the 'config'  file.
loading the config has a time/cpu cost... independent from the method you are using for finding the computername
0
 
Matthew_WayAuthor Commented:
Can we make apache pass the computer name similar to the CLI version ?
0
 
fiboCommented:
Matthew,
which solution did you select eventually?
0
 
Matthew_WayAuthor Commented:
Eventually what I did was to leave a flag file in the config directory.

is_development.flg
is_production.flg

Then I do a file_exists('is_production.flg')
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 8
  • 7
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now