Avatar of willsherwood

asked on 

HTML base href PHP- desire to have a local scope for base href for images

HTML/<base href >/PHP question:

http://a.com/parent.php   PHP-includes   http://a.com/include.html
http://b.com/parent.php   PHP-includes   http://a.com/include.html 
http://a.com/include.html    contains relative <img> links within a.com
a.com and b.com are separate hosting accounts, separate domain names, but by design have a similar parallel directory structure

I have two separate web sites of which i'm the designer of each and I have full control.
There is a portion of one page (similar/parallel on each site) that has a PHP include that includes an html file, and the text works fine-- it displays in both contexts.

However, there is an image reference in the  html code that has a *relative* <img> link -  of course, it only displays on the web site URL where the jpeg file resides.  (temporarily  i've duplicated the jpeg files in both hosting accounts just to get the project going)

The included html file (and associated images) changes each week, and the updating/editing of that weekly change is sometimes cut and paste and sometime manually entered in a WYSIWYG script web-based  html editor.   Thus it's not as easy as just making the image reference to be an absolute link.  I do not do this weekly update (my client does this, and is not computer savvy)

I'm looking for a way to accomplish the function of base href for a given scope (i.e., the scope of just the include file)-- i could live with the scope starting at the point of the include and remaining in effect for the rest of the parent file (after the include input stream returns to the parent).
I can only find that base href is allowed in the html header and is effective for the entire page.

i would rather not do frames (since the "parent" web page is complex in itself with masthead and sidebar nav menus, etc)

Another constraint is that PHP is set on our servers to not execute code across URLs.  the  home path needs to be where it's called from.   Thus i chose the include to reference an html file type so PHP would  be able to include across domain names (hosting accounts).
Thus this rules out setting a PHP variable that is then concatenated to provide an absolute directory path. or putting code in to reference environment vars to "repair" the link.
It's not efficient/reliable to hope that however the file is edited each week it will copy the image files to the parallel site's corresponding directory location.

any thoughts on how to accomplish displaying the images within the included html file?


Avatar of undefined
Last Comment
Avatar of FSWZach

Blurred text
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of FSWZach

Or - as you said - you have access to both - then just put in something like


where the hostname goes - then its a simple - no possible problems str_ireplace -

(would do that before I woud do the first - as the first - you got the possibility of it being src="http://image" or src='http://image' or src=http://image
Avatar of FSWZach

>> Another constraint is that PHP is set on our servers to not execute code across URLs.  the  home path needs to be where it's called from.  

I also would be curious on this .... if you make a file with the extension .txt - so it doesnt parse

put it on one server - and include it on the other server - does it execute?

.... put this in testinclude.txt on one account .............


echo file_get_contents("http://www.google.com");


and then this on the other testinclude.php



Avatar of willsherwood


excellent word-arounds, i'll try these next week when time permits;  so i take it there's no official way to set the scope temporarily or for the rest of the document?

Avatar of FSWZach

I am not going to say it doesnt exist.... because I have come to learn that word "impossible" when dealing with anything with computers only means impossible for the person saying it.

... but that said - it would be rather difficult.  An HTML doc never really has a "rest" of the document.  

As you have noted - you put in a base href - and it effects what is before it - same with anything else.

Think of HTML as a building - there is things going on all around the building - unrelated to each other - and just using the HTML to maintain their position in the building.  Whatever is going on in that building - is all going on at the same time.  

PHP is a widely-used server-side scripting language especially suited for web development, powering tens of millions of sites from Facebook to personal WordPress blogs. PHP is often paired with the MySQL relational database, but includes support for most other mainstream databases. By utilizing different Server APIs, PHP can work on many different web servers as a server-side scripting language.

Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews


IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo