I’m making a website and I’ve moved all the contents of the HTML <head> to a header.php file, in which I link to my stylesheets, etc..
On my other pages I’m including this document. But when I include it in pages that are in a sub folder, the link to the stylesheet is no longer correct, I’ve solved it by just copying the link and adding ../ I was wondering if there is a better way to do this? like dynamically get the right path? Without using the absolute path, like I want to be able to move the project folder and still have it work.
header.php
<?php
session_start();
?>
<!DOCTYPE html>
<html lang="nb" dir="ltr">
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta charset="utf-8">
<!-- CSS -->
<link rel="stylesheet" href="css/reset.css">
<link rel="stylesheet" type="text/css" href="./css/style.css?<?php echo time(); ?>">
<link rel="stylesheet" type="text/css" href="../css/style.css?<?php echo time(); ?>">
<link rel="stylesheet" type="text/css" href="../../css/style.css?<?php echo time(); ?>">
<!-- Title -->
<title>Inventar</title>
</head>
<body>
And then i have:
<?php include "header.php"; ?>
on all my pages.
Advertisement
Answer
change header.php to
<?php
session_start();
?>
<!DOCTYPE html>
<html lang="nb" dir="ltr">
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta charset="utf-8">
<!-- CSS -->
<link rel="stylesheet" href="<?=$tempPath;?>/css/reset.css">
<link rel="stylesheet" type="text/css" href="<?=$tempPath;?>/css/style.css?<?php echo time(); ?>">
<!-- Title -->
<title>Inventar</title>
</head>
<body>
whenever you include add path define the variable
<?php $tempPath="../";//like this add path according to the file include "header.php"; ?>
Also if needed you can use dirname(__FILE__) so that you can define path from header.php