Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP: possible to set constants in default php.ini file

Tags:

php

apache

I want to have a constant (a string) that is available to all PHP-scripts on the server.

According to http://php.net/manual/en/function.parse-ini-file.php this is quite easy if you parse an extra .ini file, however I don't want to parse an extra file, I want to set my constant in the global php.ini without having to parse anything in the scripts. (In fact that's the whole point because I need the constant to find the stuff to include/parse/etc: When I know where this extra .ini file would be, I don't need it anymore!)

Just inventing a new constant in php.ini and then trying to access it with ini_get() does not work, is there any other way?

I compile Apache and PHP myself, so I could also set the constant at compile-time and/or use Apache-constants if that is neccessary.

like image 726
Robby75 Avatar asked May 25 '12 09:05

Robby75


People also ask

Where do I put constants in PHP?

Define your constant in your top . php file, that will be included in all the other scripts. It may be your front controller, your config file, or a file created for this single purpose.

What is default constant in PHP?

By default, a constant is case-sensitive. By convention, constant identifiers are always uppercase. A constant name starts with a letter or underscore, followed by any number of letters, numbers, or underscores. If you have defined a constant, it can never be changed or undefined.

Can I use const in PHP?

Constants that are declared using the define() function become globally available to the current PHP script. You can even use it within functions and classes. To showcase this, let us write a simple script. This script will contain a function called foo() that will output the value of our constant when called.

Are PHP constants global?

Like superglobals, the scope of a constant is global. Constants can be accessed from anywhere in a script without regard to scope.


2 Answers

You can use a PHP auto_prepend_file script in your PHP ini to do this as it will be run before any of your user-land scripts:

Specifies the name of a file that is automatically parsed before the main file. The file is included as if it was called with the require function, so include_path is used.

So you can add an ini line like:

auto_prepend_file="/home/user/script.php"

The in /home/user/script.php:

define('CONSTANT_NAME', 'your nice value here');

Now in your PHP scripts you can access CONSTANT_NAME from wherever you like as it is available in all PHP scripts.

I use this technique on my staging server that uses mod_rewrite based mass virtual hosting so I can give my PHP scripts an accurate document root. I have discussed this in a blog post before.

like image 61
Treffynnon Avatar answered Sep 28 '22 07:09

Treffynnon


Simple. Create a file xxx.php where you define your constant and add that file to the global include_path in php.ini. I would like to add though that keeping this kind of global constants or variables is not a recommended way of working as you may forget where are the constants coming from and is not making your apps very portable and explicit to other developers.

like image 30
Elzo Valugi Avatar answered Sep 28 '22 05:09

Elzo Valugi