Issue When Including PHP File Located Outside Document Root

Problems with the Windows version of XAMPP, questions, comments, and anything related.

Issue When Including PHP File Located Outside Document Root

Postby bryman79 » 30. June 2017 19:35

I am attempting to use AJAX in an HTML file in the "DocumentRoot" folder to return a response from a PHP file. It works fine if the PHP file is located in the “DocumentRoot” folder, but does not work if it is located outside the “DocumentRoot” folder and included by another PHP file located in the “DocumentRoot” folder. Specifically, no response is received from the server and the following error is returned via the PHP error log (memory_limit set to 1M to reduce the amount of time before the PHP error is presented – a higher memory limit only increases the delay before presenting the error):
Code: Select all
PHP Fatal error:  Allowed memory size of 2097152 bytes exhausted (tried to allocate 4096 bytes) in C:\Users\Username\Desktop\site\DocumentRoot\test.php on line 1.
No other errors are thrown in the PHP error log or the Apache error log.

I am running XAMPP (version 7.0.18) on a Windows 10 machine. The path for the “DocumentRoot” folder is set in the httpd.conf file:
Code: Select all
DocumentRoot "C:\Users\Username\Desktop\site\DocumentRoot"
<Directory "C:\Users\Username\Desktop\site\DocumentRoot">   
  Options Indexes FollowSymLinks Includes ExecCGI   
  AllowOverride All   
  Require all granted
</Directory>


I created a simple test case with a “test.php” file and a “test-process.php” file. The “test.php” file has the following code (in its entirety):
Code: Select all
<?php include 'test-process.php'; ?>


The “test-process.php” file has the following code (in its entirety):
Code: Select all
<?php
  $output['message'] = 'Hello World';
  echo json_encode($output);
?>


The following include_path is set in the php.ini file:
Code: Select all
include_path = ".;c:\Users\Username\Desktop\site\includes"

Removing/renaming the PHP file from the includes folder and removing/renaming the includes folder itself have no effect on the error. I would expect to see some error stating that the file or directory could not be found, but there is none.

I tried using "include_once" (instead of "include") in the "test.php" file and no errors are given. So perhaps the "Allowed memory size..." error is caused from attempting to repeatedly include the "test-process.php" file.

I am new to XAMPP and cannot figure this out. Although I haven’t tried this exact situation on a production server, I have used AJAX in the past with no such issues on production servers. Any suggestions would be appreciated.
bryman79
 
Posts: 2
Joined: 30. June 2017 18:56
XAMPP version: 7.0.18
Operating System: Windows 10

Re: Issue When Including PHP File Located Outside Document R

Postby Nobbie » 01. July 2017 10:50

I am very very (very!) sure that you also have a "test-process.php" in your DocumentRoot, which also includes test-process.php or test.php and so you are running in a forever loop. When i do so, my test behaves exactly(!) as yours: no error message if i remove the include folder and after a while i get "Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 32768 bytes) in /hdd/lampp/htdocs/test-process.php on line 1".
Nobbie
 
Posts: 9562
Joined: 09. March 2008 13:04

Re: Issue When Including PHP File Located Outside Document R

Postby bryman79 » 02. July 2017 04:09

You are correct - many thanks! I had originally setup the test case using a "test-process.php" file in the "DocumentRoot" folder which includes the "test-process.php" file outside of the "DocumentRoot" folder, but added a new file "test.php" (without deleting the original "test-process.php" file in the "DocumentRoot" folder) to run the test case before posting.

However, I have done this before (e.g, included files outside the root directory with the same name as the file with the include statement located in the root directory ) on production servers without any problems. I have to presume this is because it is not looking in the root directory for files to be included. Is this unique to XAMPP? Is there a line that can be added to the php.ini or .htaccess files which removes the root directory from the include path or otherwise prevents including files from the root directory?
bryman79
 
Posts: 2
Joined: 30. June 2017 18:56
XAMPP version: 7.0.18
Operating System: Windows 10

Re: Issue When Including PHP File Located Outside Document R

Postby Nobbie » 02. July 2017 10:24

bryman79 wrote:I have to presume this is because it is not looking in the root directory for files to be included. Is this unique to XAMPP?


No, its not unique to Xampp (neither unique to your online server), it is unique to... YOURSELF!!

bryman79 wrote:Is there a line that can be added to the php.ini or .htaccess files which removes the root directory from the include path or otherwise prevents including files from the root directory?


Yes, of course!! YOU applied this line to php.ini:

Code: Select all
include_path = ".;c:\Users\Username\Desktop\site\includes"


Why did you include the dot "." in the very first position? The dot "." means "current directory" and if you are starting a script from the DocumentRoot, the DocumentRoot becomes the "current directory". Therefore PHP at first looks in your DocumentRoot in this case, as you started test.php from DocumentRoot and as you also put a test-process.php into the DocumentRoot, PHP found that and included that. ITS ALL ON YOU!! Either remove the dot from the include path or put it at the end of include_path, so that PHP first looks into your user folder.
Nobbie
 
Posts: 9562
Joined: 09. March 2008 13:04

Re: Issue When Including PHP File Located Outside Document R

Postby Altrea » 02. July 2017 14:08

Always keep your includes independend from current and working directory.
Build absolute paths with the help of __DIR__.

I don't know a really good english post about that topic, but a brilliant german one
https://stackoverflow.com/questions/324 ... quire-once (english)
http://www.ermshaus.org/2012/09/php-inc ... s-ohne-dir (german)
We don't provide any support via personal channels like PM, email, Skype, TeamViewer!

It's like porn for programmers 8)
User avatar
Altrea
AF Moderator
 
Posts: 8845
Joined: 17. August 2009 13:05
XAMPP version: several
Operating System: Windows 10 Pro x64


Return to XAMPP for Windows

Who is online

Users browsing this forum: No registered users and 19 guests