Vetaxili
Vetaxili

Reputation: 617

php include vs library; correct path

I have been trying to research what is better to use in order to include repetitive parts of the website - php include function or library(provided in Dreamweaver).

Or maybe there are other - better ways to achieve the same result?

At the moment I use php include and absolute paths. I downloaded the website from the server but it seems that the paths that work on the server don't work on my localhost. What would be the correct and the best way to write paths in order to make them work on both servers without having to re-write the code?

Thanks

Upvotes: 0

Views: 4065

Answers (4)

Joseph
Joseph

Reputation: 21

Put

include_path = ".:/your/absolute/path"

in your php.ini, both Server environment and localhost. Then you can access the include path file at anywhere of the codes.

Upvotes: 0

Lawrence Cherone
Lawrence Cherone

Reputation: 46620

Also an easy way to solve this problem is to define a constant from within the root, normally within your index.php file.

/**Path Environment**/
$root=pathinfo($_SERVER['SCRIPT_FILENAME']);
define ('BASE_FOLDER', basename($root['dirname']));   
define ('SITE_ROOT',    realpath(dirname(__FILE__)));
define ('SITE_URL',    'http://'.$_SERVER['HTTP_HOST'].'/'.BASE_FOLDER); 

Then you can use, SITE_URL and SITE_ROOT throughout your script knowing its the correct path to your web root.

So your old includes look like:

include('/srv/www/somesite.com/public_html/somefile.php');

and your new includes will look like include(SITE_ROOT.'/somefile.php'); then when developing on windows the path will change accordingly.

Upvotes: 0

Gustav
Gustav

Reputation: 1341

Since dream weaver uses library, that's probably wrong ;)

If you want to be sure the code is only included once you might use require_once instead.

Try relative paths instead of absolute

Upvotes: 0

periklis
periklis

Reputation: 10188

You can call at the start of your script the set_include_path() command and specify relative paths to your libraries

http://www.php.net/set_include_path

Upvotes: 1

Related Questions