0

I want to use the full URL for the location of my css, js, and image files in my header.php file. So that when the header.php file is called from another folder directory, it doesn't break the link.

However, I want the site to be accessible by http and https, set by the user in their profile settings in the web application.

I started to write some code below of the solution but I'm not sure if this is the correct way of handling this.

config.php

<?php
// use https
$use_https = true;
?>

header.php

<?php
if ($use_https == true) {
   $proto = "https://";
} else {
   $proto = "http://";
}
?>

  <a href="<?php print($proto . $_SERVER["SERVER_NAME"]); ?>/some-folder/">Link</a>
MarkH
  • 85
  • 1
  • 9
  • Just use protocol-less links. `//someurl/path` The browser will base it on what the root site was loaded as – Machavity Mar 16 '17 at 00:04
  • Do protocol-less links work for all modern browsers? Like IE9 and newer for example? – MarkH Mar 16 '17 at 00:34

1 Answers1

0

The easiest way is to just do:

<a href="//<?php echo $_SERVER["SERVER_NAME"]); ?>/some-folder/">Link</a>

Or since it's on your own server, just:

<a href="/some-folder/">Link</a>

Make sure to include the initial slash, so that it is relative to the root of your site, and not to the current page (this will prevent the link from breaking).

That being said, if your site works with https, you are probably better off just always using https, since you don't really have performance concerns anymore.

dave
  • 62,300
  • 5
  • 72
  • 93
  • Does the initial slash in your second example work with require() function in PHP? I seem to be getting a 500 error from it. – MarkH Mar 16 '17 at 00:33