Vishaal Kalwani Vishaal Kalwani - 1 month ago 5
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

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!

Answer

Did you try using __DIR__?

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