Php accessing Home directories
I have a script and i d like to access to home directories of users in a Linux Environment. Web root : /var/www/html/ And there are user directories such as : /home/john /home/david etc. There are files in users home directories. The permissions of user homes belong to the users. eg: /home/david/file.txt user: david group: david Is it possible to access these files with apache? I assume it s not because of the permission, Is there a way around this ? in other words, my php program under /var/www/html/index.php can acccess the files under /home/david/foo.txt How can i get this done? Thanks.
2 Answers 2
The best way would be to have the users place the specific needed files into a pub directory, then chmod 777 that directory.
If you want to access arbitrary files in the home directory, you have to run Apache as root, which is a big security risk. (While you could change the permissions of the home directory, this can mess up a lot of programs, in my experience.)
If by access you mean read, 644 is enough — no need to allow Apache to write to those files. Also make sure that all directories about the file have at least world execute permissions (711).
Maybe I am a bit paranoid when it comes to these things, but in my opinion there is something conceptually wrong here:
A script, that is exposed to the web should never be given access to users’ home directories. One reason for saying that is that a compromise of the web server might result in exposure of files in the home directories to anyone who can access the web server. Another reason is that files in the home directories are (at least to my understanding) a place where users keep more or less personal/private files that should not be available to other users. Otherwise they would have placed it in a public directory.
While I am not sure what your use case is, I suggest it might be better to use a different concept where Apache does not need access to the home directories of other users in the first place.
Php accessing Home directories
Another reason is that files in the home directories are (at least to my understanding) a place where users keep more or less personal/private files that should not be available to other users. The permissions of user homes belong to the users.
Php accessing Home directories
I have a script and i d like to access to home directories of users in a Linux Environment.
Web root : /var/www/html/ And there are user directories such as : /home/john /home/david etc.
There are files in users home directories. The permissions of user homes belong to the users. eg: /home/david/file.txt user: david group: david
Is it possible to access these files with apache? I assume it s not because of the permission,
Is there a way around this ?
in other words, my php program under /var/www/html/index.php can acccess the files under /home/david/foo.txt
How can i get this done? Thanks.
The best way would be to have the users place the specific needed files into a pub directory, then chmod 777 that directory.
If you want to access arbitrary files in the home directory, you have to run Apache as root, which is a big security risk. (While you could change the permissions of the home directory, this can mess up a lot of programs, in my experience.)
Maybe I am a bit paranoid when it comes to these things, but in my opinion there is something conceptually wrong here:
A script, that is exposed to the web should never be given access to users’ home directories. One reason for saying that is that a compromise of the web server might result in exposure of files in the home directories to anyone who can access the web server. Another reason is that files in the home directories are (at least to my understanding) a place where users keep more or less personal/private files that should not be available to other users. Otherwise they would have placed it in a public directory.
While I am not sure what your use case is, I suggest it might be better to use a different concept where Apache does not need access to the home directories of other users in the first place.
How to find user’s home directory on Windows and, I’ve understood there are several ways to determine the user’s home, depending on the platform (mainly Unix/Linux vs Windows). Composer uses an environment variable, in composer/Platform package: public static function getUserDirectory () < if (false !== ($home = getenv ('HOME'))) < return $home; >if (self::isWindows () && …
How to find user’s home directory on Windows and Linux in PHP
I’ve understood there are several ways to determine the user’s home, depending on the platform (mainly Unix/Linux vs Windows).
Composer uses an environment variable, in composer/Platform package:
public static function getUserDirectory() < if (false !== ($home = getenv('HOME'))) < return $home; >if (self::isWindows() && false !== ($home = getenv('USERPROFILE'))) < return $home; >if (function_exists('posix_getuid') && function_exists('posix_getpwuid')) < $info = posix_getpwuid(posix_getuid()); return $info['dir']; >throw new \RuntimeException('Could not determine user directory'); > public static function isWindows()
Webmozart’s path-util package uses other environment variables:
public static function getHomeDirectory() < // For UNIX support if (getenv('HOME')) < return static::canonicalize(getenv('HOME')); >// For >= Windows8 support if (getenv('HOMEDRIVE') && getenv('HOMEPATH')) < return static::canonicalize(getenv('HOMEDRIVE').getenv('HOMEPATH')); >throw new RuntimeException("Your environment or operation system isn't supported"); >
What is the difference between these two methods? Is one more reliable than the other? Note: I’m using PHP in the CLI, so it’s always the actual current user running PHP.
EDIT> I understand that this question seems to ask for an opinion, but it’s not the case. I DO NOT KNOW Windows and do not understand why some packages use different ways to determine the user’s home directory. I’m asking for explanations about the two mentioned methods: is one of them more reliable than the other and why? I’ve edited the title and the question to reflect this precision.
After not working on this for a long time, I finally decided to definitely answer this question.
There are some usefull environment variables defined on Windows: USERPROFILE , APPDATA , LOCALAPPDATA . They are easily accessible via getenv() function:
USERPROFILE exists on any Windows, according to https://docs.microsoft.com/fr-fr/windows/desktop/shell/KNOWNFOLDERID
So, on Windows, it seems to be reliable.
If you need to store data for the current user, APPDATA and LOCALAPPDATA are good variables to find that place.
I’ve written a package to make these tools reusable: https://github.com/Arcesilas/Platform
It’s still work in progress and certainly needs to be improved. Any help is welcome to make this tool reliable on any platform.
Thanks to eryksun whose comments helped a lot in solving this question.
Find Root Directory Path in PHP, Use the dirname () Function to Find the Path of the Root Directory of a Project in PHP. Use $_SERVER [‘DOCUMENT_ROOT’] to Find the Document Root Directory of a File in PHP. We will introduce different methods to find the path of the root directory of a PHP project.
Get «Home» Url with PHP
I would like to get the «home» url with PHP. NOT the current URL.
My website exists locally and on a live test server. The URL is being printed as a javascript variable and needs to work in both places (live and local).
The live «home» URL is http://pipeup.pagodabox.com and the local home is http://192.168.0.10:8888/pipeup .
So, if the home page is http://192.168.0.10:8888/pipeup/ , if I’m on any subpage, like http://192.168.0.10:8888/pipeup/page.php or http://192.168.0.10:8888/pipeup/about/our-team.php , I would like a variable that returns «http://192.168.0.10:8888/pipeup/» or «http://pipeup.pagodabox.com» depending on if I’m viewing it live or local.
But that doesn’t work when I’m on a subpage.
How do I get the Home URL with PHP? I would like the solution to also work correctly with a localhost server or local IP address.
I used below code in my application, I hope it help you also.
Note: if you just want to get home, why not just use http://example.com ? this code below provides you current URL
/* http://www.webcheatsheet.com/PHP/get_current_page_url.php PHP: How to Get the Current Page URL Print Bookmark and Share Sometimes, you might want to get the current page URL that is shown in the browser URL window. For example if you want to let your visitors submit a blog post to Digg you need to get that same exact URL. There are plenty of other reasons as well. Here is how you can do that. Add the following code to a page: */ function curPageURL() < $pageURL = 'http'; if ($_SERVER["HTTPS"] == "on") $pageURL .= "://"; if ($_SERVER["SERVER_PORT"] != "80") < $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"]; >else < $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"]; >//return preg_replace("/\.php.*/", ".php", $pageURL); $_SESSION['thisurl'] = $pageURL; return $pageURL; >
$directory = "/"; function url($directory = null) < $protocol = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != "off") ? "https" : "http"; return $protocol . "://" . $_SERVER['HTTP_HOST'] . $directory; >define("BASE_URL", url($directory));
I currently use the above from my index.php in an MVC design application. It allows you to set the directory to whatever you want («/» for your home directory if in the first file presented). Then I just call BASE_URL from any subsequent files in the application. Don’t know if it will help you but has not failed me yet!
PHP Get all subdirectories of a given directory, Teams. Q&A for work. Connect and share knowledge within a single location that is structured and easy to search. Learn more
Obtain user’s home directory
Is the following the best way of obtaining the running user’s home directory? Or is there a specific function that I’ve ovelooked?
If the above is correct, does anyone happen to know whether this approach is guaranteed to work on non-Linux platforms, e.g. Windows?
Since go 1.12 the recommended way is:
package main import ( "os" "fmt" "log" ) func main() < dirname, err := os.UserHomeDir() if err != nil < log.Fatal( err ) >fmt.Println( dirname ) >
In go 1.0.3 ( probably earlier, too ) the following works:
package main import ( "os/user" "fmt" "log" ) func main() < usr, err := user.Current() if err != nil < log.Fatal( err ) >fmt.Println( usr.HomeDir ) >
os.UserHomeDir()
In go1.12+ you can use os.UserHomeDir()
That should work even without CGO enabled (i.e. FROM scratch ) and without having to parse /etc/passwd or other such nonsense.
package main import ( "fmt" "os" "runtime" ) func UserHomeDir() string < if runtime.GOOS == "windows" < home := os.Getenv("HOMEDRIVE") + os.Getenv("HOMEPATH") if home == "" < home = os.Getenv("USERPROFILE") >return home > return os.Getenv("HOME") > func main()
Here’s a nice, concise way to do it (if you’re only running on a UNIX based system):
import ( "os" ) var home string = os.Getenv("HOME")
That just queries the $HOME environment variable.
I now see that this same method was suggested above. I’ll leave this example here as a distilled solution.
How to find path of user’s home directory in PHP Cli on, The CLI version of PHP puts the environment variables in $_SERVER[]. The home directory of the current user can be found in $_SERVER[‘HOME’]. There is no need to change any directory or run any external command. Tested it on OSX and Ubuntu but I am confident it works on any Linux …
How to find user’s home directory on Windows and Linux in PHP
I’ve understood there are several ways to determine the user’s home, depending on the platform (mainly Unix/Linux vs Windows). Composer uses an environment variable, in composer/Platform package:
public static function getUserDirectory() < if (false !== ($home = getenv('HOME'))) < return $home; >if (self::isWindows() && false !== ($home = getenv('USERPROFILE'))) < return $home; >if (function_exists('posix_getuid') && function_exists('posix_getpwuid')) < $info = posix_getpwuid(posix_getuid()); return $info['dir']; >throw new \RuntimeException('Could not determine user directory'); > public static function isWindows()
public static function getHomeDirectory() < // For UNIX support if (getenv('HOME')) < return static::canonicalize(getenv('HOME')); >// For >= Windows8 support if (getenv('HOMEDRIVE') && getenv('HOMEPATH')) < return static::canonicalize(getenv('HOMEDRIVE').getenv('HOMEPATH')); >throw new RuntimeException("Your environment or operation system isn't supported"); >
What is the difference between these two methods? Is one more reliable than the other? Note: I’m using PHP in the CLI, so it’s always the actual current user running PHP. EDIT> I understand that this question seems to ask for an opinion, but it’s not the case. I DO NOT KNOW Windows and do not understand why some packages use different ways to determine the user’s home directory. I’m asking for explanations about the two mentioned methods: is one of them more reliable than the other and why? I’ve edited the title and the question to reflect this precision.