Here is my root folder :
index.php
stylesheets(folder) --->main.css
includes(folder) --->header.php
folder1 (folder) --->page1.php
Here is link part of "header.php" (from includes folder) :
<link href="stylesheets/main.css" rel="stylesheet" type="text/css" />
Here is a part of index.php :
<?php include "includes/header.php"; ?>
Here is a part of page1.php (from folder1):
<?php include "../includes/header.php"; ?>
Problem is, when I open index.php everything is normal. As for /folder1/page1.php, it doesn't see styles but see content of header.php. That is to say, it doesn't bring main.css. Most probably, because of link href="stylesheets/main.css" rel="stylesheet" type="text/css"
Any help?
Because you access files from multiple levels, you should use an absolute path.
So either href="http://domain.com/stylesheets/main.css"
or href="/stylesheets/main.css"
But this only works (the 2nd one, and the one I recommend) when you have it on top level on your domain.
A robust solution is to use absolute links. A convenient way to do this is using a central configuration file. For instance, create a file config.php
, containing
<?php
define('SITE_ROOT', 'http://www.yourdomain.com/');
?>
And include this file in each PHP file (require_once('config.php');
). Then you can write
<link href="<?php echo SITE_ROOT; ?>stylesheets/main.css" rel="stylesheet" type="text/css" />
Which will always resolve to the URL you mean to reference. When your site's domain changes, a central configuration will allow you to easily change it.
I'm confused about your folder structure, but I understood correctly.
Try
href="../stylesheets/main.css"
Sometimes the simple way ist the best way: use an absolute path!