Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP require causing HTTP 500 error

Tags:

php

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;
}
like image 796
Bob Avatar asked Jul 27 '26 21:07

Bob


2 Answers

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.

like image 105
aynber Avatar answered Jul 29 '26 11:07

aynber


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.

like image 38
John Avatar answered Jul 29 '26 11:07

John



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!