jeff5656 Posted July 29, 2010 Share Posted July 29, 2010 Because I have include files in a subdirectory called "incl", I cannot rely on using relative paths because that include file is in a header on and the pages are in multiple subdirectories (or in the root). I need some way to get the location of the root (the one where my index file is located). I want to use something like this: $path= $_SERVER['DOCUMENT_ROOT']; include $path."/subdirectory/file.php"; However, when I use $_SERVER['DOCUMENT_ROOT'] I get : /home/content/j/p/f/name55/html/ which is not where the index file sits. Any ideas on how to get the directory where my index file sits? (i.e. the index that gets read when you got to www.domain.com) Quote Link to comment https://forums.phpfreaks.com/topic/209244-how-do-i-point-to-an-absolute-root/ Share on other sites More sharing options...
AbraCadaver Posted July 29, 2010 Share Posted July 29, 2010 If your index.php is loaded first then all paths will be relative to the index.php file. Quote Link to comment https://forums.phpfreaks.com/topic/209244-how-do-i-point-to-an-absolute-root/#findComment-1092651 Share on other sites More sharing options...
jeff5656 Posted July 29, 2010 Author Share Posted July 29, 2010 If your index.php is loaded first then all paths will be relative to the index.php file. What do you mean by loaded? Sometimes they will go to a page located in a different subdirectory (or even land there if they bookmarked that). In that case, the included file will no longer be pointing to the root directory. Of those out there that have include files in a separate subdirectory, how do you handle those files pointing to the correct location? example: in the index.php you have: include "links.php"; and in links.php you have: <a href="/subdir/file.php">Click me</a> So in index.php this will work, but lets say I am in a subdirectory off the root called ad and I have this file example.php include "links.php";</php] This won't work becasue it will try to do: <a href="ad/subdir/file.php">Click me</a> Quote Link to comment https://forums.phpfreaks.com/topic/209244-how-do-i-point-to-an-absolute-root/#findComment-1092689 Share on other sites More sharing options...
radar Posted July 29, 2010 Share Posted July 29, 2010 thats where the document_root comes... lets suppose you have a file structure like this [root] =>index.php [subdir] =>file.php [ad] =>index.php now in root you would just call include('subdir/file.php'); but in the ad index.php you would include($_SERVER['document_root']."/subdir/file.php"); or include('../subdir/file.php'); though the document_root method is more viable. Quote Link to comment https://forums.phpfreaks.com/topic/209244-how-do-i-point-to-an-absolute-root/#findComment-1092702 Share on other sites More sharing options...
jeff5656 Posted July 29, 2010 Author Share Posted July 29, 2010 Well the problem is one of the included files in is a form with action ="logout.php" So if I am in the index when I press logout it works, but if I am in a subdirectory it won't work. How do I fix that? In DOS (remember that OS?) I could just type action = c:\ad\logout.php andit would work no matter where I was. How do I do the same thing here? Quote Link to comment https://forums.phpfreaks.com/topic/209244-how-do-i-point-to-an-absolute-root/#findComment-1092766 Share on other sites More sharing options...
wildteen88 Posted July 29, 2010 Share Posted July 29, 2010 Well the problem is one of the included files in is a form with action ="logout.php" Add a forward slash before logout.php, eg <form action="/logout.php"> Quote Link to comment https://forums.phpfreaks.com/topic/209244-how-do-i-point-to-an-absolute-root/#findComment-1092816 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.