chrisg7504 Posted December 26, 2009 Share Posted December 26, 2009 Hi, I am a total newbie at PHP and am trying my best to understand some things. I have a file called header.php located in includes/header.php I also have a file called h_shop.php located in cart/h/h_shop.php The problem I am having is how to automatically reference the header include using php instead of a numerous amounts of slashes. A config file would help as I have a number of pages in the folder cart/h/ This is what I started with but doesn't work mainly because I am testing on a local server and when I upload it I have to change all the files references again: <?php require("../../../include/header.php"); ?> Any help or understanding of this will help greatly. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/186378-linking-or-server-reference-help/ Share on other sites More sharing options...
PFMaBiSmAd Posted December 26, 2009 Share Posted December 26, 2009 Use absolute file paths - require($_SERVER['DOCUMENT_ROOT'] . "/include/header.php"); Quote Link to comment https://forums.phpfreaks.com/topic/186378-linking-or-server-reference-help/#findComment-984230 Share on other sites More sharing options...
BloodyMind Posted December 26, 2009 Share Posted December 26, 2009 I would recommend using constants as base directories and its always better to use absolute paths than relative paths and its faster It's also better to include files from an index page, its much easier to include since u don't have to calculate where are u now in the application so if you did: index.php <?php require('include/header.php'); require('cart/h/h_shop.php'); ?> and manage it with get variables for instance you want the index.php to load the h_shop script you can call do this: <?php require('include/header.php'); if($_GET['go'] == 'shop'){ require('cart/h/h_shop.php'); } ?> and call it index.php?go=shop you will need to use a switch statement if you got many pages hope that helped Quote Link to comment https://forums.phpfreaks.com/topic/186378-linking-or-server-reference-help/#findComment-984234 Share on other sites More sharing options...
chrisg7504 Posted December 26, 2009 Author Share Posted December 26, 2009 Using the method you described with the use of switch statements is a great idea and would be much easier to manage, but I always thought they were not search engine friendly? Quote Link to comment https://forums.phpfreaks.com/topic/186378-linking-or-server-reference-help/#findComment-984237 Share on other sites More sharing options...
chrisg7504 Posted December 26, 2009 Author Share Posted December 26, 2009 Hi, I am a total newbie at PHP and am trying my best to understand some things. I have a file called header.php located in includes/header.php I also have a file called h_shop.php located in cart/h/h_shop.php The problem I am having is how to automatically reference the header include using php instead of a numerous amounts of slashes. A config file would help as I have a number of pages in the folder cart/h/ This is what I started with but doesn't work mainly because I am testing on a local server and when I upload it I have to change all the files references again: <?php require("../../../include/header.php"); ?> Any help or understanding of this will help greatly. Thanks Hi thanks for the quick reply. Your method is what I used first off. But my issue is that on my local server the header.php file is located in www/hosting/include/header.php and the remote file location is public_html/include/header.php which kinda of throws me off a little. Quote Link to comment https://forums.phpfreaks.com/topic/186378-linking-or-server-reference-help/#findComment-984238 Share on other sites More sharing options...
BloodyMind Posted December 26, 2009 Share Posted December 26, 2009 It can be search engine friendly by using apache mod_rewrite but this is another topic, you'll have to search for some tutorials .. you can do this in the very beginning in your script but I prefer doing it in a separate configuration file <?php // server base directory define('BASE_DIR','public_html/'); // local base directory define('BASE_DIR','www/hosting/'); require(BASE_DIR . 'include/header.php') ?> comment any of the defined statements u don't use e.g. on the server comment the define statement of the local directory Quote Link to comment https://forums.phpfreaks.com/topic/186378-linking-or-server-reference-help/#findComment-984240 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.