Julio Abdilla
Julio Abdilla

Reputation: 259

dynamic external link css in php

i'm trying to create a website using php, and i using external css file.

i have head.php file

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

this is my index.php file

$server = $_SERVER['DOCUMENT_ROOT'];
include $server.'/head.php';

it's works fine, but when i try to create other index.php file in child directory like child/index.php, it said css file not found. i tried to change head.php to

<link rel="stylesheet" type="text/css" href="<? echo $server ?>/style/style.css" />

and it's doesn't worked too.

how do i resolve this problem?

Upvotes: 2

Views: 1923

Answers (3)

Alex W
Alex W

Reputation: 38253

Your stylesheet is using a relative path. You can fix the problem by changing it to reflect that it is in a subdirectory:

../style/style.css

where the .. causes it to go "up" one directory from where the HTML is being interpreted.

The other option is using an absolute path, e.g. http://www.example.com/style/style.css

Upvotes: 0

xdazz
xdazz

Reputation: 160933

If the url of the css file is http(s?)://www.example.com/style/style.css

Then juse use:

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

Upvotes: 3

Aaron Wojnowski
Aaron Wojnowski

Reputation: 6480

Since PHP is server side, using the document root will refer to the root of your web server. However, you are trying to load the CSS file on the client side. Therefore, your href attribute value is relative to the current domain (/ isn't referring to root on your server, just the root of the domain).

Upvotes: 1

Related Questions