0

I am in the process of moving a PHP website from a shared hosting server to a DigitalOcean server. I have come across an issue that I'm not sure how to debug.

There is a page that has a log-in form with action=2/login.php. This form is located in /var/www/html/calendar.php (it is a Ubuntu 14.04 DO droplet). When submitting the form, there is a 500 Internal Server Error. I've found the error in question but I am not sure how to debug it (because it runs fine on the other server).

/var/www/html/2/login.php

<?php
require_once('../db_connect.php');
...

db_connect.php is located at /var/www/html/db_connect.php

When I $php 2/login.php I get the following error:

login_error.txt

PHP Warning:  require_once(../db_connect.php): failed to open stream: No such file or directory in /var/www/html/2/login.php on line 4
PHP Fatal error:  require_once(): Failed opening required '../db_connect.php' (include_path='.:/usr/share/php:/usr/share/pear') in /var/www/html/2/login.php on line 4

When I run $php login.php when in directory /var/www/html/2 it works fine (which I guess makes sense, as ../db_connect.php is a relative path).

I want to know why it is working on the other server and not the DO server?

The php.ini is the same (which could be a problem), but on Server #1 (Verve), it is shared hosting, so our root is /home/[username]/public_html and on the new server is is /var/www/html.

I can link to the live sites in question if necessary (but would prefer not to). The /var/www/html/2/login.php and the /var/www/html/calendar.php scripts are exactly the same, and while I understand this will definitely cause issues later as the absolute path to the root of the web application is different, I don't know what would be causing this specific issue.

I have also looked at this question which is very similar: PHP require_once resetting to current working directory. However, this code works on the other server, and not here, so I think there is something more than just a lack of a relative file path, and I would prefer to find the solution that explains why the script isn't working rather than patching the script, as I'm pretty sure I'd have to change every single other script that uses require_once('../db_connect.php') as well.

Thanks for any help!

Community
  • 1
  • 1
Vishaal Kalwani
  • 730
  • 1
  • 7
  • 20
  • Duplicate question. Follow the below chain. http://stackoverflow.com/questions/2253625/php-require-once-not-working-the-way-i-want-it-to-relative-path-issue?rq=1 – Anil Pediredla May 29 '15 at 20:33
  • The question you linked is similar to mine but I am asking a different question. I know what will solve my problem, but I want to know what is the underlying cause of the problem (why does the original code work on the first server but not the second?). I have edited my question to reflect this. Thanks for the link though! That information is still helpful. – Vishaal Kalwani May 29 '15 at 20:38

1 Answers1

2

Did you try using __DIR__?

require_once(__DIR__ . '../db_connect.php');
taxicala
  • 21,408
  • 7
  • 37
  • 66
  • I did use `__DIR__` and it "fixed" the issue, but I am more interested in why it is failing on one server and working on the other. There is no `__DIR__` on the original script and it is working without errors, and I want to avoid having to prepend `__DIR__` to all the require's. Right now I'm just migrating the files, the refactoring will hopefully come later. – Vishaal Kalwani May 29 '15 at 20:33
  • As far as I know, the server configuration has a lot to do with this. You are looking your files with a relative path, but the root path may not be the same in both of your servers, I usually solve this kind of issues by having `chdir(dirname(__DIR__));` In my index.php root file, in order to make everything relative to the application root. – taxicala May 29 '15 at 20:35
  • but the chdir trick only works if you are using a framework that always dispatches the application from your index.php file ;) – taxicala May 29 '15 at 20:36
  • Haha unfortunately this application is quite old and doesn't make use of any frameworks. It is just a large collection of .php scripts that have grown over the years. Thanks for the tip! I'll see if I can try it out – Vishaal Kalwani May 29 '15 at 20:40