简体   繁体   中英

requiring using /var/www works but using localhost doesnt work

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.

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.

要将其作为静态引用,可以将其编码为:

require($_SERVER['DOCUMENT_ROOT'] . '/TEST/database.php'); // $_SERVER['DOCUMENT_ROOT'] gets you '/var/www' or whatever your server root maybe
<?php require("database.php");?>

<?php require("database.php"); ?>

No Need To require Full Path When You Are Working in One Folder

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM