Description
There is a longstanding issue with mod_php for Apache when PHP extensions can't be loaded without adding PHP directory to PATH because they rely on some libraries which are in the root PHP directory, and the OS looks for them in the root Apache directory by default. Also, it causes an issue when a user has a few versions of PHP installed (with separate copies of Apache), one of them is in the PATH, and all the others try to load their extensions from that directory in PATH, and fail.
It is possible to resolve this issue by adding the directory of php_mod dll into the list of dll directories of current process. You should use the AddDllDirectory
(supported in Windows 7+ since 2011) to specify additional DLL search path. It could be made in the DllMain of the php8apache2_4.dll like this:
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <shlwapi.h>
BOOL APIENTRY DllMain(HMODULE hmodule, DWORD dwreason, LPVOID lpreserved)
{
static DLL_DIRECTORY_COOKIE dircookie = nullptr;
switch (dwreason)
{
case DLL_PROCESS_ATTACH:
SetDefaultDllDirectories(LOAD_LIBRARY_SEARCH_DEFAULT_DIRS);
TCHAR path[MAX_PATH];
if (GetModuleFileName(hmodule, path, MAX_PATH))
{
PathRemoveFileSpec(path);
dircookie = AddDllDirectory(path);
}
break;
case DLL_PROCESS_DETACH:
if (dircookie)
{
RemoveDllDirectory(dircookie);
dircookie = nullptr;
}
break;
}
return TRUE;
}
Another option would be using AddDllDirectory
during early initialization and passing LOAD_LIBRARY_SEARCH_DEFAULT_DIRS
to every LoadLibrary
call.
It makes sense to implement this for better security because a lot of unexpected things can be in PATH and load instead of what was expected. It will also resolve issues like #10076. This issue was already discussed in the mailing list and Christoph M. Becker agreed that it is OK to include such change.