This question is similar in concept to this one, except I see I need more deep integration with Apache.
I am authenticating users via PHP and setting a cookie with a session ID that can then be looked up in a database table to find the user name. I need to also set the REMOTE_USER variable in Apache so I can pass that variable via an AJP connection and a regular HTTP connection to other application servers to use which can’t be set to read the information from a cookie.
It appears I need to integrate more deeply with Apache (Version 2), perhaps even writing a module, to do this properly. Does anybody have any example of how this might be accomplished with as little non-PHP glue code as possible? While I’m certainly familiar with several other languages, I was hoping to keep this project as simple as possible for those who will have to maintain it after me.
Thanks for any pointers!
PHP does not provide PHP scripts access to Apache variables. Although there is a function to set environment variables, it is reported that it does not set REMOTE_USER.
You can do what you want before PHP handler starts and after it ends by adding other handlers to phases before and after PHP works. You can write your handler functions in C; but since you mean to have a solution that PHP programmers can access after you, I believe Python is a very good alternative.
mod_python allows you to do this very easily. If you go to its website, you can see that its last update is February 2008; but it is mature and stable. They do not continue development because the current Python-Apache integration continues on mod_wsgi instead of mod_python. Since mod_wsgi sticks with WSGI, it does not provide Apache-specific functionality as mod_python does.
After installing mod_python, all you need to have is writing simple functions that will run when you want them to. Below is an example of running two functions before and after PHP so you can access a cookie that was set by PHP.
Apache Configuration:
This configuration needs set_auth_user.py where Python can import it. You can put it in site-packages folder of Python installation.
Here is our Apache module written in Python:
Here is our PHP file:
Here is my Apache log after a request is made to server:
I hope this solves your problem the way you needed. I added two handler functions for demonstration. If you need only one of them, remove the line in Apache configuration so there won’t be a useless method call for every request.