I'm using AltoRouter to route my urls to the correct files. Now basically here, I describe my problem already.
On one page, I have an alert included, styled by bootstrap.
It's defined as simple as that:
$('#wrongPasswordDiv').html('<br/><div class="alert alert-danger" id="wrongPWAlert" role="alert">Falsches Passwort. Bitte erneut versuchen!</div>');
Also, before, Bootstrap css file is included:
<link rel="stylesheet" type="text/css" href="/bootstrapcss" />
bootstrapcss is routed to the correct css file using AltoRouter and this line of code:
$router->map('GET','/bootstrapcss','vendor/twbs/bootstrap/dist/css/bootstrap.css','bootstrapcss');
Now, in console, it throws a warning saying Resource interpreted as Stylesheet but transferred with MIME type text/html: "http://localhost/bootstrapcss".
If I use the complete path to the css file, a CDN or remove the DOCTYPE, its working fine. But I don't wanna do either of those variations... Removing the doctype, might damage other functions and if I would use the complete css path, then I wouldn't need the routing...
Any ideas how I could send the Content-type: text/css header, in order to get it working?
You should send the proper Content-Type before you send the response content. I don't know much PHP so I may not read the CSS in the best way, but this is a working example:
Using this route:
$router->map('GET','/bootstrapcss','example.css','bootstrapcss');
And then while matching:
$match = $router->match();
if($match['name'] === 'bootstrapcss'){
header("Content-Type: text/css");
$fileName = $match['target'];
echo file_get_contents($fileName);
return;
}
For context, I have a full example here: https://gist.github.com/kobi/09eaeeecb3406b193a84a674218798a9
This is based on the basic example on AltoRouter: https://github.com/dannyvankooten/AltoRouter/tree/master/examples/basic
Related
I'm recently doing a website for a school project. In order to organize my work, I create a tree folder that keeps all the work organized. It is similar like this:
Back-Office
Pages
Home
home_test1.php
home_test2.php
home_test3.php
Login
Folder_Login
login.php
logout.php
Resources
CSS
style_home.css
style_navbar.css
style_footer.css
JS
script_home.css
script_navbar.css
Sections
navbar.php
footer.php
After all, with the require() method available in PHP, I want to call the "navbar.php" file to the "home_test1.php", "home_test2.php" and "home_test3.php", but the CSS style that is connected with the file "navbar.php" ("style_navbar.php"), doesn't display.
I've tried to change the path of the CSS style in the file "navbar.php" when I require() to the other file ("home_test1.php") and the CSS style shows up, but wont display in other file with a different path. How can I make this work dynamically? Sorry for long post and bad English grammar.
Thank you in advance.
You need to set your css and js files with absolute path instead of relative path
$dir = realpath($_SERVER["DOCUMENT_ROOT"]);
<link rel="stylesheet" href="<?php echo $dir.'/resources/css/style_home.css'; ?>" >
Without physically seeing you code it is quite hard to debug however there is an "obvious" answer that I'll suggest as a starting point.
The important thing to remember is that PHP and HTML are processed in completely different places. PHP executes on the server and should be used to build a full HTML "document" which it gives to the client/browser. The client/browser then reads the document provided and renders it according to HTML standards.
Calling require() will tell PHP to get the file and slot its contents directly where it was called and as it is a CSS file it will need to sit within the style tags. With a lot of modern browsers, if you use require on a file outside of the html tags, the content will be dumped at the top of the screen or simply ignored due to invalid syntax.
Alternatively if you would like to simply use tell the browser to include the CSS file, you could use the good old method of using <link rel="stylesheet" href="/path/to/file">. It's good to know when and when not to use PHP.
PS: You have .css files in your JS directory.
In PHP, there is a global variable containing various details related to the server. It's called $_SERVER. It contains also the root:-
$_SERVER['DOCUMENT_ROOT']
<?php
$path = $_SERVER['DOCUMENT_ROOT'];
<link rel="stylesheet" href="<?php echo $path.= '/Resources/CSS/style_navbar.css';?>" />
?>
I am writing my own MVC implementation and I am facing a problem including local CSS files. Basically all requests in my applications are going to a method which is decomposing them in order to get a controller and action. For example:
localhost:8888/items/list
this request will be redirected to my index.php where I have:
Application::start($_SERVER['REQUEST_URI']);
Then the request is decomposed to get the controller 'items' and action 'list'
So far all good and I didn't realise the problem as I was using bootstrap from CDN. However now if I include in my html something like:
<link rel="stylesheet" type="text/css" href="/css/style.css">
This will go to my method again and it will not work.
What I tried is to check if the URI contains 'css' and if it does just include the file with:
if(preg_match('/css/',$uri)){
include "../webroot/$uri";}
This worked in terms of loading the css but the css is ignored. Also I get
"Resource interpreted as Stylesheet but transferred with MIME type text/html: "http://localhost:8888/css/style.css"."
The problem is even bigger if I try to include images although with images at least I am able to display them but again through 'include'. Any ideas how to fix this ?
P.S I know that the problem is most likely because the css is loaded as text file, however I don't know how to get this working with my current setup
Try header("Content-type: text/css; charset: UTF-8"); with file_get_contents instead of include like this
if(preg_match('/css/',$uri)){
header("Content-type: text/css; charset: UTF-8");
if(!$file = file_get_contents("http://localhost:8080/project_name/webroot/$uri")) return 0;
echo $file;
}
I have a stylesheet link that looks like below:
<link rel="stylesheet" href="/example/get_page.php?location=bla.css" id="main_ss" />
get_page.php just gets a URL using file_get_contents():
if (isset($_GET['location'])) {
echo file_get_contents('/example/styles/' . $_GET['location']);
}
I can see that the stylesheet file is being fetched properly (for example the text of the file is showing in firebug when I expend the link tag) but for some reason it is ignored by the browser. If I just fetch the CSS file directly of course everything works.
The code can be seen here: www.specman-verification.com/example/bla.html
Any leads? I'm at loss here.
Add the Content-type header like this (do this before you output anything):
header("Content-type: text/css");
Your code is just trying to load the script get_page.php. To load the CSS file you need:
<link rel="stylesheet" type="text/css" href="/example/bla.css" />
(or similar depending on the actual path to your CSS file). In other words the href attribute needs to specify the path to your spreadsheet file, not the HTML page file.
You need to do it the right way. I understand what you're doing here. You need a good mechanism to dynamically load external CSS and have the result display normal html in the browser output.
Follow the instructions on this url: http://www.warpconduit.net/2009/05/12/dynamically-load-css-and-js-files-using-php/
This will at least get you to have a mechanism to load external css file with php dynamically. You're definitely missing steps in your code.
I am using php to generate a css file, declaring a header like:
<?php
header("Content-type: text/css; charset: UTF-8");
?>
and passing the url from index.php like:
<link href="http://www.site.com/css.php?style_profile=2" media="screen" rel="stylesheet" type="text/css" />
I can't get this file to validate from the w3c css validator because it doesn't recognize the file type. This is the error I keep getting:
Unknown error org.w3c.www.http.HttpInvalidValueException: Invalid content type.
Is there any way to get this to validate without manually pasting the css data? If not, is there any other validation service that will recognize a css.php file (or one with get vars attached, if that is the issue causing the error)? I'd really like to be able to validate by link and not have to cut and paste the css for every single page on my site.
I don't know if this will resolve your problem (unable to test right now), but it is something that I noticed:
According to the W3 spec, your PHP header is incorrect, and it should read charset=utf-8.
ih ave read about the technique to allow dynamic HTML stylesheets using PHP's preprocessor, however I was windering how safe this tecnique is, and if for any reason it is discouraged.
For example, instead of using typical:
<link rel="stylesheet" type="text/css" href="http:mysite.com/style.css/>
I could use:
<link rel="stylesheet" type="text/css" href="http:mysite.com/style.php/>
What do you think?
It's perfectly fine.
I'd suggest setting the following header in the PHP though:
Header("Content-Type: text/css");
If you can use .htaccess files you can set it up to parse *.css files as PHP using:
AddHandler application/x-httpd-php .css
Then you can use PHP directly within your *.css files.
Be sure to also set the header type to text/css as the others have mentioned as well:
header('Content-type: text/css');
It's only as safe as the code that you put into style.php.
Yes, you can use PHP to generate your stylesheet. Make sure to declare the output properly as CSS by sending an appropriate Content-Type value specifying both the media type and the character encoding.
But note that it costs additional time and resources to generate the stylesheet with every request. So you should add some kind of caching mechanism (static files and HTTP caching) to reduce server load and even unnecessary requests.
As others have mentioned, PHP can be used to output any kind of text. So it's not a problem to output dynamic CSS (or even dynamic JavaScript). Be aware though that you're increasing your server load by doing this. The server will have to fire up the PHP engine to serve what would otherwise be a simple static .css file.
You can render any kind of Text Output with PHP (and other stuff) including CSS, just make sure the right header is given header('Content-Type: text/css; charset=iso-8859-1'); (or any other charset).
But to be honest I can't think of an Situation were it would be necessary to use a dynamic stylesheet in that manner.