10

How do I set a base URL for my website and get it to include in every page?

Is there a way for me to easily change a variable to be the base url for the website, such as <?php $baseurl = "http://www.website.com/website/"; ?>, and include this on every page so that all CSS, JavaScript, images and PHP includes follow this $baseurl?

user2183116
  • 113
  • 1
  • 1
  • 4
  • Output a `` element https://developer.mozilla.org/en/docs/HTML/Element/base – Alex K. Mar 18 '13 at 16:16
  • You may think this is a good idea, but it probably is not. First off, unless you want to use PHP to dynamically generate your CSS, you would have to manually change the domain information there. Second, you will then have to litter your display code with a bunch of ` echo $baseurl; ?>` for every `href` or `src` on your site. Why not just use URI's, as they will give you the same exact result (they will all point the resources based on the domain currently used to access the site.) – Mike Brant Mar 18 '13 at 16:20
  • 1
    Setup an include file called `base.php` for example and put this in every PHP file `include("base.php");` with what `castis` and others wrote, and/or anything else you wish. – Funk Forty Niner Mar 18 '13 at 16:20
  • @Fred I just tried this, and it works for everything but the other PHP includes on the page. – user2183116 Mar 18 '13 at 16:29
  • @user2183116 Look at Martin's example and base yourself on that. `Pardon the pun`. – Funk Forty Niner Mar 18 '13 at 16:31

3 Answers3

19

You can’t make both PHP and client-side assets use the same base URL, unless you use PHP to echo a base URL variable or constant to the page.

The usual approach is to have a bootstrap file that you include on every page, and define your base URL and other site-wide variables in there.

bootstrap.php:

<?php
    define('BASE_URL', 'http://example.com');

index.php:

<?php
    include('bootstrap.php');
?>
<!DOCTYPE html>
<html>
  <head>
    <!-- // -->
    <link rel="stylesheet" href="<?php echo BASE_URL; ?>/css/styles.css" />
  </head>
  <body>
    <!-- // -->
  </body>
</html>
Martin Bean
  • 38,379
  • 25
  • 128
  • 201
11

You may want to take a look at the html base tag.

Inside the <head> section of your html, put

<base href="http://www.website.com/website/">

On top of that, you may want to have a base.php with default directories and whatnot that you include into your project.

castis
  • 8,154
  • 4
  • 41
  • 63
-3

(xampp), the How do I use on the local computer. folder layout,

http://www.resimagaci.com/img/90rvnrf.png

ust.php

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title></title>
<meta name="description" content="">
<meta name="viewport" content="width=device-width">
</head>
<body>

alt.php

</body>
</html>

sabitler.php

<?php
#sabitler
define('BASE_URL', 'base-url');
?>

index.php

<?php
include 'kutuphane/sabitler.php';
?>
<?php
$ust= BASE_URL . '/kutuphane/ust.php';
$alt= BASE_URL . '/kutuphane/alt.php';
?>
<?php
include ($ust);
?>
<?php
include ($alt);
?>