Help with a PHP Include code |
Help with a PHP Include code |
nWo Sting |
Dec 12 2006, 12:09 AM
Post
#1
|
Member Group: Members Posts: 63 Joined: 5-November 06 Member No.: 709 |
My server does no support SSI, but they do support PHP. I was trying to use a simple include code for my side navigation bar, but I cannot seem to get it to work. I am using the following code........
<?php require($DOCUMENT_ROOT . "path to file/include-nav.html"); ?> I named my Navigation page, which is basically just my side navigation buttons, nav.html Is there some extra code I have to put on my nav.html page for the php to work? My web hosting company says php is activated already, but perhaps I have to do something extra. This may sound like a simple question but I am new to php and using includes. Can anyone help? |
Brian Chandler |
Dec 12 2006, 04:36 AM
Post
#2
|
Jocular coder Group: Members Posts: 2,460 Joined: 31-August 06 Member No.: 43 |
My server does no support SSI, but they do support PHP. I was trying to use a simple include code for my side navigation bar, but I cannot seem to get it to work. I am using the following code........ <?php require($DOCUMENT_ROOT . "path to file/include-nav.html"); ?> I named my Navigation page, which is basically just my side navigation buttons, nav.html Is there some extra code I have to put on my nav.html page for the php to work? My web hosting company says php is activated already, but perhaps I have to do something extra. This may sound like a simple question but I am new to php and using includes. Can anyone help? I can't remember the difference between require() and include(), but it's not much. All you need to do is write include(<<whateverthefilenameis>>); For <<whateverthefilenameis>> just write 'nav.html' if the navigation code is in the same directory. The example you've been given is trying to be helpful, because typically you should keep include files somewhere else - actually it's best to keep them *outside* the webserver document tree, because you don't want to serve them directly to users. But supposing your web document root is: /usr/www/users/horigome And supposing you keep the include files in a directory include below this root (and set the server not to allow http access to this directory), then the full path name of your file is: /usr/www/users/horigome/include/nav.html But in case you've forgotten (or whatever) the document root, then you can use the system variable to supply it: hence in this case you would refer to the file as /usr/www/users/horigome/include/nav.html $DOCUMENT_ROOT . 'include/nav.html' However, it is better (security etc) not to enable the option for bare system variables like $DOCUMENT_ROOT - and it's likely your system has done this. In which case refer to it with $_SERVER['DOCUMENT_ROOT'] OK? (Just did _my_ homework: require() is "stricter" than include() - if anything goes wrong using require() the page crashes to a halt. I recommend include(), because if something goes wrong the included bit will just be missing. Or there'll be one of those entertaining error messages for your visitors.) So the answer is: include($_SERVER['DOCUMENT_ROOT'] . '/include/nav.html'); Incidentally, I think it's a good idea to use a different file extension (such as .inc) for included files, since they are not (complete) html pages. HTH |
Christian J |
Dec 12 2006, 06:16 AM
Post
#3
|
. Group: WDG Moderators Posts: 9,661 Joined: 10-August 06 Member No.: 7 |
Do you use .php extensions on the web pages containing the inclusion directive?
If you don't want that extension it can be changed to e.g. .html, but the web hosts' default is often to ignore PHP in .html web pages. |
nWo Sting |
Dec 12 2006, 11:49 PM
Post
#4
|
Member Group: Members Posts: 63 Joined: 5-November 06 Member No.: 709 |
Do you use .php extensions on the web pages containing the inclusion directive? If you don't want that extension it can be changed to e.g. .html, but the web hosts' default is often to ignore PHP in .html web pages. no, that was one of my mistakes, I will name the page I am practicing with .php, thanks. |
nWo Sting |
Dec 12 2006, 11:50 PM
Post
#5
|
Member Group: Members Posts: 63 Joined: 5-November 06 Member No.: 709 |
My server does no support SSI, but they do support PHP. I was trying to use a simple include code for my side navigation bar, but I cannot seem to get it to work. I am using the following code........ <?php require($DOCUMENT_ROOT . "path to file/include-nav.html"); ?> I named my Navigation page, which is basically just my side navigation buttons, nav.html Is there some extra code I have to put on my nav.html page for the php to work? My web hosting company says php is activated already, but perhaps I have to do something extra. This may sound like a simple question but I am new to php and using includes. Can anyone help? I can't remember the difference between require() and include(), but it's not much. All you need to do is write include(<<whateverthefilenameis>>); For <<whateverthefilenameis>> just write 'nav.html' if the navigation code is in the same directory. The example you've been given is trying to be helpful, because typically you should keep include files somewhere else - actually it's best to keep them *outside* the webserver document tree, because you don't want to serve them directly to users. But supposing your web document root is: /usr/www/users/horigome And supposing you keep the include files in a directory include below this root (and set the server not to allow http access to this directory), then the full path name of your file is: /usr/www/users/horigome/include/nav.html But in case you've forgotten (or whatever) the document root, then you can use the system variable to supply it: hence in this case you would refer to the file as /usr/www/users/horigome/include/nav.html $DOCUMENT_ROOT . 'include/nav.html' However, it is better (security etc) not to enable the option for bare system variables like $DOCUMENT_ROOT - and it's likely your system has done this. In which case refer to it with $_SERVER['DOCUMENT_ROOT'] OK? (Just did _my_ homework: require() is "stricter" than include() - if anything goes wrong using require() the page crashes to a halt. I recommend include(), because if something goes wrong the included bit will just be missing. Or there'll be one of those entertaining error messages for your visitors.) So the answer is: include($_SERVER['DOCUMENT_ROOT'] . '/include/nav.html'); Incidentally, I think it's a good idea to use a different file extension (such as .inc) for included files, since they are not (complete) html pages. HTH thanks for all of the info, I will give this a shot, I am only used to Front Page, and I dont think I can make .php in Front Page, so I have to teach myself DreamWeaver I guess. |
Lo-Fi Version | Time is now: 27th April 2024 - 07:46 AM |