newbie69 Posted September 3, 2007 Share Posted September 3, 2007 I have site that uses a include file for its pages. Recently i had to create a folder with different pages, but had to create the a include file with the same code but specifically for the those files that are in a directory. If i a create another directory in that directory, it means i have to create another include file specifically for files at that level. Is there a way for me to eliminate this problem. Creating the include files with the same code for different folders is going to become very unmanagable. files structure / _images /php_scripts -getscv_root_level.incl -getscv_level2.incl /newsletters /august -index.php -index.php -training_register.php please note that the include files are the sames except for the way they are point to different direcoty levels. pages in the root level would refer to getscv_root_level.incl snippet of code for getscv_root_level.incl: ---------- echo "<td class=\"content bordersolid\">".($Entry[2] == "" ? " " : "<a href=\"".$Entry[4]."\" title=\"".$Entry[2]."\" target=\"\">".$Entry[2]."</a>")."</td>"; $temp = "training_register.php?course=".$Entry[2]."&d=".$Entry['DisplayDate']; echo "<td class=\"content bordersolid\"><a href=\"".$temp."\" id=\"imglink\" title=\"\"> <img src=\"_images/home_register_button.gif\" border=\"0\"></td> </a>"; ---------------- pages in the root level would refer to -getscv_level2.incl snippet of code for getscv_level2.incl: ------------- echo "<td class=\"content bordersolid\">".($Entry[2] == "" ? " " : "<a href=\"../../".$Entry[4]."\" title=\"".$Entry[2]."\" target=\"\">".$Entry[2]."</a>")."</td>"; $temp = "training_register.php?course=".$Entry[2]."&d=".$Entry['DisplayDate']; echo "<td class=\"content bordersolid\"><a href=\"../../$temp\" id=\"imglink\" title=\"\"> <img src=\"../../_images/home_register_button.gif\" border=\"0\"></td> </a>"; ----------- I hope this is more decriptive the only difference between the 2 snippets are "../../" I ideally i would only like to point to the one file regards of where in the website (be it at root level or nested directories) the calling page is located. Thanks Quote Link to comment Share on other sites More sharing options...
jitesh Posted September 3, 2007 Share Posted September 3, 2007 In your project folder create a folder with name include. in which you place all .inc files now you can use following statement anywhere in any folder of your poject. include($_SERVER['DOCUMENT_ROOT']."project_folder/includes/file1.inc"); Quote Link to comment Share on other sites More sharing options...
newbie69 Posted September 3, 2007 Author Share Posted September 3, 2007 Thanks Jitesh, I will try this. Thanks again Quote Link to comment Share on other sites More sharing options...
newbie69 Posted September 3, 2007 Author Share Posted September 3, 2007 Hi This is the statement i used: include($_SERVER['DOCUMENT_ROOT']."php_scripts/global.inc"); this works fine for documents in root level, but if i try this for pages in that are in another folder, it doenst. Is there something i'm doing wrong? Thanks Quote Link to comment Share on other sites More sharing options...
jitesh Posted September 3, 2007 Share Posted September 3, 2007 include($_SERVER['DOCUMENT_ROOT']."php_scripts/global.inc"); This should work everywhere check like this in pages in folders. if(file_exist($_SERVER['DOCUMENT_ROOT']."php_scripts/global.inc")){ echo "File found"; }else{ echo "wrong path"; } Quote Link to comment Share on other sites More sharing options...
trq Posted September 3, 2007 Share Posted September 3, 2007 Your missing a slash. include $_SERVER['DOCUMENT_ROOT'] . "/php_scripts/global.inc"; ps: Using the inc file extension is a bad idea, as if someone stubles accross this file they may be able to view it in plain text. No good if youve got passwords and other sensitive data in it. Quote Link to comment Share on other sites More sharing options...
newbie69 Posted September 3, 2007 Author Share Posted September 3, 2007 Hi jeetesh, I have tried your piece of code and i'm get the a "wrong path" on the screen. don't know if this clarifies things site: /php_scripts/global.php /newsletters /august index.php I used include($_SERVER['DOCUMENT_ROOT']."/project_folder/includes/file1.inc"; on newsletters/august/index.php if i use include($_SERVER['DOCUMENT_ROOT']."../../project_folder/includes/file1.inc"); then it works, but this what i want to avoid. Thanks Sudhir Quote Link to comment Share on other sites More sharing options...
jitesh Posted September 3, 2007 Share Posted September 3, 2007 Generate Stucture like this C:/Program Files/Apache Group/Apache2/htdocs/MY_PROJECT/php_scripts/global.php C:/Program Files/Apache Group/Apache2/htdocs/MY_PROJECT/newsletters/august/index.php Here MY_PROJECT is current project's root directory. i mean php_scripts,newsletters are in a common directory. include($_SERVER['DOCUMENT_ROOT']."MY_PROJECT/php_scripts/global.php"); Quote Link to comment Share on other sites More sharing options...
newbie69 Posted September 4, 2007 Author Share Posted September 4, 2007 Thanks. This works on my web server, but locally it doesnt work because i thinks its running through IIS. But no worry as long as it works online. Thanks alot. Quote Link to comment Share on other sites More sharing options...
newbie69 Posted September 4, 2007 Author Share Posted September 4, 2007 would anyone know why does not this work on my pc? My php is working off the IIS on windows. When i try this on my online site it works and returns :"/home/optinumi/public_html" on my desktop PC it doesnt. <?php $test= $_SERVER['DOCUMENT_ROOT']; // this returns a blank. echo $test; ?> Quote Link to comment Share on other sites More sharing options...
jitesh Posted September 4, 2007 Share Posted September 4, 2007 Try This in local machine include("http://".$_SERVER['SERVER_NAME']."/php_scripts/general.php"); Quote Link to comment Share on other sites More sharing options...
newbie69 Posted September 4, 2007 Author Share Posted September 4, 2007 thanks will do Quote Link to comment Share on other sites More sharing options...
newbie69 Posted September 7, 2007 Author Share Posted September 7, 2007 thanks this has worked. Thanks alot, i really appreciate your help. Quote Link to comment Share on other sites More sharing options...
newbie69 Posted September 7, 2007 Author Share Posted September 7, 2007 Hi, since i want all my files,regardless of where they are in the site to use the same include file, which contains links i did the following this is what i had in the include file: echo "<a href=../../events.php">dafdasf</a>"; this is what i tried to do: Since i want the links to work across the entire site regardless of how many folders i put the file in, i tried the following and when i click on the link it doesnt work. echo "<a href=".$_SERVER['DOCUMENT_ROOT']."/events.php>events</A>"; when i echo $_SERVER['DOCUMENT_ROOT'] i get the following response C:\pws\optinum and my events.php is located at c:\pws\optinum\events.php so i dont see why my piece of code shouldnt work? does anyone know something i dont? thanks Quote Link to comment Share on other sites More sharing options...
jitesh Posted September 12, 2007 Share Posted September 12, 2007 For anchor tag you can not assign root path. Just follow server path echo "<a href="http://".$_SERVER['SERVER_NAME']."/events.php>events </ a>"; Quote Link to comment Share on other sites More sharing options...
xyn Posted September 12, 2007 Share Posted September 12, 2007 echo("<a href=\"http://".$_SERVER['SERVER_NAME']."/events.php\">events </ a>"); slash \" Quote Link to comment Share on other sites More sharing options...
newbie69 Posted September 13, 2007 Author Share Posted September 13, 2007 Thanks very much, this works! once i appreciate all the help. This site rocks! Quote Link to comment 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.