3

in /var/www/ is my main.php file:

<?php
include("../config/config.php");
header('Content-type: text/plain');
echo $user;
?>

in /var/config/ is my config.php file

<?php
$user = "myUser";
?>

When calling main.php in my browser I'm getting a blank site, instead of "myUser".

2 Answers 2

1

If your document root is set to "/var/www/" then you should work in that directory.

Your code does not work properly because of the "/", on Windows it doesn't matter but on Linux it does and it's different.

Anyway, a more better example would be this:

<?php
include("..".DIRECTORY_SEPARATOR."config".DIRECTORY_SEPARATOR."config.php");
header('Content-type: text/plain');
echo $user;
?>

And now main.php will be able to access your config.php on any operating system.

Sign up to request clarification or add additional context in comments.

8 Comments

This still gives me a blank site
@LisaCleaver That is not possible, I have tested before I posted this... check if you actually have permisions on "/var/config/"
@LisaCleaver Or even better, move config folder to /var/www/config/ and then call "include("config".DIRECTORY_SEPARATOR."config.php");"
My browser returns HTTP ERROR 500. The config file contains a database password as well, that's why I dont want it to be in /www
Then you have other issues, turn on error_reporting to see what's going on
|
0

This worked for me!

include dirname(__FILE__).'/../config/config.php';

3 Comments

This answer is incomplete, and it would not work on both Linux and Windows.
You can use realpath function for that
This still gives me a blank site as well

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.