Delicious Bookmark this on Delicious Share on Facebook SlashdotSlashdot It! Digg! Digg



PHP : Function Reference : PHP Options&Information : get_current_user

get_current_user

Gets the name of the owner of the current PHP script (PHP 4, PHP 5)
string get_current_user ( )

Returns the name of the owner of the current PHP script.

Return Values

Returns the username as a string.

Examples ( Source code ) » get_current_user

It's easy to test - create a file containing:
<?
echo get_current_user();
?>

Code Examples / Notes » get_current_user

justin samuel

with 4.3.11 (and i assume all other versions):
get_current_user() does *not* get the name of the user the script is running as, as stated in the comment below.  instead, it "gets the name of the owner of the current PHP script" (as stated in the description above) --- that is, the owner of the file, not the owner of the process.
if the script file is owned by root but php scripts are being being run as apache (for example, you're using mod_php because you don't mind your shared hosting environment being insecure), when you request your script through the webserver, get_current_user() will return "root".  that does not mean your script is running as root.


comicforum

Under windows, this function does not work the same in Apache as it does in IIS.  If you want the user name in apache, you have to use...
getenv("REMOTE_USER");


justin samuel

to get the username of the process owner (rather than the file owner), you can use:
<?php
$processUser = posix_getpwuid(posix_geteuid());
print $processUser['name'];
?>


s dot bond1

The information returned by get_current_user() seems to depend on the platform.
Using PHP 5.1.1 running as CGI with IIS 5.0 on Windows NT, get_current_user() returns the owner of the process running the script, *not* the owner of the script itself.
It's easy to test - create a file containing:
<?
echo get_current_user();
?>
Then access it through the browser. I get: IUSR_MACHINE, the Internet Guest Account on Windows, which is certainly not the owner of the script.


siliconexpress

The 'constant' __FILE__ works easier ...
Example :
echo __FILE__;
Returns - '/home/username/public_html/filename.php'
This works INSIDE includes!!!  It saved me a bunch of problems..  If I want to make sure somebody does not load a file directly this is supposed be be an include this is the code I use...  Hope it helps you as much as it does me!  :)
//If user tries to load page directly the redirect to the home page
if ($_SERVER['SCRIPT_FILENAME'] == __FILE__) {
header("HTTP/1.1 301 Moved Permanently");
header("Location: /");
echo "DO NOT TRY TO ACCESS THIS FILE DIRECTLY\r\n";
exit;}


chris

If you want to get the absolute name of the file you are currently on, use this:
<?
$rest = explode("php.net/", $PHP_SELF);
$rest = $rest[0];
echo("/home/".get_current_user()."/public_html".$rest);
?>
Just change php.net/ to your site's domain and a slash.


joho

get_current_user () returns the owner of the process running the script. In most cases, this will be the web server user ("nobody", "httpd", "apache", etc).

tmacedo

<?php
/**
* addendum to 'SiliconExpress at Techie dot com' post:
* at Win32 enviroment, you have:
**/
var_dump(strcmp($_SERVER['SCRIPT_FILENAME'], __FILE__));
// output: int(0)
/**
* but you can use, instead:
**/
var_dump(strcmp(realpath($_SERVER['SCRIPT_FILENAME']), __FILE__));
// output: int(-1)
?>


Change Language


Follow Navioo On Twitter
assert_options
assert
dl
extension_loaded
get_cfg_var
get_current_user
get_defined_constants
get_extension_funcs
get_include_path
get_included_files
get_loaded_extensions
get_magic_quotes_gpc
get_magic_quotes_runtime
get_required_files
getenv
getlastmod
getmygid
getmyinode
getmypid
getmyuid
getopt
getrusage
ini_alter
ini_get_all
ini_get
ini_restore
ini_set
main
memory_get_peak_usage
memory_get_usage
php_ini_scanned_files
php_logo_guid
php_sapi_name
php_uname
phpcredits
phpinfo
phpversion
putenv
restore_include_path
set_include_path
set_magic_quotes_runtime
set_time_limit
sys_get_temp_dir
version_compare
zend_logo_guid
zend_thread_id
zend_version
eXTReMe Tracker