3

I am making a php website with a simple php website template

This is the index.php looks like

require './config.php';
require './functions.php';
//echo 'hello';
run();

the run() function is in functions.php

function run() {
    include config('template_path') . '/template2.php';
}

and this is template2.php file

include 'header.php';
pageContent();
include 'footer.php';

When I run the site in localhost using WAMP server, it works perfectly without any problem.

But when I upload this project into the amazon server , the server returns HTTP 500 error. When I comment the require statements , the page works(displays hello). I am a php beginner and I have no idea whats the problem and need help.

This is the error page Error page in browser

This is the .htaccess file

Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?page=$1 [L]

Folder Structure enter image description here

config .php

   <?php

/**
 * Used to store website configuration information.
 *
 * @var string
 */
function config($key = '') {
    $config = [
        'name' => 'Test',
        'nav_menu' => [
            'home' => 'Home',
            'about-us' => 'About Us',
            'portfolio-works' => 'Portfolio',
            'contact' => 'Contact',
        ],
        'portfolio' => [
            [ 'Test','Test', "assets/img/portfolio/test.jpg",'movie'],
            ['Test','test1', "assets/img/portfolio/test.jpg",'documentary'],
            ['Test','test2', "assets/img/portfolio/test.jpg",'advertisement'],
        ],
        'template_path' => 'template',
        'content_path' => 'content',
        'pretty_uri' => true,
        'version' => 'v2.0',
        'link' => '/peekay',
        'about_us' => 'Lorem ipsum',
    ];

    return isset($config[$key]) ? $config[$key] : null;
}
Bob
  • 88
  • 1
  • 2
  • 9
  • `require './config.php'; require ('./functions.php');` - just for a test, try them both with parentheses. But if you have any error logs on the server that'll help show where the issue lies. Or build the page line by line to see which bit fails :) – Andy Holmes Aug 08 '17 at 13:39
  • 2
    Do you have any error logs? – Jerodev Aug 08 '17 at 13:39
  • whats your directory structure? I think you dont need the dots before slashes – leoap Aug 08 '17 at 13:40
  • 2
    The slashes for the header and footer are unnecessary and will break your script. When it comes to PHP, a beginning slash looks at the root of the system, not your website. – aynber Aug 08 '17 at 13:41
  • 4
    On a 500 Internal Server Error, the absolute first thing you go and do is check the server logfiles. – CBroe Aug 08 '17 at 13:41
  • where can I find the logs? – Bob Aug 08 '17 at 13:42
  • 2
    @CBroe but these are missing the point (no pun intended) `include '/header.php'` .. so you need to write `include 'header.php'` or in general `./file.php`, Also notice that `require` and `include` are **not** functions, so the `( )` are not needed. – xander Aug 08 '17 at 13:44

2 Answers2

5

This is mostly conjecture without looking at the server error logs (talk to your web host or check the support site for information on how to get those), but you'll have issues with your includes and requires depending on the path of your page.

This will always look in the folder of your current page:

require './config.php';
require ('./functions.php');

And this will always look in the root of the system, not the website

include '/header.php';
pageContent();
include '/footer.php';

Take advantage of $_SERVER['DOCUMENT_ROOT'] to inject the path of where your web files live:

require $_SERVER['DOCUMENT_ROOT'].'/config.php';
require ($_SERVER['DOCUMENT_ROOT'].'/functions.php');

And

include $_SERVER['DOCUMENT_ROOT'].'/header.php';
pageContent();
include $_SERVER['DOCUMENT_ROOT'].'/footer.php';

You can add subdirectories after the DOCUMENT_ROOT if you need to.

aynber
  • 22,380
  • 8
  • 50
  • 63
  • `require $_SERVER['DOCUMENT_ROOT']. '/peekay/config.php';` `require $_SERVER['DOCUMENT_ROOT']. '/peekay/functions.php';` `echo $_SERVER['DOCUMENT_ROOT'].'-hello';` `//run();` This should print hello. but not working, same error – Bob Aug 08 '17 at 15:15
  • I commented the run() function – Bob Aug 08 '17 at 15:18
  • If you're still getting a 500 server error, you really need to find the server error logs and look at them to find out what's going on. All 500 means is that something went wrong. I'd also suggest creating just a test file `info.php` and put ` – aynber Aug 08 '17 at 15:27
  • /peekay is the subfolder – Bob Aug 08 '17 at 15:28
  • root is set to `/var/www/html` – Bob Aug 08 '17 at 15:30
  • Okay, then you'll definitely need to find those server files, or [turn on error debugging](https://stackoverflow.com/questions/845021/how-to-get-useful-error-messages-in-php) – aynber Aug 08 '17 at 15:40
  • I checked the error log found this `[Tue Aug 08 15:26:23 2017] [error] [client 137.97.10.41] PHP Parse error: syntax error, unexpected '[' in /var/www/html/peekay/config.php on line 9` seems like an array declaran problem. I will update the code in the question – Bob Aug 08 '17 at 15:43
  • 2
    It sounds like you're running a version less than PHP 5.4. That's when the short syntax was added. Try changing the array declarations from `[]` to `array()` – aynber Aug 08 '17 at 15:51
  • Upgraded php to 5.6. Now everything is fine. Thanks – Bob Aug 08 '17 at 19:09
2

Upload a script (test.php) with the following in it:

<?php
phpinfo();
?>

Run that in your browser and do a search for $_SERVER['SCRIPT_FILENAME'] (or just "SCRIPT_FILENAME")

That will give you a full path to where your files are, e.g.

/some/dir/test.php

This path (the absolute path, /some/dir/) can be used to reference anything else that needs to be included/required within your webspace, e.g.

require '/some/dir/config.php';

You can use this to test whether it resolves the problem. If it does, the problem is the path to the included/required files. You can update this to a relative link eventually - so as not to hardcode the absolute path. But it should tell you whether that's the source of the problem, which I highly suspect it is.

Another source of the problem may be the line

include config('template_path') . '/template2.php';

Presumably config('template_path') is supposed to be a string read from somewhere. It's possible that this is not evaluating and so it's trying to load /template2.php. If it has nothing preceding it, it will treat the / as the "root" which is almost certainly not where that file is on the server. Again, hardcoding an absolute path in to test things may help.

Other than this you should also look at the server's error_log which your host should be able to point you to. It's possibly accessible via a control panel (e.g. Plesk) depending on what the server has installed.

Andy
  • 5,142
  • 11
  • 58
  • 131