ColeGauthier
ColeGauthier

Reputation: 87

PHP Include & CSS

In my website, I'd like to use the PHP include line within the index.php page to add in important sections like the header and footer.

Now, the folder structure is as follow:

/index.php

/css/style.css

/includes/header.php

/includes/footer.php

Keep in mind I am working in a localhost environment.

Now my question to you is how do I properly reference my CSS file in the index.php and header.php? Should I use a config.php file? I'd like to avoid using absolute paths if that's possible.

Currently, my CSS file is read and displays properly, however, when I make changes, the CSS does not display the changes made and keeps the original unchanged file.

index.php

<head>
    <link rel="stylesheet" href="css/style.css" type="text/css">
</head>

<body>
  <?php include 'includes/header.php';?>
  <div class="container">
    <div class="inner-container">
      <?php include 'includes/javascript.php';?>
    </div>

header.php

<link rel="stylesheet" href="css/style.css" type="text/css">
</head>
<body>
  <header>
      <div id="logo">
        <div class="logo-container">
          <img src="img/final_logo_400px.png" alt="">
        </div>
      </div>
      <nav>
        <ul>

Upvotes: 1

Views: 1979

Answers (1)

mayersdesign
mayersdesign

Reputation: 5310

Simply use a relative link no? ie:

<link href="/css/style.css" rel="stylesheet" type="text/css" />

I presonally do the following which checks whether the file is local or remote, and versions the file using its modification time if it is remote (that check fails locally):

 $url = $_SERVER['DOCUMENT_ROOT'];
 $parts = explode('/',$url);
 $base = $parts[0];
 if ($base == 'C:') { ?>
   <link href="/css/style.css" rel="stylesheet" type="text/css" />
 <?php } else { ?>
   <link href="/css/style.css?v=<?php echo filemtime($basepath."/css/style.css")?>" rel="stylesheet" type="text/css" />
 <?php } ?>

Upvotes: 2

Related Questions