appobs Posted September 20, 2015 Share Posted September 20, 2015 Increasingly I'm hardcoding '/home/username/myScriptsDir/script.php' at the start of index.php files to include some 'tools' I use during development. Now I'm getting more use out of functions, I want a library of my own available in the same way, to every site/subdomain/app/whatever on my rented (shared) linux webserver. Why am I finding it so hard to find a var (or constant) for '/home/username'? phpinfo() doesn't show me anything useful. Lot's of googling, nothing sems to work on my server (see paste below, not exhaustive, just to show I've been trying) I'm trying to get away from: Every site/subdomain/app/whatever gets it's own script in its own $_SERVER['DOCUMENT_ROOT'] Hardcoding '/home/username/blah/' Non-solutions like ../ when depth varies file to file Could do but not ideal: Use php to 'look for' a known path/file outside of public_html The above but, make php write the path it figures out, automated-hardcoding, then delete itself... Seems a little crazy to me... Compare get_current_user to $_SERVER['DOCUMENT_ROOT'] and make a var/constant from there (most reasonable idea so far IMO!) Things I just added because I'm looking at it now: Adjust $_SERVER vars using PHP Variables Manager (it's there in my cPanel) Add my own var using PHP Variables Manager or put a php.ini file in there Given the var isn't already there, I think there might be something bad about this idea /* ========== INCLUDES =========== */ include '~/ti.php'; include ('~/ti.php'); include realpath('~/ti.php'); /* $_SERVER vars that don't work on my server */ include $_SERVER['HOME'] . '/ti.php'; include ($_SERVER['HOME']) . '/ti.php'; include $_SERVER['USER'] . '/ti.php'; include ($_SERVER['USER']) . '/ti.php'; include $_SERVER['USR'] . '/ti.php'; include ($_SERVER['USR']) . '/ti.php'; echo $_SERVER['HOMEDRIVE']; echo $_SERVER['HOMEPATH']; /* getenv instead */ include getenv("home") . '/ti.php'; include (getenv("home")) . '/ti.php'; include getenv("user") . '/ti.php'; include (getenv("user")) . '/ti.php'; include getenv("usr") . '/ti.php'; include (getenv("usr")) . '/ti.php'; include getenv("HOME") . '/ti.php'; include (getenv("HOME")) . '/ti.php'; include getenv("USER") . '/ti.php'; include (getenv("USER")) . '/ti.php'; include getenv("USR") . '/ti.php'; include (getenv("USR")) . '/ti.php'; /* posix stuff */ include(posix_getuid()) . '/ti.php'; include (posix_getpwuid(posix_getuid())) . '/ti.php'; /* =========== ECHOES ============ */ echo realpath('~/ti.php'); /* $_SERVER vars that don't work on my server */ echo $_SERVER['HOME']; echo ($_SERVER['HOME']); echo $_SERVER['USER']; echo ($_SERVER['USER']); echo $_SERVER['USR']; echo ($_SERVER['USR']); echo $_SERVER['HOMEDRIVE']; echo $_SERVER['HOMEPATH']; /* getenv instead */ echo getenv("home"); echo (getenv("home")); echo getenv("user"); echo (getenv("user")); echo getenv("usr"); echo (getenv("usr")); echo getenv("HOME"); echo (getenv("HOME")); echo getenv("USER"); echo (getenv("USER")); echo getenv("USR"); echo (getenv("USR")); /* posix stuff */ echo (posix_getuid()); echo (posix_getpwuid(posix_getuid())); Quote Link to comment https://forums.phpfreaks.com/topic/298236-why-so-hard-to-find-varconstant-for-the-dir-homeusername/ Share on other sites More sharing options...
scootstah Posted September 20, 2015 Share Posted September 20, 2015 http://php.net/manual/en/language.constants.predefined.php Quote Link to comment https://forums.phpfreaks.com/topic/298236-why-so-hard-to-find-varconstant-for-the-dir-homeusername/#findComment-1521205 Share on other sites More sharing options...
appobs Posted September 20, 2015 Author Share Posted September 20, 2015 (edited) I just put the following on a page pa($_SERVER); pa(get_defined_constants(true)); (where pa() just does a print_r wrapped in <pre> tags) I'm looking at it now... Edited September 20, 2015 by appobs Quote Link to comment https://forums.phpfreaks.com/topic/298236-why-so-hard-to-find-varconstant-for-the-dir-homeusername/#findComment-1521206 Share on other sites More sharing options...
Solution scootstah Posted September 20, 2015 Solution Share Posted September 20, 2015 __DIR__ returns the absolute path to the directory of the script it is executed from. If your site all runs through a single index.php file (like it should), then you can define a constant in index.php which would be your base app path. Like:define('APPPATH', __DIR__);Now when you include just doinclude APPPATH . '/somefile.php'; Quote Link to comment https://forums.phpfreaks.com/topic/298236-why-so-hard-to-find-varconstant-for-the-dir-homeusername/#findComment-1521207 Share on other sites More sharing options...
appobs Posted September 20, 2015 Author Share Posted September 20, 2015 (edited) The question I'm asking fizzles out if I'm loading content using a single point of entry, which I've figured out only very recently.(I have an awful lot of folders lying around containing index.php files!)So if you have a repository of your own scripts/functions etc... Do you keep them outside of public_html? Edited September 20, 2015 by appobs Quote Link to comment https://forums.phpfreaks.com/topic/298236-why-so-hard-to-find-varconstant-for-the-dir-homeusername/#findComment-1521211 Share on other sites More sharing options...
scootstah Posted September 20, 2015 Share Posted September 20, 2015 So if you have a repository of your own scripts/functions etc... Do you keep them outside of public_html? Yes, typically. Only index.php needs to be in public_html. But that doesn't really matter in the context of your question. 1 Quote Link to comment https://forums.phpfreaks.com/topic/298236-why-so-hard-to-find-varconstant-for-the-dir-homeusername/#findComment-1521212 Share on other sites More sharing options...
appobs Posted September 20, 2015 Author Share Posted September 20, 2015 Yes... the context of my question! It appears I've found out why it doesn't exist, which answers my question, thank you Quote Link to comment https://forums.phpfreaks.com/topic/298236-why-so-hard-to-find-varconstant-for-the-dir-homeusername/#findComment-1521215 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.