Make php variable/constant available to all scripts ad all sites - php

I have about 150 websites each on its own Apache virtual host running php7 on ubuntu 16. Each site has a config.php in the root dir with constants in it: define('MYVAR','myval');
I want to make a new constant that I will have to update every 2 months or so. I don't want to open each config and edit them. How can I have each site/virtual host be aware of this constant I want to set. I want to be available everywhere just like $_SERVER['REMOTE_ADDRESS']; is.
I do not want to create a file them symlink it to each site, thats messy. I would like to edit the apache config or edit a php config someplace to have a define() in it.
Is this possibe? or am I stuck sym linking a file or editing all my configs?

As pointed out in this link; PHP: possible to set constants in default php.ini file
You can't just invent variables in the PHP file, you'll need to auto-prepend a config file that has all the variables defined.
Ignore Below
You could store some global variables in your php.ini file.
The single php.ini file should be used by all your Apache virtual-hosts.
If you update your php.ini file and put a variable in like
my_custom_var = ABC123
Then you can access the variable with ini_get
echo ini_get('my_custom_var');
See more details on ini_get at http://php.net/manual/en/function.ini-get.php

I think you should try using OS environment variables.
Try setting the variables and then exporting them in your .profile file.
Ideally, this is where you should store sensitive credentials too. This way, even if your code is leaked, no one can get access to your existing server (never store Database or other credentials in your code)
You can either store your system variables in .profile or .bashrc file (I would recommend .profile). You can see how to set system variables here: https://unix.stackexchange.com/a/117470/209405
You can get the variables in PHP with the help of getenv($var) or with $_ENV[$var]. For more info, you can check here: http://php.net/manual/en/function.getenv.php and http://php.net/manual/en/reserved.variables.environment.php

The best non-intrusive way for me was to make sure mod_env was enabled then I added in the conf-enabled dir a conf file with the following in it
SetEnv MYLITTLEVAR thestringIwant
Then in php I can get it by:
print $_SERVER['MYLITTLEVAR'];
Tested and it works for me.

If want to make some variables globally available in PHP you need to add those variable in the apache config file.
if you're using ubuntu open /etc/apache2/apache2.conf file, And if you're using windows open httpd.conf. And add the following line.
SetEnv VARIABLE_NAME 'whatever value you want'

Related

PHP session.save_path ignored

I was having problems with my PHP website (SuiteCRM) not being able to log users in and I found it was due to not being able to write on the sessions directory.
I am able to fix it by creating the directory /tmp/php_sessions and giving it write permissions for the Apache user www-data. I see the directory get populated with files as users log in.
However, Ubuntu Xenial is deleting my entire tmp directory on reboots, so I have to redo this all over again every time. I decided to move my save_path elsewhere.
After changing things in my php.ini file, and restarting Apache, I can check that they are effective by running this simple script:
<?php
echo ini_get("session.save_path");
phpinfo();
?>
This shows me a double confirmation of the new path, first echoing /var/tmp/php_sessions and then, in the middle of all the phpinfo information, showing the same value as both Local Value and Master value for directive session.save_path.
BUT the directory that php is using is still the first one, /tmp/php_sessions! It seems that my setting is being ignored.
Am I overlooking something? Where could that old setting be buried? Or how can I make the new one effective?
(P.S. - I am not using a redis handler as in another similar SO question)
Ok, I solved my own problem and the general answer is as follows:
There are two more things that can be changing the path and need to be checked,
the PHP code of the application might be changing the ini directive, search the code for ini_set(session.save_path
the PHP code might be using the session_save_path PHP command to override the ini. Search the code for that also (and notice the two underscores _!)
And the specific answer for my case was that SuiteCRM uses session_save_path command to set its path with a value coming from the file config.php found at the web root. That's where I found the old setting, and changing it solved my problem (for good, I hope).

Where to find definition of $Server['DOCUMENT_ROOT'].?

I'm transferring a website to a different server. I need to change all the file paths. In my config folder I found this line of code. Where might I find the definition of
$_SERVER['DOCUMENT_ROOT']
in a standard PHP/MySQL website?
This is defined at the webserver's configuration.
For Apache it is the DocumentRoot directive in the configuration file; for nginx it is the root directive.
Once you have updated the web server configuration correctly, the variable will point to the location specified there. You do not want to manipulate this variable "manually" in your code as it will lead to problems later on.
It should be
$_SERVER['DOCUMENT_ROOT'];
Not
$SERVER['DOCUMENT_ROOT']
And document root will the (folder)path that you have placed at your server,And if you want to change it or view it,You can go through with your configuration of your server.
echo "<pre>";
print_r($_SERVER);
echo "</pre>";.
exit;
add this to top of your page, you will get all server variables... and dont forget to remove this snippet once you are done seeing the values.

Can't seem to set my php.exe path in pear.bat

I installed PEAR and when I try to run it, I receive this message:
PHP_PEAR_PHP_BIN is not set correctly. Please fix it using your
environment variable or modify the default value in pear.bat The
current value is: .\php.exe
In the pear.bat file that error message is generated as such:
:PEAR_PHPBIN_ERROR
ECHO PHP_PEAR_PHP_BIN is not set correctly.
ECHO Please fix it using your environment variable or modify
ECHO the default value in pear.bat
ECHO The current value is:
ECHO %PHP_PEAR_PHP_BIN%
GOTO END
At the top of the file there is a conditional set like so:
IF "%PHP_PEAR_PHP_BIN%"=="" SET "PHP_PEAR_PHP_BIN=.\php.exe"
That's my starting point. I've changed that path to this:
IF "%PHP_PEAR_PHP_BIN%"=="" SET "PHP_PEAR_PHP_BIN=C:\hqp\xampp\php\php.exe"
Regardless of the change, I get the same error in the command line, as in it still thinks the value is .\php.exe. I can tweak the error message and my tweaks will appear in the command line so I'm confident this pear.bat file is being referenced.
So my assumption is, based on that condition, "%PHP_PEAR_PHP_BIN%"!="" and if that is the case, where is it being set so I can override it to my correct path? Ideas where I should look or how to troubleshoot this further?
Here's my pear.bat source code: http://codetidy.com/919/
Thanks!
I was able to override the path by removing the condition:
Replaced
IF "%PHP_PEAR_PHP_BIN%"=="" SET "PHP_PEAR_PHP_BIN=C:\hqp\xampp\php\php.exe"
with
SET "PHP_PEAR_PHP_BIN=C:\hqp\xampp\php\php.exe"
Though this solved my issue, I'd still like to know where that value was being set prior to this file. I confirmed the path was correct in the php_bin value in the pear.ini file as well, of course.
For Windows, must SET "PHP_PEAR_PHP_BIN" under user environment variable to path to php.exe. Like me, I set it to "C:\wamp\bin\php\php5.3.0\php.exe".
This will solve your problem.
For wamp on windows set environment variable as
Go to System>Advanced System Settings.
Then set environment variables as
PHP_PEAR_PHP_BIN
c:\wamp\bin\php\php5.3.13\php.exe
Save
Run cmd and type pear on command prompt.
you shall find it in user variables under environment variables in windows. I was facing the same issue, then changed the value for user variables and it worked.
continuing on in the installation the document at http://pear.php.net/manual/en/installation.checking.php under verify include paths states to run php --ini at the command line. doing this i noticed that Configuration File (php.ini) Path: was set to c:\windows (this is a widows xp machine) I thought this was strange so i went looking for the file to see what was in it. There was no php.ini file but there was a pear.ini file so i decided to look at that. It has to lines in it, the second like is where i found the path. The file read s:7:"php_bin";s:9:".\php.exe" near the end of the line i changed it to s:7:"php_bin";s:9:"C:\php\php.exe" and all was good.
Just thought i would update this as i dont like to change batch files that should not need to be changed.
change the ./php.exe to the actual path eg c:xampp\php\php.
thanks
A simple solution without going withing the environment variable of Windows.
You can edit the PEAR_ENV.reg file available after first installation of Pear. This file should be in your PHP folder. Within this file you will find the following line:
"PHP_PEAR_PHP_BIN"=".\\php.exe"
to
"PHP_PEAR_PHP_BIN"="F:\\PHP5.2\\php.exe"
Change "F:\PHP5.2" to whatever where is you PHP installation.
You can now double-click on the PEAR_ENV.reg file to setup correctly your environment variable.

How can we check if we are running the PHP script on localhost?

Is there a way to check if we are running a PHP script on localhost - development server or on live server - production server? Is there any PHP constant, variable, function,etc that can give me this information.
I need this to put different settings for production and development server. Now I parse the URL to see which one it is but I was wondering s there any better way to do that. My concern is that we may change the URL of the script and that may ruin my check.
I am looking few a solution with one config file and IF condition in it depending on which I will define different settings. The only problem is that I do not want to change the IF statement when there are changes on the server settings like hostname, document_root or something else that I am using to identify local/remote host.
And want to SVN update from one source without changing anything to my production server.
And I would like ideally to be able to run and CRON jobs with these settings.
I use the SetEnv in my host definition to locate on which environment i am running (Dev, Stage, Production) :
<VirtualHost *:80>
(all the host info)
SetEnv SERVER_CONTEXT "dev"
</VirtualHost>
And each config file as an extra word in it : config.dev.ini, config.stage.ini, config.prod.ini, etc, ...
It works like a charm.
if($_SERVER["REMOTE_ADDR"]=="127.0.0.1"){
$local = True;
}else{
$local = False;
}
EDIT
You could also check the first part of the address and see if the server is in the local network, the again assuming your server won't be in the local network when in production
I set an environment variable in the Apache configuration and check that. This has the advantage over using a PHP configuration file that all your application code remains exactly the same on PROD, TEST, DEV etc; no need to go and make changes after a check out, as the code just pulls the config from Apache.
To demonstrate, the following can be set in your VirtualHost configuration
SetEnv ENVIRONMENT PROD
In your PHP code, you can then check the environment with
$env = getenv('ENVIRONMENT');
If you feel the need to make this available everywhere, you can then use define, but I don't find it necessary (I use the specified environment to load the appropriate configuration files and create a read-only Singleton configuration, which is then used to check any configuration options; better than if ($env == 'PROD') {} type code, as all that logic is in the config, not in your code).
Use a config file you include with a define in it
define("DEBUG",true); //set to false for live
and use that in your code, e.g.:
if(DEBUG){}
You can try and use the global $_SERVER['SERVER_NAME'] This should tell you the host name of the system that's running the script. You could use this to determine what settings to use (depending on the system).
I don't know that this will output 'localhost' however and you may need to know your actual host name of your development machine.
The server has no idea what environment it is unless you tell it to. What I do is use DEFINE to set the environment. My application code is the same on every instance but the my configuration files change. That way you can use .htaccess file include the configuration files on every script and check to see what they settings are.
I know this word may sound strange to PHP developer, but have you considered build of your project?
In PHP there's nothing to compile, however changing copied files is one of features of any build process. You could specify 2 targets: production and dev. There would be no need for any conditionals, that should work, or may work, but under some circumstances won't.
i always make "config.php" wich i include to other php files...
it contains something like (home file):
$CONFIG['server']="dev";
$CONFIG['db_user']="root";
and thing like that...
so at home i have this one up there and at server where site is running i have another one wich i dont update if not changes to it...
so on server i have something like:
$CONFIG['server']="prod";
$CONFIG['db_user']="lwu9918_admin";
and then in other php files:
include("config.php");
if($CONFIG['server']=="dev"){echo "Development";}
thing like that!
On dev server
* * * * * php -r cronjob.php this_is_dev
On production server
* * * * * php -r cronjob.php this_is_live
In your script
switch ($argv[1])
case 'this_is_dev':
// load your dev configuration
break;
case 'this_is_live':
// load your live configuration
break;
default:
die('invalid server');
break;
}
since is meant for cronjob, the $argv is exist
good luck with Windows :(
you better put the cli running program outside www directory ,for example
c:\iis\www is the public html directory
the cli file should be put under c:\iis instead

Can I configure environment specific content?

In the same way that you can generate specific content based on browser type is there a way to generate specific content based on the server running PHP without reference to the server or site name?
For example, a way for PHP to automatically detect the environment it was in and configure things like DB connections, ini_set for errors etc. depending if it was a development, ITS, UAT or production environment.
The 2 ways I thought of were to recognise an HTTP header indicating development and QA environments or to have custom properties in php.ini.
I have woken up slightly and found out the php function to read the http headers but php overrides anything I set in the web server and I do not know if they can be set in php.ini at all.
I have no idea if it is possible to add custom values to php.ini but I had a test and ini_get would not find it (I had restarted the web server after changing php.ini of course).
you can specify an environment variable in apache (conf, vhost, .htaccess or as an httpd daem) and then acces it via the ˆ$_ENVˆsuperglobal
I use the following to load different settings for different servers:
switch ($_SERVER['SERVER_NAME']) {
case 'web-host': case '10.0.0.208':
# Set DB Settings
case 'mydomain.com': default:
# Live server settings
}
Not had a problem with it so far
$_ENV / http://www.php.net/manual/en/reserved.variables.environment.php
Using FastCGI on IIS you can set Environment variables. They do not seem to be available to $_ENV but can be retrieved with getenv("varname").
To configure FastCGI environment variables in IIS 5 or 6 you need to edit:
C:\%systemdrive%\system32\inetsrv\fcgiext.ini
For example:
[Types]
php=d:\Dev\PHP\php-cgi.exe
php:1=PHP Site 1
*=Wildcard Mapping
[d:\Dev\PHP\php-cgi.exe]
QueueLength=999
MaxInstances=20
InstanceMaxRequests=500
[PHP Site 1]
ExePath=d:\Dev\PHP\php-cgi.exe
EnvironmentVars=PHPRC:d:\Dev\PHP\,SiteType:Developer
In this instance it is IIS 5 so there is only one site and the site ID is 1 as indicated in line 2 of [Types].
On IIS 6 you may have multiple sites and the following link tells you how to find the Site ID: http://weblogs.asp.net/owscott/archive/2005/07/29/how-to-find-the-siteid-in-iis5-and-iis6.aspx.
IIS 7 can be configured via the UI apparently once the Administration Pack for IIS 7 has been installed.
Another alternative that hasn't been mentioned yet would be to create a server-specific (but with the same name) configuration file that would be included in the beginning of your site script. In that server-specific config file you could set configuration variables as constants. That way, if there was a 'generic' configuration file loaded later, its values could be overridden in the server-specific configuration file as constants can't be redefined. You would want to either exclude the server-specific configuration file name from the synchronizations, or keep it in a path outside of the main content so that it is not accidentally overwritten.
For this purpose you can even configure Constant Arrays with help of Constant Array 2 class.
When server-specific configuration files are used you don't have to worry about the current SERVER_NAME -- this makes it easier for you to define the intended environment regardless of the current system name which could be handy also for QA purposes.

Categories