Is this possible, or valid, in HTML and PHP?
Can I put this in my HTML <head>
?
<link rel="stylesheet" href="http://site.com/some/php/script/userid/style.php" />
Basically, can I pass a user ID via a URI segment (or a GET variable) to a PH开发者_如何学运维P script - which will still be validly treated as a stylesheet?
So, for example, if in my webapp a user has a custom CSS stylesheet for their page, I can just load it up dynamically in my controller by outputting this as the stylesheet URL - is this possible? I know the PHP part is certainly possible, but will this still be valid or fully browser/server compatible?
Thanks!
I am assuming you're asking "will this work?" and not "is this valid HTML?" for that, See @Gordon's answer.
If your PHP script outputs a text/css
content type and valid CSS, this will work fine.
header("Content-type: text/css");
This header is necessary for FF to interpret the CSS. See here why.
It will be desirable to add some caching headers as well, otherwise the resource will probably be requested on every page load.
However...
If you need to output dynamic CSS, consider having a static style sheet (i.e. one that does not get interpreted by PHP) with all the generic stuff, and adding dynamic data in the document's head:
<link rel="stylesheet" href="http://site.com/resources/style.css" />
<style type="text/css">
.userText { color: <?php echo $userTextColor; `> }
etc. etc.
</style>
this way, the CSS resource can remain a static file. It will not require starting a PHP process to get served. If your CSS parameters change from page to page, this will save you a HTTP request.
Yes, this is valid HTML. For the validity of the HTML document, the only thing that's important is that the href attribute points to a URI.
<!ELEMENT link EMPTY>
<!ATTLIST link
%attrs;
charset %Charset; #IMPLIED
href %URI; #IMPLIED
hreflang %LanguageCode; #IMPLIED
type %ContentType; #IMPLIED
rel %LinkTypes; #IMPLIED
rev %LinkTypes; #IMPLIED
media %MediaDesc; #IMPLIED
>
You can check the validity of your document with http://validator.w3.org/check
But …
The script that is located at that URI has to serve any content with the correct header so the browser can actually interpret it as CSS. This is a completely different thing than HTML validity though. So for serving a CSS file, you have to set the text/css header.
Note that using this approach is generally discouraged though unless you implement some sort of caching mechanism. When you point to a PHP file, the webserver will execute the PHP file so you lose any caching mechanism you'd usually have for static resources.
As long as you output proper CSS it fine. Your browser doesn't care how the stylesheet is generated or how the URI looks like. It just has to be a stylesheet.
as long as you have correct headers set, there should not be any problems with that.
精彩评论