Vishaal Kalwani Vishaal Kalwani - 14 days ago 4x
PHP Question

require_once in PHP giving errors on new server, but not on original

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

. This form is located in
(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).



is located at

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


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
it works fine (which I guess makes sense, as
is a relative path).

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

is the same (which could be a problem), but on Server #1 (Verve), it is shared hosting, so our root is
and on the new server is is

I can link to the live sites in question if necessary (but would prefer not to). The
and the
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
as well.

Thanks for any help!


Did you try using __DIR__?

require_once(__DIR__ . '../db_connect.php');