简体   繁体   中英

php include with wamp

I'm developing a site on my local wamp stack. I have created an alias to view the site so i go to localhost/eee/ to view it. Ideally i would like to go to www.eee.lo but ever since upgrading to win8 I can't get it to work.

So this is the problem, i'm making modules for the website so i don't have to change all the code etc... And i don't want to have to go around changing all the url's when i migrate to the online server so i'm creating a file called _control.php which has this;

$_SITELOC = "localhost/eee/";

And then each time i want to include a file i will go;

include "$_SITELOC/scripts/inc/_header.php";

But this doesn't work and i can't work out why as if i echo it rather than include it and then i take what it prints and put it into the url it goes to the correct file. But it throws errors on the include, it gives two warnins;

Warning: include(localhost/eee/scripts/inc/_header.php) [<a href='function.include'>function.include</a>]: failed to open stream: No such file or directory in C:\Users\Chris\Documents\EEE\Website\Site\index.php on line 3

Warning: include() [<a href='function.include'>function.include</a>]: Failed opening 'localhost/eee/scripts/inc/_header.php' for inclusion (include_path='.;C:\php\pear') in C:\Users\Chris\Documents\EEE\Website\Site\index.php on line 3

I read somewhere that it might be to do with the include path so i tried;

set_include_path(get_include_path() . PATH_SEPARATOR . $_SITELOC."/scripts/inc/");

but this too did not work and now i'm not sure where to go.

Thanks, Chris

localhost/eee/ is your public address that you can use in your web browser. This public address should more appropriately be written as http://localhost/eee/ . When you move to web server, you get the public address http://www.eee.lo/ .

When including files, you have to use file paths. For example, if you have your www (or httpd, whatever) directry in D:\\ on windows, then your include path should start with D:\\www\\eee\\ .

So, basically you have to use two variables to keep paths.

$_SITELOC = "http://localhost/eee/"; //For all URLs used in your HTML document.
$_INCPATH = "D:\www\eee\\";  //For all internal file includes.

In practice, you will need both of these, and it is good practice to keep the website address and internal paths out of your main script because when uploaded to remote server, not only your public address changes, but you will also have to deal with absolutely different internal (include) paths.

Your idea is basically good, to define one (root) path of the application and include files based on it, but unfortunately you're not doing it quite right. You have basically two ways of doing that.
One way (which I personally find better) is to include local files in your file system, where you can define the root path, ie like

define ('ROOT', 'your/document/root/path');
// and then include the files
include ROOT . '/' . '/scripts/inc/_header.php';

The other way would be to include a web resource, what you're trying to do, but you've forgotten to specify the scheme (protocol) you want to use, ie

define ('ROOT', 'http://localhost/eee');
// and then include the files
include ROOT . '/' . '/scripts/inc/_header.php';

For more information, see the examples, provided by the documentation for include
Note: If you want to include the source of a php file, ie file with definitions of functions, etc., use the first approach. Including files, using the second approach will only include the output produced by that file.

If you include() a URL, you will (probably) be including the output of the script's execution, when you want to include the script's source . It seems like you actually want to include by local file system path.

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