June 17, 201313 yr I am wanting to make my life easier when using php include tags to include a php script or html from another source. My problem is, on a localhost mamp.. all is great and it works. Upload it to a server and I get errors. Can someone help me understand what I need to do to get this to work on my hosting server? <?php include ($_SERVER['DOCUMENT_ROOT'] . '/assets/navbar.php'); ?> gives me the error of: Warning: include() [function.include]: Failed opening '/home/'domain'/public_html/assets/subbar.php' for inclusion (include_path='.:/usr/lib/php:/usr/local/lib/php') in /home/'domain'/public_html/families_relief/index.php on line 34 The actual folder root the website is it is: domain.com/families_relief Mainly because it is a tester. I am looking for some sort of way to work it so that the include will always grab the right code and knows where to look. I was looking at something like a 'baseURL'. But anyone offer me advice, I will happily take it! Thanks
June 17, 201313 yr One way is to have a config file (config.inc.php) that is included by all scripts that will define all your helper functions and site constants. In that file define your base url and base uri constants define ('BASE_URI', '/home/example/'); //remote path to root folder define ('BASE_URL', 'http://www.example.com/'); //live url //define ('BASE_URI', '/Applications/MAMP/htdocs/example/'); //local path to root folder //define ('BASE_URL', 'localhost/example/');//local url Then your include can be something like include(BASE_URI . 'assets/subbar.php'); Be sure to comment / uncomment out the 2 lines depending on whether you're working locally or remotely
Create an account or sign in to comment