Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Who is running my script?

Status
Not open for further replies.

klockstone

Technical User
Jan 20, 2005
2
GB
My PHP script generates files. It can be run by Apache, cron or userx (command line). Thus the files get different owners. How does the script find out (before the file is generated) who the owner will be?

TIA,

Keith.
 
klockstone
You could, I suppose, execute [tt]whoami[/tt] as an external command:

<?php
print `whoami`;
?>


MJB3K:
A PHP script like:

Code:
#! /usr/local/bin/php
<?php
print 'foo';
?>

Works in cron just fine on my LAMP box.

Want the best answers? Ask the best questions!

TANSTAAFL!!
 
PHP launched from cron depends on the specifics of your system.

i.e. If you're running a distribution (like debian) with APT, and you installed php, but not php-cli, you may not be able to.

Personally... since I run alot of web pages as scripts I tend to use a cron entry like

php /path/to/script/script.php

Or have cron kick off a shell script which first sets the working directory and then launches the script.
 
I think I'll do it the hard way:

function whoami() // get numeric owner of files
{ $temp = tmpfile(); // make temp file
if (is_bool($temp)) // failure
return (0);

$fstat = fstat($temp); // get file stats
if (is_bool($fstat)) // failure
return (0);

$x = $fstat[4]; // file owner

$y = fclose($temp); // close and remove file
if (is_bool($y)) // failure
return (0);

return $x;
}

It only needs to be run once at the beginning of the script. Pity tho'.

Thanks all,

Keith.
 
try a simple script like this:
Code:
<?php
print_r ($GLOBALS);
?>
And see what is available to you in the script, you might pick the username from therp
Under windows I have an enviroment varaible called USERNAME which I can use, I'm not sure if it's something my NT guys have done ir comes as part of windows, I'm sure @nix musttell you without going to whoami etc.
I'd be interestd to knoe not being a *nix person
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top