0

I just finished few things on my project, so I decided to publish it to the server we have at school. However, every time I try to run anything it shows nothing at all. Here's some code I use in my index.php:

<?php require '/root.php'; ?>

In my root.php, I specify everything I want to apply to all of my pages on server, for example:

<?php
$root_php = (realpath(dirname(__FILE__)));
?>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0">
<?php
$db_connection = mysqli_connect("localhost", "username", "password", "db_main");
?>

The lines above work fine, but here's the problem: when I try to include any script with forward slash "/" at the beginning to make sure the path "starts" on the path the "root.php" script is executed, the script breaks returning nothing but plain uncompleted HTML page. When I try to look at the source code, it always looks something like this:

<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0">

The code which breaks it looks something like this:

require_once '/account/login_verify.php';

(there's the slash at the beginning, which I need there to ensure the script won't try to find it somewhere else)

I also tried another hosting with PHP & MySQL support, but it returned (almost) same result:

<!doctype html>
<html>
<head>

It works neither with the slash at the beginning, nor with $root_php variable to set the whole path to the script.

Is there any problem with Apache / PHP configuration? If not, why does the code fully work on virtual server while it does nothing on the physical server? Any ideas to try are much appreciated. By the way, I don't have access to reconfigure server at our school, but one of our teachers have and can reconfigure it, if I tell him what he should change.

Thanks in advance!

  • Good start with your web pages would be correct declaration of the HTML document: ` `. The DOCTYPE must be upper case! [Recommended Doctype Declarations to use in your Web document](https://www.w3.org/QA/2002/04/valid-dtd-list.html). – ino Dec 03 '17 at 11:33
  • Second thing to start with is to enable PHP error reporting. [https://stackoverflow.com/questions/1053424/how-do-i-get-php-errors-to-display](https://stackoverflow.com/questions/1053424/how-do-i-get-php-errors-to-display) – ino Dec 03 '17 at 11:38
  • Third thing - I do not know the details about your school environment, but I would say your project will not be located up in the web root.. so try following: `` – ino Dec 03 '17 at 11:41
  • Thank you so far for your suggestions. I will change "doctype" to uppercase. I also checked PHP error reporting and it's enabled. And yes, everything is located in the web root, so it should be fine. – David Langr Dec 03 '17 at 13:14
  • Is the `` very first line in your index.php? – ino Dec 03 '17 at 13:18
  • It's right under the tag. – David Langr Dec 03 '17 at 14:44

1 Answers1

0

Try not to include/require files with absolute paths unless you pull in the paths via some kind of external configuration setting.

Absolute paths will vary depending on the system you install your app on. So your best option is to start from a known entrypoint to the filesystem (like, the location of the current file) and address things relatively from there.

That way your code will be portable across systems, no matter where in the filesystem it will live.

So instead of <?php require '/root.php'; ?>, try <?php require 'root.php'; ?>, if root.php is in the same directory as your current file. If not, you can address it relatively, like so:

<?php require realpath(__DIR__ . '/../../root.php'); ?>

(adapt the actual path to what your filesystem layout looks like)

For extra credits, if you want a glimpse at how modern PHP applications handle directory layout and code inclusion, have a look at the PSR4 standard and how that is used:

https://www.google.de/search?q=psr4+tutorial