Solved

Load css via php

Posted on 2016-07-14
18
32 Views
Last Modified: 2016-07-15
Hi,

Is it possible to load CSS via php and if so, how could you do it? So, instead of :

 <link href="css/theme.css" rel="stylesheet" type="text/css" media="all" />

Open in new window


you use php to load this css file?
0
Comment
Question by:tofat
  • 7
  • 6
  • 3
  • +1
18 Comments
 
LVL 7

Expert Comment

by:rgranlund
Comment Utility
<?php include('css/theme.css'); ?>
0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
PHP is a server-side technology.  PHP and MySQL are used to generate whole-cloth web pages consisting of HTML, JavaScript and CSS.  So the answer is "yes."  You can inject the CSS inline into the HTML document.
https://www.experts-exchange.com/articles/11271/Understanding-Client-Server-Protocols-and-Web-Applications.html
0
 
LVL 7

Expert Comment

by:rgranlund
Comment Utility
You can start there but there is a little more you should do.
0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
Sorry, I forgot to highlight the part of the article.  See: The Timeline of Request and Response
0
 
LVL 82

Expert Comment

by:Dave Baldwin
Comment Utility
You can load your CSS with PHP but what is the point?  What are you trying to accomplish?
1
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
I kinda share Dave's question here, too.  It seems like there is a backstory to questions like this, and when we can uncover the "deep down" roots of the question, there is often a simple (or simpler) design pattern that emerges.
0
 

Author Comment

by:tofat
Comment Utility
Well, I am not having much luck using defer/async plus a bunch of other techniques to load js and css files without getting the below the fold errors from google insights. Unfortunately I tend to obsess about things once I really get an idea in my head, and my idea is that I want to get 100% or as close to as possible.

Some of the methods work somewhat but don't always work across browsers. I have a few posts on here already with various other methods that I have been trying but I read someone say somewhere on all my searches that you could use php to do it (I think it was a wordpress function actually, but I am not using wordpress).

I am not sure if using php would in fact work to prevent that dreaded google speed insights message or not and if it would work across browsers without breaking my layout.
0
 
LVL 82

Accepted Solution

by:
Dave Baldwin earned 500 total points
Comment Utility
PHP runs on the server and will deliver the files in the place you have the link in the page.  It will not defer it to later loading.  For what it's worth, I never do the things you're talking about to defer loading except for putting tracking scripts at the bottom of the page.  Everything else goes where it is needed which is often in the <head> section.
0
 

Author Comment

by:tofat
Comment Utility
Yeah, everything behaves better when putting css in the head and Js before the closing body tag, but then I get that annoying above the fold error. I guess I should just stop obsessing over it and do it like that. But there is one particular website I found that has 100% and they have lots of css files and js. Am I allowed to post that page's code somewhere for you to look at? I would just like to know what it is exactly they are doing to get that. I don't know what is going on in there but since most of you here know 1000 times more than me, I am sure you would know. I will give you 500 points if you do, hehe  :)
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 82

Expert Comment

by:Dave Baldwin
Comment Utility
If you are trying to follow the 'recommendations' of Google Pagespeed, you should first go look at the 'View Source' of a Google search results page.  The largest thing in the <head> section is a couple of hundred lines of JavaScript.  And they put <style> sections throughout the body code.  Google is not following their own 'recommendations'.
0
 

Author Comment

by:tofat
Comment Utility
Where else can I do these kind of tests if Google isn't the best?

And, you didn't say if you were prepared to look at that code?
0
 
LVL 82

Expert Comment

by:Dave Baldwin
Comment Utility
Just post a link to the site and I'll look at it.  I don't want you to send me the code.

The point is that all these recommendations are coming from Google and they are not following them themselves.  You need to do what makes sense and works for you and then move on.
0
 

Author Comment

by:tofat
Comment Utility
https://developers.google.com/speed/pagespeed/insights/?url=http%3A%2F%2Fwww.websitedesignpe.co.za&tab=desktop

http://www.websitedesignpe.co.za/

If you could explain some of the techniques it would be appreciated. Perhaps you could even tell me if they are good or bad in your opinion.
0
 
LVL 82

Expert Comment

by:Dave Baldwin
Comment Utility
I'm sorry but that is almost fraudulent.  The page loads very slow the first time because it has at least 1000 lines, yes, one thousand lines of JavaScript in the <head> section.  About 80% of the page.  And that does not include the jQuery files that are being loaded.  Even when I load that page a second time, it takes forever for that top section to display properly.  It may 'test well' but I would not say that it 'works well'.
0
 

Author Comment

by:tofat
Comment Utility
Yeah, I did notice that it had LOADS of code in the page which gave me a headache. So, do you think the site gets 100% because most of the JS is inlined?

And last question (promise), what does that bit at the bottom do with all the css files? Google doesn't pick them up but I am assuming it is because they are at the bottom of the page below a 1000 odd lines of code. What does that function do? (like I said, that is my last question, I just want to know what that function at the bottom does with the css files)
0
 
LVL 82

Expert Comment

by:Dave Baldwin
Comment Utility
That site is 'optimized' for testing, not functioning.  And as part of that... the javascript at the bottom loads some links to the CSS files so they won't be seen in the <head> section.  I didn't notice at first but something like 80% of the <head> section is the CSS <style> section.  And that's before the JavaScript loads more.
0
 

Author Closing Comment

by:tofat
Comment Utility
Thanks for your patience and answering all my questions :)
0
 
LVL 82

Expert Comment

by:Dave Baldwin
Comment Utility
You're welcome, glad to help.
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now