📜 ⬆️ ⬇️

Client and CGI programs in PHP with common environment variables in bash_profile

I work at work on various small projects. I decided to share with you what I’m doing, as it seems to me that I’ve gone the wrong way, or I’m doing absolutely perversion :-)

It will be about how you can use the environment variables described in the ~ / .bash_profile file in PHP programs, and how to match the OS user directory with the URL from the server PHP process in order for the PHP interpreter to include classes from this directory . All this is necessary in order for the working version of the project to be in the home directory of one user, and the test versions in the home directories of other users.

In general, it turned out that there are two (and before that there were three) users on the Solaris OS on the server where the development is being conducted. Still have SVN. There is also a DBMS (in this case it doesn't matter which one). In each user's home directory there is a working copy, but there is one special user ... This user always has a working debugged version of files, scripts and everything else. All other directories are under development.
Now a little more about what is in working copies (they are home directories). They have:

The .bash_profile file is used as a repository of environment variables that are needed in all scripts. So historically. In addition, I find this a convenient solution in the circumstances. In bash programs, environment variables are used perfectly. PHP client programs use the getenv () function. But in PHP CGI programs ... This is the most interesting part of the note.

If the bash programs and PHP client applications can use the $ USER variable and the getenv ('USER') function, respectively, then in the case of CGI PHP, everything is more complicated. The situation is complicated by the fact that the process of the WEB server is in no way connected with the users of the OS. I had to invent. Some solutions to the problem of binding the current PHP process when executed by its WEB server are:

It turns out that the transfer of information about the user name is possible only through the URL. I chose the second method, probably because it was too lazy to start subdomains.
Thus, after the implementation of this cunning mechanism, which I will discuss below, when entering at the address of the form http: // server / production / PHP program:
  1. takes the last part of the URL
  2. looks for the / home / production directory in the FS
  3. adds the / home / production / php / include path (which contains the classes) to the include_path variable
  4. Reads the .bash_profile file of the selected user (/home/production/.bash_profile)
  5. in it, the function preg_match searches for environment variables and writes them into the $ _ENV array
  6. further classes are connected by the __autoload handler in which there are some specific handlers

production - the name of the user who has checked working files and programs. Other users (user1, user2, etc.), as already mentioned, have working copies in which they are being developed. Thus, we have classes connected from the directory of the desired user, and environment variables from .bash_profile.
')
In addition, there is also a database with schemas called pairs * and * _test. For example, stat and stat_test. Programs running from a production user connect to the first kind of schemes, all other users connect to the second one, using them for debugging with test data sets. In addition, PHP programs from the production user directory, running with the --test switch, will access * _test type schemes, which is done for debugging convenience.

I hope that explained clearly. Once again, I’ll emphasize that the above should not be taken as advice or guidance, and should not be repeated either, as this is a private and very specific decision that I use. This note should be understood as a story about everyday programming in PHP or a fantastic story :-)

Source: https://habr.com/ru/post/48723/


All Articles