Ricky55 Posted January 10, 2010 Share Posted January 10, 2010 Hi Just starting out with PHP. Working on a site that uses some basic includes, these are working fine for all my files in the root of my site as the links from the header include is just linking straight to pages for example contactUs.php I am now however having to create some new folders to hold some product ranges, these are two folders down from the root of the site. I can link to the includes fine but the links don't work, to work they would need ../../ etc Now I could create a new header include file especially for use by the files in the deeper folders or I could make the links absolute but I just wondered if there was another way? I wondered if you could use PHP to perhaps detect the folder level and then add the necessary path??? I don't know am I way off the mark? Any help / tips much appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/187951-basic-question-regarding-php-includes/ Share on other sites More sharing options...
Catfish Posted January 10, 2010 Share Posted January 10, 2010 Yeah you could probably make PHP workout how many directory levels in you are and prepend a string of ../ onto a include's filename. Check out this user's example from the php manual's user comments under function include(): http://www.php.net/manual/en/function.include.php#76092 What a pain! I have struggled with including files from various subdirectories. My server doesn't support an easy way to get to the root HTML directory so this is what I came up with: <?php $times = substr_count($_SERVER['PHP_SELF'],"/"); $rootaccess = ""; $i = 1; while ($i < $times) { $rootaccess .= "../"; $i++; } include ($rootaccess."foo/bar.php"); ?> This will give you what it takes to get to the root directory, regardless of how many subdirectories you have traveled through. Quote Link to comment https://forums.phpfreaks.com/topic/187951-basic-question-regarding-php-includes/#findComment-992339 Share on other sites More sharing options...
corbin Posted January 10, 2010 Share Posted January 10, 2010 I would make links absolute personally. Quote Link to comment https://forums.phpfreaks.com/topic/187951-basic-question-regarding-php-includes/#findComment-992347 Share on other sites More sharing options...
teamatomic Posted January 10, 2010 Share Posted January 10, 2010 My prefered method is to make a config file <?php $path='/home/path/public_html'; $path_includes = "$path/includes"; $url = 'http://MyDomain.com'; $url_images = "$url/images"; ?> Then include the config file into all your pages. I usually hard code it with something like this if (!file_exists("c:/home/singlechef/includes/config.php")) {include('/home/singlechef/htdocs/includes/config.php');} else {include('c:/home/singlechef/includes/config.php');} I do it this way so I can build and test on my development computer at home then upload to the server without having to alter code for the config file(s). But you can just use an include with an absolute path if you wish. include('/home/user/public_html/includes/config.php'); The you just use the path/url vars and you have absolute values and never have problems no matter where the file are or are called from. HTH Teamatomic Quote Link to comment https://forums.phpfreaks.com/topic/187951-basic-question-regarding-php-includes/#findComment-992393 Share on other sites More sharing options...
Ricky55 Posted January 10, 2010 Author Share Posted January 10, 2010 Thanks for all your replies. I think I probably would just use absolute paths but they make local testing a bit of a pain and I'm also uploading the site to a different server while I build the site for the client to approve all the links would be broken until its on the correct server. I'll check them out and let you know how I get on. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/187951-basic-question-regarding-php-includes/#findComment-992448 Share on other sites More sharing options...
oni-kun Posted January 11, 2010 Share Posted January 11, 2010 Thanks for all your replies. I think I probably would just use absolute paths but they make local testing a bit of a pain and I'm also uploading the site to a different server while I build the site for the client to approve all the links would be broken until its on the correct server. I'll check them out and let you know how I get on. Thanks Just remember, if you're using absolute paths (ie: http: //yoursite.com/includes/a.php) you will lose all included functions and variables, as it will pull the results (from an http request) rather than actually including as intended. You can simply list the document path first, in place of an absolute path (which is more preferred) like so: $docroot = getenv("DOCUMENT_ROOT"); include($docroot.'includes/a.php'); which will pull something such as /usr/var/name/ht_docs/includes/a.php. Quote Link to comment https://forums.phpfreaks.com/topic/187951-basic-question-regarding-php-includes/#findComment-992492 Share on other sites More sharing options...
trq Posted January 11, 2010 Share Posted January 11, 2010 [ot] Just remember, if you're using absolute paths (ie: http: //yoursite.com/includes/a.php) Just a note here, these are urls, not absolute paths. [/ot] Quote Link to comment https://forums.phpfreaks.com/topic/187951-basic-question-regarding-php-includes/#findComment-992495 Share on other sites More sharing options...
PFMaBiSmAd Posted January 11, 2010 Share Posted January 11, 2010 The issue in this thread are the links (URLs) in the included file. You can use $_SERVER['HTTP_HOST'] when forming the absolute URLs to get the host name so that you don't need to change anything when you move from your development system and a live server. Quote Link to comment https://forums.phpfreaks.com/topic/187951-basic-question-regarding-php-includes/#findComment-992497 Share on other sites More sharing options...
Ricky55 Posted January 11, 2010 Author Share Posted January 11, 2010 Pleased you said that I was getting a bit confused there myself. Yes just to recap its not linking to the includes, I can do that no bother at all as each page can just link to the include however it needs to. Its the links inside the include file that I'm referring to. They are all currently looking for root based files for example contact.php So when I use this include in a file at the root level all is well but when I use the file several folders in the links obviously no longer work. For them to work they'd need to have ../../ in front of them but I can do this as this will break the links for the files that us the include at the root level. So my use of absolute paths or URLS would be on the links inside the include. I know some of these answers where looking at this problem. Thanks for everyone who has responded its good to be on such a lively forum. So on balance then what is the preferred method of getting around this problem? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/187951-basic-question-regarding-php-includes/#findComment-992647 Share on other sites More sharing options...
Ricky55 Posted January 11, 2010 Author Share Posted January 11, 2010 PFMaBiSmAd, just read your post back again, that sounds ideal so I can use absolute urls but not have to change them. How would I use this then? in my link? Sorry my knowledge of PHP is very small at the moment. Quote Link to comment https://forums.phpfreaks.com/topic/187951-basic-question-regarding-php-includes/#findComment-992649 Share on other sites More sharing options...
oni-kun Posted January 11, 2010 Share Posted January 11, 2010 PFMaBiSmAd, just read your post back again, that sounds ideal so I can use absolute urls but not have to change them. How would I use this then? in my link? Sorry my knowledge of PHP is very small at the moment. I was going to mention this but forgot. Anyway, Like this: $host = $_SERVER['HTTP_HOST']; echo "http://$host/folder/test.html"; //http://127.0.0.1/folder/test.html or http://site.com/folder/test.html That will display the correct host, in development or live environment. For your reference I'd look at this: http://www.php.net/manual/en/reserved.variables.server.php It will list all the $_SERVER variables which may become handy to you over time. Quote Link to comment https://forums.phpfreaks.com/topic/187951-basic-question-regarding-php-includes/#findComment-992653 Share on other sites More sharing options...
Ricky55 Posted January 11, 2010 Author Share Posted January 11, 2010 Thanks very much. I understand that code but would that be used on every link in my include file? For example currently my links look like links... <a href="">contactUs.php</a> Could I declare $host = $_SERVER['HTTP_HOST']; that once for instance and then use the echo on my links? Quote Link to comment https://forums.phpfreaks.com/topic/187951-basic-question-regarding-php-includes/#findComment-992656 Share on other sites More sharing options...
Ricky55 Posted January 11, 2010 Author Share Posted January 11, 2010 Sorry my links don't look like that at all they look like this <a href="contactUs.php">contact Us</a> Quote Link to comment https://forums.phpfreaks.com/topic/187951-basic-question-regarding-php-includes/#findComment-992657 Share on other sites More sharing options...
oni-kun Posted January 11, 2010 Share Posted January 11, 2010 Sorry my links don't look like that at all they look like this <a href="contactUs.php">contact Us</a> That is what you'd call a relative URI. You would need to do nothing, as it simple includes it from the path. Such as './contactUs.php' , it doesn't need a host name. Quote Link to comment https://forums.phpfreaks.com/topic/187951-basic-question-regarding-php-includes/#findComment-992660 Share on other sites More sharing options...
Ricky55 Posted January 11, 2010 Author Share Posted January 11, 2010 Sorry I'm getting a bit confused, if I use that relative link though they won't work when I use the include file in files that two folders down from the root. So i do need to turn my links into absolute paths but this will break on my test server as its not the final host so how would I use your example in this? <a href="htttp://www.mysite/includes/contact.php">contact.php</a> Sorry if I being a bit thick! Quote Link to comment https://forums.phpfreaks.com/topic/187951-basic-question-regarding-php-includes/#findComment-992662 Share on other sites More sharing options...
trq Posted January 11, 2010 Share Posted January 11, 2010 So i do need to turn my links into absolute paths but this will break on my test server as its not the final host so how would I use your example in this? Use $_SERVER['HTTP_HOST'] instead of hardcoding your host (domain) name. Quote Link to comment https://forums.phpfreaks.com/topic/187951-basic-question-regarding-php-includes/#findComment-992699 Share on other sites More sharing options...
Ricky55 Posted January 11, 2010 Author Share Posted January 11, 2010 Cheers Thorpe!! Quote Link to comment https://forums.phpfreaks.com/topic/187951-basic-question-regarding-php-includes/#findComment-992716 Share on other sites More sharing options...
Ricky55 Posted January 11, 2010 Author Share Posted January 11, 2010 Sorry to be a pain Thorpe. My link looks like this <a href="http://www.choicecreative.co.uk/welcome/index.php">Home</a> How would it look if I replaced the domain with that code, I tried a few options and it didn't seem to work. Also, do I still need the initial part of $host = $_SERVER['HTTP_HOST']; And if so where does that need to be located as the include file although saved with the PHP extension is just HTML code. Quote Link to comment https://forums.phpfreaks.com/topic/187951-basic-question-regarding-php-includes/#findComment-992791 Share on other sites More sharing options...
Ricky55 Posted January 11, 2010 Author Share Posted January 11, 2010 Sorry to push this up again, I still can't get this working, I've tried $_SERVER[HTTP_HOST] instead of hard coding my domain name in my links but this still remains in the address bar of the browser so its not getting parsed as PHP. Besides changing the links inside my include file what else do I need to do? <a href="http://$_SERVER[HTTP_HOST]/index.php">Home</a> Also my include file is saved with the extension of .PHP but inside there are no PHP tags its just regular HTML is this correct or should I be using the <?PHP tags? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/187951-basic-question-regarding-php-includes/#findComment-993072 Share on other sites More sharing options...
trq Posted January 11, 2010 Share Posted January 11, 2010 Are you within <?php ?> tags? <a href="http://<?php echo $_SERVER['HTTP_HOST']; ?>/index.php">Home</a> Quote Link to comment https://forums.phpfreaks.com/topic/187951-basic-question-regarding-php-includes/#findComment-993157 Share on other sites More sharing options...
Ricky55 Posted January 11, 2010 Author Share Posted January 11, 2010 whoops, sorry man, I didn't know I could just add PHP tags in that manner, Sorry, but thanks so much. I should have known that really. Quote Link to comment https://forums.phpfreaks.com/topic/187951-basic-question-regarding-php-includes/#findComment-993185 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.