tmallen Posted October 3, 2007 Share Posted October 3, 2007 I'm a freelance web designer and I've begun to use PHP extensively for my clients' websites. A problem I run into is the consistent use of "/" for the site root. My server thinks that that refers to public_html/, meaning I can't test a site with "/" used to recognize the site root if I'm, say, one directory deep. How can I have a specific website recognize its own base folder as the site root? I imagine that in PHP, without touching Apache config files, there's a way to set a new site root, I just don't know how. :( Quote Link to comment https://forums.phpfreaks.com/topic/71742-site-root/ Share on other sites More sharing options...
trq Posted October 3, 2007 Share Posted October 3, 2007 Take a look at $_SERVER['DOCUMENT_ROOT']. Quote Link to comment https://forums.phpfreaks.com/topic/71742-site-root/#findComment-361286 Share on other sites More sharing options...
tmallen Posted October 3, 2007 Author Share Posted October 3, 2007 Thanks. I took a look. Didn't help though...where should I set this variable? I couldn't even find an example, just code-reference style entries. Quote Link to comment https://forums.phpfreaks.com/topic/71742-site-root/#findComment-361289 Share on other sites More sharing options...
trq Posted October 3, 2007 Share Posted October 3, 2007 It is set by your server and already points to the doc root. Quote Link to comment https://forums.phpfreaks.com/topic/71742-site-root/#findComment-361292 Share on other sites More sharing options...
tmallen Posted October 3, 2007 Author Share Posted October 3, 2007 Yes, it does. But imagine: (1) I have my portfolio website. This is the only one where a URL like "/images/logo.png" would work, because I store this site in the public_html folder, which my server has set as the root. (2) I have many websites for clients. When I deploy on their server, "/images/logo.png" will work fine for their site, but right now I have it located on my server at "/clientA/images/logo.png". However, for the Header include, I'd like to have <img src="/images/logo.png" />. If I do that, it will point to my website's logo, and not theirs. (3) I'd like their site root to be located not at public_html/, but at public_html/clientA/ so that I can easily deploy/migrate. Quote Link to comment https://forums.phpfreaks.com/topic/71742-site-root/#findComment-361293 Share on other sites More sharing options...
trq Posted October 4, 2007 Share Posted October 4, 2007 A site within a site is just that. They share the same document root because they are actually the same site. Ive always used a seperate vhost when developing for clients. One thing you could probably do would be to create your own $root var. eg; $root = $_SERVER['DOCUMENT_ROOT'] . '/clientA'; However then you'll need to include this variable anywhere you need to use it. Also, it will not effect pure html such as <img src='/images/logo.png'>. You would need to use <img src='<?php echo $root ; ?>/images/logo.png'> Quote Link to comment https://forums.phpfreaks.com/topic/71742-site-root/#findComment-361298 Share on other sites More sharing options...
tmallen Posted October 4, 2007 Author Share Posted October 4, 2007 Which is really no better than adding "/clientA" to every absolute root URL and using a site-wide Replace to wipe it out later. I wish there were another way, and it defies reason that PHP doesn't provide a way to set a non-global site root for a specific script or set of scripts. Quote Link to comment https://forums.phpfreaks.com/topic/71742-site-root/#findComment-361319 Share on other sites More sharing options...
trq Posted October 4, 2007 Share Posted October 4, 2007 It has little to do with php and more to do with your server. If you want to have a different site root for each client, create a seperate vhost for it. Quote Link to comment https://forums.phpfreaks.com/topic/71742-site-root/#findComment-361322 Share on other sites More sharing options...
tmallen Posted October 4, 2007 Author Share Posted October 4, 2007 It has everything to do with PHP. The "/" indicating the site root is completely up to PHP. PHP could define "/" to mean anything it wants, and a programmer should be able to control where "/" points...that's the reason for my skepticism. Quote Link to comment https://forums.phpfreaks.com/topic/71742-site-root/#findComment-361352 Share on other sites More sharing options...
trq Posted October 4, 2007 Share Posted October 4, 2007 The "/" indicating the site root is completely up to PHP. Are you talking about this? <img src="/images/logo.png" /> / is not the document root defined by php, that is your server (and that code is not php). If you use php to attempt to include a file like so... include '/foo/bar/'; php will search for bar in the root of your filesystem, not your web doc root. Quote Link to comment https://forums.phpfreaks.com/topic/71742-site-root/#findComment-361429 Share on other sites More sharing options...
corbin Posted October 4, 2007 Share Posted October 4, 2007 Which is really no better than adding "/clientA" to every absolute root URL and using a site-wide Replace to wipe it out later. I wish there were another way, and it defies reason that PHP doesn't provide a way to set a non-global site root for a specific script or set of scripts. A non global site root? By that do you mean set a variable to something like '/clientA/' because if that's what you mean, that's what thorpe suggested.... I get a little crazy with the use of vhosts.... I have around 15 (most of them are just entries in my hosts file ;p). Quote Link to comment https://forums.phpfreaks.com/topic/71742-site-root/#findComment-361442 Share on other sites More sharing options...
SammyGunnz Posted October 4, 2007 Share Posted October 4, 2007 Relative paths....as long as the file structure is the same (A logo will always be in folder 'images/logo.gif'), will work regardless of where the test/client folder is. I don't see what all the confusion is about. Just use relative paths and you won't have any problems. Quote Link to comment https://forums.phpfreaks.com/topic/71742-site-root/#findComment-361453 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.