Page 1 of 1

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

Posted: Wed Aug 27, 2003 5:29 pm
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!

Posted: Wed Aug 27, 2003 7:09 pm
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..

Posted: Sun Aug 31, 2003 2:45 pm
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.