Link to home
Start Free TrialLog in
Avatar of ellandrd
ellandrdFlag for Ireland

asked on

PHP & MS DOS

can PHP execute MS DOS commands such as %username% ?
SOLUTION
Avatar of Roonaan
Roonaan
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Yes.

But %username% is an environment variable.

To get that use getenv('username') just like in *ix.

Avatar of ellandrd

ASKER

so would my code look like this:

<?php
$command = "getenv('username')";
echo exec($command);
?>
No.

getenv is a PHP function, not a command in DOS.

Assuming the environment variable username contains a program name that can be executed AND you know the path!!!! (no PATH searching!!!!!) then

<?php
$s_username = getenv('username');
$s_output = shell_exec($s_username);

echo $s_output;
?>



dont work but im just gonna leave it... nothing seems to work for me
Oh come on!

Does the content of the variable contain a program name or is it a shell command?

If %username% = dir then OK, shell_exec will run the shell command OK.

If %username% = my_prog.exe and my_prog.exe is NOT in the current working directory for the PHP script it will not work.

BUT, if my_prog.exe is in the path, then ...

<?php
$s_command = getenv('username');
echo "We retrieved '$s_command' from the environment.\n";

$s_full_path = getenv('PATH');
echo "We retrieved '$s_full_path ' from the environment.\n";

$a_path = explode(PATH_SEPARATOR, $s_full_path );

foreach($a_path as $s_path)
      {
      if (file_exists($s_path . DIRECTORY_SEPARATOR . $s_command))
            {
            $s_output = shell_exec($s_path . DIRECTORY_SEPARATOR . $s_command);
            break;
            }
      }

echo $s_output;
?>

would do something along the lines of what you want.

This is ultra simple stuff. As long as you actually use a shell day in day out. In fact the above code SHOULD run on a *ix box without modification.
first im running windows 2000 and if you echo %username% in MS dos, you get the username of person logged into the machine..
Ok. No problem with that.

So why do you want to EXECUTE the username?

At the CMD prompt, type

SET

followed by the enter key.

You will see a whole LOAD of envirornment variables!

username is just 1 of them.

But that is the username on YOUR computer!!!!!

If you have a web server running a PHP script, it will PROBABLY be the user which is running the web server.

To get the environment variable 'username' into a PHP variable ...

<?php
$s_username = getenv('username');
?>
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
just prints out %username%
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
i give up!

ive tried everything i can think of to get the NT username of the user viewing a webpage on our network and have failed each time...
Ah! Why didn't you say you wanted the NT username of the person MAKING the request!!! You dozy bugger!

What web server are you using?

Take a look at the $_SERVER array to see if IIS is being very silly and telling you things about your clients.

everybody im giving it ONE last go...

look here for a senerio of what im trying to do...

https://www.experts-exchange.com/questions/21865118/Connect-to-MS-SQL-server-2000.html