The IWDN

Advanced Search





You are not logged in.

IWDN Forum Index > Web Development > Programming >> php includes and base href

Reply
 
Thread Tools
Old October 12th, 2007, 00:23   #1
grandpafred
Aspiring Guru
 
Join Date: Dec 2006
Location: Wyoming, USA
Posts: 455
php includes and base href

I've only used the base href concept with ASP pages and never encountered a problem... I'm trying to help a friend of mine using PHP and can't quite figure out what's going on.
Scenario:
Most pages of the site are in the root: www.domain.com/page.php
Included items like footer/main menu etc are in:
www.domain.com/includes/mainmenu.php and since the menu items are in the root mainmenu.php is written with links like this:
a href="page.php"

He now wants to create some nested directories like this:
www.domain.com/sub/sub/page.php and use base href on that page to handle the mainmenu which I thought would work based on my having done that with ASP. However, this is not the case.... I get nasty php errors.

I set the base href to http://www.domain.com and the page has no problem with using the style sheet which is accessed by
href="/includes/page.css
BUT has a problem with the mainmenu which is trying to be accessed like this:
<?php include("/includes/mainmenu.php") ?>

I've tried putting a trailing slash / in the base href and removing the leading slash everywhere else... I've tried leaving the base href alone and adding a preceeding slash to the mainmenu links but can't get any combination to work.

I would suppose I could always suggest putting the absolute path in the mainmenu.php file but I am curious as to why the base href idea isn't working.

TIA
grandpafred is offline   Reply With Quote
Old October 12th, 2007, 01:03   #2
kirxklox
Web-Tinker
 
Join Date: Nov 2004
Location: Dardanelle, Arkansas
Posts: 1,208
Have you checked for a Permissions Problem?
__________________
kirxklox (Sam Kirk)
Contact by form mail

It is not a job, but an excursion through life!
kirxklox is offline   Reply With Quote
Old October 12th, 2007, 01:11   #3
inimino
IWDN Core Team
 
Join Date: Oct 2004
Location: Colorado, US
Posts: 1,174
Send a message via ICQ to inimino Send a message via AIM to inimino
You must be careful about the distinction between filesystem paths and the path component of a URI.

Quote:
Quoting grandpafred: View Post
Included items like footer/main menu etc are in:
www.domain.com/includes/mainmenu.php
I'm not sure this is quite right. www.domain.com/includes/mainmenu.php suggests you are thinking of this as having a URL, but this is just a file on the filesystem, included by PHP. It might be accessible at that URL, or it might not; there's no reason for it to be accessible by HTTP at all, and steps are often taken to prevent direct HTTP access to files that are intended only to be included.

Quote:
and since the menu items are in the root mainmenu.php is written with links like this:
a href="page.php"

He now wants to create some nested directories like this:
www.domain.com/sub/sub/page.php and use base href on that page to handle the mainmenu which I thought would work based on my having done that with ASP. However, this is not the case.... I get nasty php errors.
There's no need for <base>, just rewrite those links as <a href="/page.php">.

Quote:
I set the base href to http://www.domain.com and the page has no problem with using the style sheet which is accessed by
href="/includes/page.css"
That will work with or without <base> because the leading slash specifies that this is an absolute path.

Quote:
BUT has a problem with the mainmenu which is trying to be accessed like this:
<?php include("/includes/mainmenu.php") ?>
That's because that's a filename, not a URI path component, and it's handled by PHP on the server side, not by the browser, so <base> has no effect. The filesystem knows nothing of the structure of your URI space. Unless you actually have a file at /includes/mainmenu.php on your filesystem that won't work.

Quote:
I've tried putting a trailing slash / in the base href and removing the leading slash everywhere else... I've tried leaving the base href alone and adding a preceeding slash to the mainmenu links but can't get any combination to work.
Don't use <base>. The purpose of <base> is to deal with relative URIs in documents that are not served over HTTP. As long as you are serving your document over HTTP, your relative links will already be evaluated in the context of the URI of your page, and <base> buys you nothing.

Your PHP include() statements are something else altogether, you need to make sure the path is the correct filesystem path relative to the current directory on the filesystem. So you probably need to use something like:
Code:
include("../includes/mainmenu.php");
Quote:
I would suppose I could always suggest putting the absolute path in the mainmenu.php file but I am curious as to why the base href idea isn't working.
You should do that, the only way <base> can prevent you from needing to use absolute paths there would be if you put a URI in <base> that is different from the URI of the page, which would be incorrect.
inimino is offline   Reply With Quote
Old October 12th, 2007, 04:35   #4
kirxklox
Web-Tinker
 
Join Date: Nov 2004
Location: Dardanelle, Arkansas
Posts: 1,208
It depends upon how he has .htaccess set and his File permissions.
__________________
kirxklox (Sam Kirk)
Contact by form mail

It is not a job, but an excursion through life!
kirxklox is offline   Reply With Quote
Reply


Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump