doujiabing1228 2018-05-29 02:09
浏览 85
已采纳

PHP require_once不止一次被调用

I posted the solution as an answer

I'm running into an odd issue while migrating some PHP code to a new server. The old server is running PHP 5.3 (x86) and the new server is running PHP 7.0 (x64). Both are running on IIS 7.5.

The code causing the issue has been in production without issues for almost three years. It runs fine on the current production server as well as two development systems. All three are running PHP 5.3 (x86).

These errors do not occur when I use PHP 5.3 (x86) on the new server.

A shared file (named _common_funcs.php) contains common functions that are called from many scripts. Since some of these scripts can interact I use require_once('_common_funcs.php') to include the file. This is approach pretty basic.

On the new server the first time a the script runs it generates a 'Cannot redeclare' error. Refreshing the page does not display the error - it displays the page as expected. For example:

  • //localhost/app/display_account.html?account=123456 generates the error.
  • //localhost/app/display_account.html?account=123456 again does not display the error.

Restarting the IIS server causes the error to be generated again on the first run.

Now for the odd part. Note the capitalization of the drive letters in the error below:

Cannot redeclare acct_has_graduated() (previously declared in D:\app_directory\includes\_common_funcs.php:2061) in d:\app_directory\includes\_common_funcs.php on line 2061

If I hardcode the full path in one require_once() line, and use a lowercase 'd', then the error in the log will swap the capitalization. This results is 'd:\app ...' and 'D:\app ...'.

If I hardcode both calls that happen to get called when this script runs with the error is not generated no matter what mix of capitalization I use.

And to make this just a little more interesting, the acct_has_graduated() function is the last function in the _common_funcs.php file. But if I disable OpCache in php.ini I get the following error:

Cannot redeclare update_ledger() (previously declared in D:\app_directory\includes\_common_funcs.php:7) in d:\app_directory\includes\_common_funcs.php on line 42

Two things of note: update_ledger() is the first function in the _common_funcs.php file, and "line 42" is the closing bracket of the update_ledger() function (line 7 is the declaration).

If anyone can point me in the right direction on this I would greatly appreciate it.

  • 写回答

2条回答 默认 最新

  • donglu3184 2018-05-29 11:43
    关注

    Solution

    I found the issue and the file is absolutely being included twice. This happens under a specific set of circumstances. It affects PHP 7.0 but does not affect PHP 5.3. I did not test any other versions of PHP.

    This scenario is certainly an outlier, but here it is:

    1. I have a directory symlink from c:\inetpub\wwwroot\app_location to d:\app_location.
    2. The IIS configuration for this site points to c:\inetpub\wwwroot\app_location.
    3. The application's configuration file defines the include_path using c:\inetpub\wwwroot\app_location as the base.
    4. All the files for this site are located in d:\app_location.

    Using the configuration above all entries in the application's log files (tracking, audit, error and backtrace) show D:\app_location as the base location of all files.

    Calling require_once() on a file more than once, while allowing the include_path to resolve the location of the file in one or both of the calls, generates the 'Cannot redeclare' error. The error reports the opposite capitalization of the 'D:' drive letter (see error text in the post above).

    Changing the base of the include_path (#3 above) to d:\app_location resolved this issue.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上