?
Solved

Windows / Apache / PHP 5.2 -Retreve Windows computer name

Posted on 2008-10-04
19
Medium Priority
?
898 Views
Last Modified: 2013-12-13
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
Comment
Question by:Matthew_Way
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 7
  • 3
  • +1
19 Comments
 
LVL 4

Expert Comment

by:petr_hlucin
ID: 22639949
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
 
LVL 4

Expert Comment

by:petr_hlucin
ID: 22639953
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
 

Author Comment

by:Matthew_Way
ID: 22640260
I don't want to run an exec, need something fast.

Why dont my Windows environment vars show up in PHP ??
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 4

Expert Comment

by:petr_hlucin
ID: 22640304
0
 
LVL 29

Expert Comment

by:fibo
ID: 22641134
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
 

Author Comment

by:Matthew_Way
ID: 22643190
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
 

Author Comment

by:Matthew_Way
ID: 22643266
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
 
LVL 29

Expert Comment

by:fibo
ID: 22644269
<<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
 

Expert Comment

by:EgonFreeman
ID: 23016140
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
 

Author Comment

by:Matthew_Way
ID: 23408537
All these solutions are too slow, I need some thing quick!
0
 
LVL 29

Expert Comment

by:fibo
ID: 23409368
if you run a phpinfo(), which $_SERVER variable is giving you the windows computername?
0
 

Author Comment

by:Matthew_Way
ID: 23447968
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
 
LVL 29

Expert Comment

by:fibo
ID: 23448213
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
 

Author Comment

by:Matthew_Way
ID: 23448370
I can see your point.
But i use a custom session handler and want to load the 'config'  file.
0
 
LVL 29

Expert Comment

by:fibo
ID: 23449274
>>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
 
LVL 29

Expert Comment

by:fibo
ID: 23449275
>>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
 

Author Comment

by:Matthew_Way
ID: 23455146
Can we make apache pass the computer name similar to the CLI version ?
0
 
LVL 29

Expert Comment

by:fibo
ID: 24743006
Matthew,
which solution did you select eventually?
0
 

Accepted Solution

by:
Matthew_Way earned 0 total points
ID: 25556510
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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses

719 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question