Server Environment Variables

SoftDesigns
SoftDesigns
@softdesigns
7 years ago
242 posts
On a Jamroom Premium Hosted Server: How to Set environment variables via: .htaccess
--
Goal: have Apache set environment variables for use in our Smarty Templates, via a .htaccess file, using SetEnv. We assume these variables must start with 'HTTP_' for security purposes.
--
Examples:
--
SetEnv HTTP_MY_VAR "my value"
SetEnv HTTP_MY_VAR myValue

Then use $_SERVER to fetch it from custom Module or somehow from Smarty Template.
$_SERVER['HTTP_MY_VAR']
--
FYI: On Azure - we can set these directly via Azure App Control Panel.
--
On a Jamroom Premium Hosted Server: How to Set Server Level environment variables, that can be accessed from any of our multiple JR sites?
updated by @softdesigns: 07/09/17 02:06:29PM
brian
@brian
7 years ago
10,148 posts
This is not something you can do, nor would you want to. Let me know what you're trying to accomplish and I can let you know how to go about it.

Thanks!


--
Brian Johnson
Founder and Lead Developer - Jamroom
https://www.jamroom.net
SoftDesigns
SoftDesigns
@softdesigns
7 years ago
242 posts
We need set a Server Level Global Constant - accessible from ALL JR Sites on the same server, custom modules, or smarty templates. Prefer not mess with DB, just a simple Server Environment variable - get value from code with $_SERVER['HTTP_MY_VAR'] - would be easy method...
--
We use Env vars on Azure - simple setup via control panel on Azure app
--
How can we Get/Set - Server Level Global Constant - on JR Hosted Server?
updated by @softdesigns: 04/06/17 02:27:33PM
brian
@brian
7 years ago
10,148 posts
So Azure is what is know as a "platform as a service" - i.e. you have direct access to your servers, and it is up to you to maintain and administer them. That is different than Jamroom Hosted. With Jamroom Hosted you do NOT administer your own server, you don't get "root" access, etc. So going in and setting a "server wide" variable is not something we can support.

Instead you'll need to create a custom module with a config.php file that accepts whatever you want to store as a config, then access it in Jamroom that way.

Sorry!


--
Brian Johnson
Founder and Lead Developer - Jamroom
https://www.jamroom.net
SoftDesigns
SoftDesigns
@softdesigns
7 years ago
242 posts
Ok - That makes sense - no worry.
--
Another way is to just run some code in the first "startup" on each JR site.
For example - on WordPress - we accomplish this - by running some simple code in theme: functions.php
--
Is there an existing simple JR PHP file - that runs "before any other modules" - so we could DEFINE a Site-Wide var that all other modules could access?
--
We could create custom module, but is there already some JR "startup" code area (like functions.php) - maybe that we could use?
updated by @softdesigns: 04/06/17 02:58:20PM
brian
@brian
7 years ago
10,148 posts
The problem with just sticking in an existing module is that it will be deleted when you upgrade - that is why you want to put it in your own custom module inside the "init" function - i.e.

function myCustomModule_init(){
    putenv("FOO=BAR");
    return true;
}

Then through out the site you could do:

getenv('FOO');

You could also stick it in server - i.e.

$_SERVER['myCustomModule_foo'] = 'bar';

and then in a module:

$_SERVER['myCustomModule_foo'];

or in a template:

{$_server.MyCustomModule_foo}

Let me know if that helps.


--
Brian Johnson
Founder and Lead Developer - Jamroom
https://www.jamroom.net

updated by @brian: 04/06/17 02:59:41PM
SoftDesigns
SoftDesigns
@softdesigns
7 years ago
242 posts
That makes sense - very helpful code above :)
--
We will create a custom module for this purpose
--
Last Question: We know this code - have done this before:
function myCustomModule_init(){
    putenv("API_URL=https://...");
    return true;
}
Question: Since ALL our other custom modules will depend on this sample var above - API_URL :
How do we make SURE this custom module init executes before other custom modules?
updated by @softdesigns: 04/06/17 03:06:09PM
brian
@brian
7 years ago
10,148 posts
In your module's meta() function, set the "priority" key to a really low value - i.e.

'priority' => 5

And then just make sure all your other custom modules have a HIGHER value. Let me know if that helps.


--
Brian Johnson
Founder and Lead Developer - Jamroom
https://www.jamroom.net
SoftDesigns
SoftDesigns
@softdesigns
7 years ago
242 posts
Ahh - yes - now I remember that trick - perfect..
Thanks for helping clear up our confusion on how to set a JR Site wide global constant
--
BTW: Actually we Prefer the Hosted JR Platform setup - over Azure - because we prefer focus on code, not servers.
--
Great Support - Solved :)

Tags