?
Solved

PHP & MS DOS

Posted on 2006-05-25
16
Medium Priority
?
464 Views
Last Modified: 2008-03-06
can PHP execute MS DOS commands such as %username% ?
0
Comment
Question by:ellandrd
16 Comments
 
LVL 49

Assisted Solution

by:Roonaan
Roonaan earned 200 total points
ID: 16760868
You would have to look into exec() and system() functions to communicate with the servers operating system.

I think it is possible.

-r-
0
 
LVL 40

Expert Comment

by:Richard Quadling
ID: 16760980
Yes.

But %username% is an environment variable.

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

0
 
LVL 16

Author Comment

by:ellandrd
ID: 16761056
so would my code look like this:

<?php
$command = "getenv('username')";
echo exec($command);
?>
0
Technology Partners: 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!

 
LVL 40

Expert Comment

by:Richard Quadling
ID: 16761096
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;
?>



0
 
LVL 16

Author Comment

by:ellandrd
ID: 16761122
dont work but im just gonna leave it... nothing seems to work for me
0
 
LVL 40

Expert Comment

by:Richard Quadling
ID: 16761188
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.
0
 
LVL 16

Author Comment

by:ellandrd
ID: 16761206
first im running windows 2000 and if you echo %username% in MS dos, you get the username of person logged into the machine..
0
 
LVL 40

Expert Comment

by:Richard Quadling
ID: 16761231
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.

0
 
LVL 40

Expert Comment

by:Richard Quadling
ID: 16761238
To get the environment variable 'username' into a PHP variable ...

<?php
$s_username = getenv('username');
?>
0
 
LVL 40

Assisted Solution

by:Richard Quadling
Richard Quadling earned 1000 total points
ID: 16761266
%username% is NOT actually a command.

The command you where running is echo.

This command took a parameter of %username% which is expanded by the echo command (a built in command not an external executable) into the current username of the person logged on to the computer where the program is running.

I use this sort of thing a LOT.

Take a look at this line from one of my .BAT ...

IF NOT EXIST "%ALLUSERSPROFILE%\Start Menu\Programs\Dimensions\." ECHO %DATE% %TIME% : Creating Public Dimensions Shortcut Directory.

You see %ALLUSERSPROFILE% and %DATE% and %TIME%

These are expanded at runtime.

Just like $s_date is in this example.

<?php
$s_date = date('r');
echo "The date is $s_date\n";
?>

0
 
LVL 16

Assisted Solution

by:dr_dedo
dr_dedo earned 400 total points
ID: 16761404
a VERY simple work around

<?
exec('echo %username% >c:\dos.txt');
$name= file_get_contents('c:\dos.txt');
echo $name;
unlink('c:\dos.txt');
?>
0
 
LVL 16

Author Comment

by:ellandrd
ID: 16761452
just prints out %username%
0
 
LVL 4

Accepted Solution

by:
punkstar earned 400 total points
ID: 16761476
ellandrd, you do understand that you won't be getting the username of the person visiting your website right?
0
 
LVL 16

Author Comment

by:ellandrd
ID: 16761557
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...
0
 
LVL 40

Expert Comment

by:Richard Quadling
ID: 16769013
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.

0
 
LVL 16

Author Comment

by:ellandrd
ID: 16769388
everybody im giving it ONE last go...

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

http://www.experts-exchange.com/Web/Q_21865118.html
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
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
Course of the Month17 days, 1 hour left to enroll

862 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