homepage Welcome to WebmasterWorld Guest from 54.167.173.250
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
How to to reference a CSS file from a “header.php” include file
CSS_Kidd




msg:4309987
 5:43 pm on May 9, 2011 (gmt 0)

What is the correct way to reference a CSS file from a “header.php” include file? I have a few subdirectories within my site that I would (of course) like to use the same CSS file with, via a header include. However I am unsure of what the general standard is when referencing the css header link in my “inc/header.php” file. Searching around, I haven’t found one agreeable solution. Here is a general example of my site directory:

wwwRoot/
wwwRoot/subDirectoy1
wwwRoot/subDirectoy1/index.php // uses include(../inc/header.php)
wwwRoot/subDirectoy2
wwwRoot/subDirectoy3
wwwRoot/css
wwwRoot/css/style.css // stylesheet
wwwRoot/inc
wwwRoot/inc/header.php // header include that references style.css for whole site

So far I have seen some thing as simple as:
link href="http://wwwRoot/css/style.css" rel="stylesheet" type="text/css"

TO:
link href="http://<?php echo $_SERVER['HTTP_HOST'] . '/css/style.css'; ?>" rel="stylesheet" type="text/css"

And Even seen somewhere where they said to change the include path in the php.ini file, which isn’t an option, because I will not always have access to do this.

So what is the Common Practice for something like this?

 

g1smd




msg:4310006
 6:39 pm on May 9, 2011 (gmt 0)

The CSS file is requested by the browser as a URL.

The URL is a reference system used outside on the web. Therefore in the link to the CSS file use a URL that begins with a slash and counts, in URL terms, from the root of the site, e.g. href="/css/style.css".

Do not use a relative URL, otherwise each page of the site will be looking for a CSS file at a place that is relative to that page... and which will therefore fail to find the CSS file.

You could add the protocol and domain name to the CSS link but this will cause extra work if you have a mix of HTTP and HTTPS pages.

rocknbil




msg:4310388
 4:24 pm on May 10, 2011 (gmt 0)

In addition this gets really important if you are using mod_rewrite for friendly URL's. If you rewrite some request

h t t p://www.example.com/my-widget-category/

to a script in a directory

/products/script.php

The request still appears to come from example.com/my-widget-category/, even when you're in /products/ - the leading slash always fixes this. It's the same for any CSS, JS, Image, or other objects too, always reference with a leading slash.

The down side is, it doesn't work locally unless you set up a virtual server of some sort. Not a big deal, but you (generally) have to mod all paths for offline work, update them before deployment.

g1smd




msg:4310392
 4:41 pm on May 10, 2011 (gmt 0)

With XAMPP installed and running in just a few minutes, everyone should be testing changes to their sites on /localhost/ or /127.0.0.1/.

bjassoc




msg:4311520
 7:24 pm on May 12, 2011 (gmt 0)

after many years of doing websites, I didn't know that starting a link with / goes back to the root. Exciting! except that I just tried it with php files that I'm using (for first time - converting website built with FrontPage).

the file I'm testing is [findajp.com...]

which has this Code:
<?php require("/includes/navhoriz.php"); ?>

not working - it isn't going back to the root for the includes folder, instead looking for the includes folder in the findajp.com/jpnh/ directory.

thanks for your help.

g1smd




msg:4311534
 8:11 pm on May 12, 2011 (gmt 0)

You need to be aware of another thing; "includes" are an internal request within the server, so you need the full internal server path.

To be clear, "includes" do NOT use a "URL". URLs are used only outside the server, out there on the web. The URL path begins at the site root not at the internal file system root.

bjassoc




msg:4311558
 9:09 pm on May 12, 2011 (gmt 0)

I didn't know that.
I changed the call to
<?php require("/home/bjassoc/public_html/includes/navhoriz.php"); ?>
which I think is the full path.

I dont understand the error msg: "No such file or directory in /home/bjassoc/public_html/jpnh/rockingham.php on line 102."

It's still looking in the folder that's doing the call?
thanks again.

rocknbil




msg:4311998
 5:32 pm on May 13, 2011 (gmt 0)

Try this instead.

<?php require($_SERVER['DOCUMENT_ROOT'] . "/includes/navhoriz.php"); ?>

Or you could get it from phpinfo() and hard code it, but hard coding it is a lot less flexible.

<?php
php_info();
?>

<aside>(W.O.W., please do not leave phpinfo.php on your server. Use it, then delete it. It reveals far too much info about your server configuration and is one of the files that hackers look for.)</aside>

bjassoc




msg:4312182
 11:17 pm on May 13, 2011 (gmt 0)

the call you suggested worked - thanks so much. Don't understand how to do the php_info. I think I was wrong to put my path out there (earlier post) but dont see a way to delete it. too late to edit.

rocknbil




msg:4313157
 4:16 pm on May 16, 2011 (gmt 0)

That bit of code I posted is a php_info() in entirety, put it in a file and upload to your server. I meant, don't leave **that** on your server - obviously the path you posted is incorrect anyway or it would have worked. :-)

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved