msaz87 Posted July 2, 2009 Share Posted July 2, 2009 I'm new to basing sites in PHP and a prevailing problem I've run into is dealing with relative/absolute paths. I'll try to explain it as best I can and if anyone can give me some advice or let me know if I'm just setting things up poorly... I'd greatly appreciate it. Suppose on all my pages I include a header.php -- which is located in an "includes" folder. So it would look like: <? include('includes/header.php'); ?> But if I have a page not located on the root, that path will be incorrect. So if this second page is in a different directory, one off the root, I adjust the include to: <? include('../includes/header.php'); ?> But now the problem is that the CSS, JS and other files being linked to within that header document are also off. One solution would be to use absolute paths, but for the sake of developing in an environment, I'd rather not do that. Is there an easy way to deal with this that I'm just unaware of? Thanks so much! Quote Link to comment https://forums.phpfreaks.com/topic/164490-newbie-question-dealing-with-paths/ Share on other sites More sharing options...
dreamwest Posted July 2, 2009 Share Posted July 2, 2009 / means root so make your css and javascript a base /style.css /js/java.js etc.. Quote Link to comment https://forums.phpfreaks.com/topic/164490-newbie-question-dealing-with-paths/#findComment-867674 Share on other sites More sharing options...
msaz87 Posted July 2, 2009 Author Share Posted July 2, 2009 I used what you said and kind of figured out a little more, but am still having issues configuring it to work well when I develop it in XAMPP. I now have a checker in my config file to see what environment it's in: if ( $_SERVER['SERVER_NAME'] == "localhost" ) { $root = "/Applications/XAMPP/......../"; } else { $root = "http://www..../"; } Then for the header: <link rel="stylesheet" href="<? echo $root; ?>css/style.css" type="text/css" /> This works well for the version actually on the web... but I can't seem to get it to work with XAMPP. Any ideas? Thanks for the help! Quote Link to comment https://forums.phpfreaks.com/topic/164490-newbie-question-dealing-with-paths/#findComment-867701 Share on other sites More sharing options...
Adam Posted July 2, 2009 Share Posted July 2, 2009 You'd just use http://localhost/path/to/files Quote Link to comment https://forums.phpfreaks.com/topic/164490-newbie-question-dealing-with-paths/#findComment-867704 Share on other sites More sharing options...
msaz87 Posted July 2, 2009 Author Share Posted July 2, 2009 Yeah I just figured that out... I guess I was just making things way too difficult for myself. Thanks for your help! Quote Link to comment https://forums.phpfreaks.com/topic/164490-newbie-question-dealing-with-paths/#findComment-867705 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.