[RESOLVED]How to access the Apache `$_SERVER`global variable

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
xtianos
Forum Newbie
Posts: 14
Joined: Tue Mar 11, 2014 9:35 pm

[RESOLVED]How to access the Apache `$_SERVER`global variable

Post by xtianos »

I am trying to figure a way to query the server's $_SERVER global variable to get access to it's "HOME" property. I do not have root access on my website, it is shared hosting. A reference to the above can be found at: http://php.net/manual/en/reserved.variables.server.php. Thanks in advance for your help.
Last edited by xtianos on Thu May 28, 2015 11:50 pm, edited 2 times in total.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: How to access the Apache `$_SERVER`global variable

Post by Celauran »

As far as I know, $_SERVER['HOME'] is available only through the CLI. What is it you're trying to do?
xtianos
Forum Newbie
Posts: 14
Joined: Tue Mar 11, 2014 9:35 pm

Re: How to access the Apache `$_SERVER`global variable

Post by xtianos »

I am trying to hone my PHP skills before I purchase the Certification book and take the test. Basically, I am writing my own framework (no GUI interface, just code) and in doing so I needed to declare some constants that would automatically define themselves without me having to hard-code anything. At the beginning I followed the concept of "make it work first, then tweak it for efficiency", so I ended up hard-coding CONSTANTS values, but as I went over the code and began making my tweaks I was unable to find a way to set that value automatically. The idea is for me to copy/paste that framework into another ISP and it would automatically set the constants for me without the need of hard-coding.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: How to access the Apache `$_SERVER`global variable

Post by Celauran »

I'm not sure where $_SERVER['HOME'] would fit in, then. That literally maps to your home directory; /home/username (or /Users/username on OS X). What if your site lives in /var/www? Maybe defining some constants relative to your bootstrap file, say using dirname(), would make more sense.
xtianos
Forum Newbie
Posts: 14
Joined: Tue Mar 11, 2014 9:35 pm

Re: How to access the Apache `$_SERVER`global variable

Post by xtianos »

It would be in the "index.php" file in the "public_html" folder and would look something like:

Code: Select all

define("FRAMEWORK_PATH", $_SERVER["HOME"] . "/framework_folder_name");
require_once FRAMEWORK_PATH . "/Framework.Constants.php";
require_once CONTROLLER_PATH . "/Main.Controller.php";

// Rest of code here...
I just want to avoid having to write the full path to define the first constant. Right now I am calling the file that has all the constant definitions with the full path, then after that I begin using CONSTANTS for the remainder of the framework. Currently the constants file is inside the main root folder of the framework, so I am using the __DIR__ to refer to the framework folder. Hope it makes sense.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: How to access the Apache `$_SERVER`global variable

Post by Celauran »

What you've described sounds about like how I'd approach it. Use dirname(__FILE__) inside your bootstrap to define the install path then define all other paths relative to that.

Code: Select all

define('BASE_PATH', dirname(__FILE__));
define('APP_PATH', BASE_PATH . '/app');
etc.
xtianos
Forum Newbie
Posts: 14
Joined: Tue Mar 11, 2014 9:35 pm

[RESOLVED] How to access the Apache `$_SERVER`global variabl

Post by xtianos »

Roger that! I appreciate the time you took to look over my post and reply. Cheers!
Post Reply