简体   繁体   English

使PHP网站指向的路径与代码中的路径不同

[英]Having PHP site point to a different path than one that is in the code

I am running a PHP site on Windows using Wampserver. 我正在使用Wampserver在Windows上运行PHP网站。 All throughout the site there is a hardcoded line such as: 在整个站点中,都有一条硬编码的行,例如:

$settings = parse_ini_file("/usr/local/apache2/myconfigs/settings.ini", true);

I know this is bad practice to begin with but it is out of my control. 我知道这是一个坏习惯,但我无法控制。

When the site runs is there any possible way I can trick the site to point to C:\\wamp64\\bin\\apache\\apache2.4.27\\myconfigs\\settings.ini whenever the code is looking for /usr/local/apache2/myconfigs/settings.ini in the parse_ini_file function? 当站点运行时,有任何可能的方法可以诱骗站点指向C:\\wamp64\\bin\\apache\\apache2.4.27\\myconfigs\\settings.ini只要代码正在寻找/usr/local/apache2/myconfigs/settings.ini在parse_ini_file函数中的/usr/local/apache2/myconfigs/settings.ini

$settings = parse_ini_file(APACHE_INI_PATH, true);
// $settings = parse_ini_file("/usr/local/apache2/myconfigs/settings.ini", true);

This is a bit hackish but I think it's what you are looking for, so the trick here is to redefine the parse_ini_file function and make it ignore the invalid passed path ( "/usr/local/apache2/myconfigs/settings.ini" ) and use your correct file instead. 这有点骇人听闻,但我认为这正是您要寻找的东西,因此这里的技巧是重新定义parse_ini_file函数并使它忽略无效的传递路径( "/usr/local/apache2/myconfigs/settings.ini" )和请改用正确的文件。

This seems straightforward but a bit tricky since your new function should also call the original parse_ini_file function somehow, that's why you need to rename it first then override it 这似乎很简单,但是有点棘手,因为您的新函数还应该以某种方式调用原始parse_ini_file函数,这就是为什么您需要先重命名然后覆盖它的原因

You will need PHP runkit extension enabled for this, have look at runkit_function_redefine and runkit_function_rename for reference. 您将需要为此启用PHP Runkit扩展 ,请查看runkit_function_redefinerunkit_function_rename作为参考。

Untested but should work, the code should be something around these lines : 未经测试但应该可以工作,代码应该围绕以下几行:

runkit_function_rename('parse_ini_file','original_parse_ini_file');
runkit_function_redefine('parse_ini_file', function() {
    original_parse_ini_file("C:\wamp64\bin\apache\apache2.4.27\myconfigs\settings.ini", true);
});

Make sure the code above is executed at the start of your application script and any parse_ini_file call should use your file instead of the hardcoded one. 确保上面的代码在应用程序脚本的开头执行,并且任何parse_ini_file调用都应使用您的文件,而不是硬编码的文件。

If there is no single entry point to your application where you can put the code above, you can put it in a separate script and make PHP load before running any script via the auto_prepend_file setting in your settings.ini file, also make sure that runkit.internal_override is set to On since parse_ini_file is not a user defined function. 如果您的应用程序没有单个入口可以在上面放置代码,则可以将其放在单独的脚本中,并在通过settings.ini文件中的auto_prepend_file设置运行任何脚本之前加载PHP,还请确保该runkit.internal_override由于parse_ini_file不是用户定义的函数,因此runkit.internal_override设置为On

Hello please check this 你好,请检查一下

runkit_function_rename('parse_ini_file','o_parse_ini_file');
runkit_function_redefine('parse_ini_file', function($p1,$p2)  use($someCondition) {
    if($someCondition)
        o_parse_ini_file("C:\wamp64\bin\apache\apache2.4.27\myconfigs\settings.ini", true);
    else
        o_parse_ini_file($p1,$p2);

});

it can return 它可以返回

Call to undefined function runkit_function_rename() 调用未定义的函数runkit_function_rename()

to fix this error please read here 要解决此错误, 请在此处阅读

or here 或在这里

If you don't want to do a find and replace as suggested by @cddoma, I propose that you create the directory /usr/local/apache2/myconfigs/ in your windows machine, and copy the file settings.ini from C:\\wamp64\\bin\\apache\\apache2.4.27\\myconfigs\\settings.ini to that directory. 如果您不想按照@cddoma的建议进行查找和替换,建议您在Windows计算机中创建目录/ usr / local / apache2 / myconfigs /,然后从C:\\复制文件settings.ini。 wamp64 \\ bin \\ apache \\ apache2.4.27 \\ myconfigs \\ settings.ini到该目录。

Open your windows command line and enter the following 打开Windows命令行并输入以下内容

mkdir C:\\usr\\local\\apache2\\myconfigs\\

copy C:\\wamp64\\bin\\apache\\apache2.4.27\\myconfigs\\settings.ini C:\\usr\\local\\apache2\\myconfigs\\

您可以通过Windows上符号链接来执行此操作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM