completeamateur Posted April 1, 2006 Share Posted April 1, 2006 Hello, I am new to php and I have a problem that I am sure php can solve, but don't know how.Firstly, I suppose it may help if I tell everyone what I have got:Windows XPApache 2.0.55 win32 (for testing offline)PHP 4.4.2 win32IE/firefoxWhat I am trying to do is have an include file (which is the menu for the whole site) which I can then just 'load' into all individual files, making changes easier. The problem I have is defining the location of the include file (as it is located in a different directory).The source files are stored on a local server in:C:\Program Files\Apache Group\Apache2\htdocsand can be viewed by opening (in a browser):[a href=\"http://localhost/\" target=\"_blank\"]http://localhost/[/a]I had thought of$root = "http://localhost/"; //can be changed to address of websitethen constructing the location of the include file:include ($root . "include/menu.php")...but this doesn't seem to work on my local server. I get the "Apache HTTP Server has encountered a problem and needs to close. We are sorry for the inconvenience." pop-up box. I haven't tried uploading it.I suppose what I really need is to be able to access the root directory or something? I don't really know what I'm talking about so if anyone can make any sense of this, that would be great! Below is the location & content of the files to try and give a better explanation.[a href=\"http://localhost/index.php\" target=\"_blank\"]http://localhost/index.php[/a][blockquote]<?php$root = "http://localhost/";echo("<html><body>This is some text.");include ($root . "include/menu.php");echo("This is some more text. <a href='" . $root . "subdir/anotherindex.php'>Click here</a>.</body></html>");?>[/blockquote][a href=\"http://localhost/subdir/anotherindex.php\" target=\"_blank\"]http://localhost/subdir/anotherindex.php[/a][blockquote]<?php$root = "http://localhost/";echo("<html><body>This is some text.");include ($root . "include/menu.php");echo("This is some more text. <a href='" . $root . "index.php'>Back to main index</a>.</body></html>");?>[/blockquote][a href=\"http://localhost/include/menu.php\" target=\"_blank\"]http://localhost/include/menu.php[/a][blockquote]<?phpecho("<hr>This is the menu<hr>");?>[/blockquote] Quote Link to comment Share on other sites More sharing options...
play_ Posted April 1, 2006 Share Posted April 1, 2006 Try this:$root = "http://localhost/";$menu = "include/menu.php";$file = $root.menu;include ($file);and see if it still gives you an error Quote Link to comment Share on other sites More sharing options...
completeamateur Posted April 1, 2006 Author Share Posted April 1, 2006 [!--quoteo(post=360716:date=Apr 1 2006, 11:12 PM:name=play_)--][div class=\'quotetop\']QUOTE(play_ @ Apr 1 2006, 11:12 PM) [snapback]360716[/snapback][/div][div class=\'quotemain\'][!--quotec--]Try this:$root = "http://localhost/";$menu = "include/menu.php";$file = $root.menu;include ($file);and see if it still gives you an error[/quote]No joy play, but thanks for the quick reply. Do you think it is a problem with my set up but might work if I upload it to my webspace? Quote Link to comment Share on other sites More sharing options...
ToonMariner Posted April 1, 2006 Share Posted April 1, 2006 Alternatively you could use this...[code]<?phpinclude($_SERVER['DOC_ROOT'] . "/includes/menu.php");?>[/code]Should be able to use that on each and every page with no problem.Bear in mind that apache shutting down could be as a result of a different error in your scripts... even if it only occure when you implemented this. Quote Link to comment Share on other sites More sharing options...
sford999 Posted April 1, 2006 Share Posted April 1, 2006 If the file you need to include is in a different directory, then you can just use:[code]include ("../inc/file.php");[/code] Quote Link to comment Share on other sites More sharing options...
play_ Posted April 1, 2006 Share Posted April 1, 2006 [!--quoteo(post=360725:date=Apr 1 2006, 05:25 PM:name=sford999)--][div class=\'quotetop\']QUOTE(sford999 @ Apr 1 2006, 05:25 PM) [snapback]360725[/snapback][/div][div class=\'quotemain\'][!--quotec--]If the file you need to include is in a different directory, then you can just use:[code]include ("../inc/file.php");[/code][/quote]No no, the file is in the same directory always, but he calls the file from different directories.[!--quoteo(post=360723:date=Apr 1 2006, 05:22 PM:name=ToonMariner)--][div class=\'quotetop\']QUOTE(ToonMariner @ Apr 1 2006, 05:22 PM) [snapback]360723[/snapback][/div][div class=\'quotemain\'][!--quotec--]Alternatively you could use this...[code]<?phpinclude($_SERVER['DOC_ROOT'] . "/includes/menu.php");?>[/code]Should be able to use that on each and every page with no problem.Bear in mind that apache shutting down could be as a result of a different error in your scripts... even if it only occure when you implemented this.[/quote]Yea..maybe the intallation went wrong somewhere?SO yea, completeamateur, try uploading it to your web host.Also, replace 'include' with 'require' Quote Link to comment Share on other sites More sharing options...
ToonMariner Posted April 1, 2006 Share Posted April 1, 2006 That will only work if the call is from a file in a directory of teh same level as the inc directory.if inc is in teh root dir then a call to "../inc/menu.php" from root/dir1/dirx/diry/file.php would NOt call the correct file (the only time infact, that no error would be returned in that situation is if this existed.. root/dir1/dirx/inc/menu.php)MAKE SURE YOUR PATH IS CORRECT AT ALL TIMES (which is why $_SERVER['DOC_ROOT'] is so very useful) Quote Link to comment Share on other sites More sharing options...
play_ Posted April 1, 2006 Share Posted April 1, 2006 [!--quoteo(post=360727:date=Apr 1 2006, 05:33 PM:name=ToonMariner)--][div class=\'quotetop\']QUOTE(ToonMariner @ Apr 1 2006, 05:33 PM) [snapback]360727[/snapback][/div][div class=\'quotemain\'][!--quotec--]That will only work if the call is from a file in a directory of teh same level as the inc directory.if inc is in teh root dir then a call to "../inc/menu.php" from root/dir1/dirx/diry/file.php would NOt call the correct file (the only time infact, that no error would be returned in that situation is if this existed.. root/dir1/dirx/inc/menu.php)MAKE SURE YOUR PATH IS CORRECT AT ALL TIMES (which is why $_SERVER['DOC_ROOT'] is so very useful)[/quote]If inc was in the root dir, wouldn't he use "./inc/menu.php" ?I think you put an extra '.' in there ;) Quote Link to comment Share on other sites More sharing options...
completeamateur Posted April 1, 2006 Author Share Posted April 1, 2006 [!--quoteo(post=360723:date=Apr 1 2006, 11:22 PM:name=ToonMariner)--][div class=\'quotetop\']QUOTE(ToonMariner @ Apr 1 2006, 11:22 PM) [snapback]360723[/snapback][/div][div class=\'quotemain\'][!--quotec--]Alternatively you could use this...[code]<?phpinclude($_SERVER['DOC_ROOT'] . "/includes/menu.php");?>[/code]Should be able to use that on each and every page with no problem.Bear in mind that apache shutting down could be as a result of a different error in your scripts... even if it only occure when you implemented this.[/quote]Toon, this works for the index.php file in the main directory but not for the anotherindex.php file in the "subdir" directory. Is this because it calls the root of where the file is? I suppose the root I require will always be the root to the main directory? Is there a function for this... or am I just talking garbage! Quote Link to comment Share on other sites More sharing options...
ToonMariner Posted April 1, 2006 Share Posted April 1, 2006 Sorry mate its my short hand...$_SERVER['DOCUMENT_ROOT']get that used to talking bout it!!!!sorry my bad (embarrassed smily ;)) Quote Link to comment Share on other sites More sharing options...
completeamateur Posted April 1, 2006 Author Share Posted April 1, 2006 DOCUMENT_ROOT seems to return:C:/Program Files/Apache Group/Apache2/htdocon my local server, rather than[a href=\"http://localhost/\" target=\"_blank\"]http://localhost/[/a]which is what I think I require, so unfortunately it doesn't work. Suppose it will if I upload it but would be nice to get it working in both. Is this something to do with the setup of apache?Thanks. Quote Link to comment Share on other sites More sharing options...
completeamateur Posted April 1, 2006 Author Share Posted April 1, 2006 Got it working. It seems this doesn't work:[blockquote]<?phpecho("<html><body>This is some text.<p>");echo("DOCUMENT_ROOT " . $_SERVER['DOCUMENT_ROOT']);include($_SERVER['DOCUMENT_ROOT'] . "/include/menu.php");echo("This is some more text. <a href='" . [b]$_SERVER['DOCUMENT_ROOT'][/b] . "/subdir/anotherindex.php'>Click here</a>.</body></html>");?>[/blockquote]But this does:[blockquote]<?php[b]$root = ("http://localhost");[/b]echo("<html><body>This is some text.<p>");echo("DOCUMENT_ROOT " . $_SERVER['DOCUMENT_ROOT']);include($_SERVER['DOCUMENT_ROOT'] . "/include/menu.php");echo("This is some more text. <a href='" . [b]$root[/b] . "/subdir/anotherindex.php'>Click here</a>.</body></html>");?>[/blockquote]I guess it all comes with being a complete amateur! I will post back when I get a chance to upload it to my webspace, but fingers crossed it is all sorted now.Big thank you to everyone who has helped, particularly the tyneside massive! Quote Link to comment Share on other sites More sharing options...
ToonMariner Posted April 1, 2006 Share Posted April 1, 2006 it will return C:\\blah blah on your machine but that is fine!did it include the menu file correctly? You should have no probs using it at ll just makes sure that all the paths are correct. Quote Link to comment Share on other sites More sharing options...
completeamateur Posted April 2, 2006 Author Share Posted April 2, 2006 Tested it all on my webspace and local server and it seems to all work swimmingly, I just need to design the rest of the site now!!!No doubt i'll be back on here before the end of the day! Thanks for your help. 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.