How to make a "relative-path" template include-fil

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Swede78
Forum Contributor
Posts: 198
Joined: Wed Mar 12, 2003 12:52 pm
Location: IL

How to make a "relative-path" template include-fil

Post by Swede78 »

I made a couple template files (top and bottom), which I include in the beggining and end of each web page. Right now, I create a URL prefix that I stick in front of each file reference, whether it be a link or image.

Here's the code for how I do this:

Code: Select all

<?php
if( isset($_SERVER['HTTP_REFERER']) ) { $CurrentURL = $_SERVER['HTTP_REFERER']; } else { $CurrentURL = ''; }
$FindInURL = 'https';
$URLSecure = strpos($CurrentURL, $FindInURL);
if ($URLSecure === false) {
    $RelURL = 'http://www.mysite.com/'; 
} else {
    $RelURL = 'https://www.mysite.com/'; 
}
?>
This method solved my initial problem, which was that pages that were in a sub directory would not work, because the path wasn't relative anymore.

The drawback of this method is that if I have 50 references to images or links that use this $RelURL, then the file size increases significantly.


Any ideas would be appreciated!
User avatar
Stoker
Forum Regular
Posts: 782
Joined: Thu Jan 23, 2003 9:45 pm
Location: SWNY
Contact:

Post by Stoker »

hmm if you need a base filesystemdir of containing script file you can find that with __FILE__ , as for linking wether using https or not, isn't most your images and such always on the same absolute path? then you can do like
<img src="/images/blah.gif" />
will always use the current protocol from the root of the domain..
Its really no different using full url since that is absolute as well, now you just dont need to know the protocol (http/https)

another way to find out if its ssl or not can be done by port number;

Code: Select all

<?php
  if ($_SERVER['SERVER_PORT'] == 443)
  {
      # Its SSL
      $baseurl = 'https://domain.com/';
  }
  else
  {
      # Its not ssl
      $baseurl = 'http://domain.com/';
  }
?>
  <img src="<?php echo $baseurl; ?>images/blah.gif" />
keep in mind that there is no benefit over that code than just using domain root absolute adressing..
Swede78
Forum Contributor
Posts: 198
Joined: Wed Mar 12, 2003 12:52 pm
Location: IL

Post by Swede78 »

Thanks. I forgot about using just "/" as the root. I used to use Dreamweaver's template system, which would update your paths if you saved the file in a different directory. It always screwed things up when I used "/". So, I got in the habit of never using that. But, I don't use DW's template system anymore. So, problem solved.

BTW, I like your way of detecting HTTPS better. But, like you stated, I really don't need it anymore.
Post Reply