Reputation: 151
I am new to working on linux environment and i am also working on a php project on linux. And I faced a problem while requiring php files. I have two php files called test.php and database.php. Both this files are in /var/www/TEST/.
This the code in database.php
<?php echo("You have included the database"); ?>
And this is the code in test.php
<?php require("/var/www/TEST/database.php");?>
Then when i browse to test.php using firefox i get the expected output "you have included the database". but when I change test.php to
<?php require("localhost/TEST/database.php"); ?>
I get no out put when I browse to test.php. I have started the apache service and also changed the file access of /var/www/ to 755. I hope you could help me on why I am getting this kind of result. Thank you.
Upvotes: 1
Views: 116
Reputation: 1106
To make it as a static reference you can code it like:
require($_SERVER['DOCUMENT_ROOT'] . '/TEST/database.php'); // $_SERVER['DOCUMENT_ROOT'] gets you '/var/www' or whatever your server root maybe
Upvotes: 1
Reputation: 32270
First you should enable error reporting:
<?php
// Put these lines to the top of your script
error_reporting(E_ALL);
ini_set('display_errors', true);
ini_set('display_startup_errors', true);
ini_set('xmlrpc_errors', true);
Then you try to include a file via HTTP stream, but you forgot http://
or https://
before localhost
and including via stream is disabled for security reason.
The configuration is allow_url_include
. It's off by default and you should leave it off.
Try to get into composer / psr classmaps / psr autoloading. This is kinda "industry standard" mechanism. You should go for that.
Upvotes: 1
Reputation: 86
<?php require("database.php");?>
<?php require("database.php"); ?>
No Need To require Full Path When You Are Working in One Folder
Upvotes: 0