get back PHP variable from php CSS file - php

My problem is that I cannot access a PHP variable from a CSS file loaded like this from index.php:
<link href="css/style.php" rel="stylesheet">
In the style.php file, I have this:
<?php header("Content-type: text/css; charset: UTF-8");
$myClassName = 'myClass'; ?>
.<?= $myClassName?> {
font-weight: bold;
}
in my index.php I have this:
<span class='<?= $myClassName?>'>this is a text</span>
But $myClassName return an empty string like it doesn exists...it does mean that I cannot access the PHP variable like this....is there someone have maybe a trick..?
I really need to set the css classnames with PHP variables from the css file and be able to get them back to my index.php

Under the header, do $css = $_GET['css']; or replace it with wherever you are initializing the variable from.For example:
<?php
header('Content-Type: text/css');
$css = $_GET['css'];
?>
body {
<?= $css ?>border-radius: 3px
}

Related

using php in css ,substr_count() seach file location

i have some question about php in css
my projet folde
What i'm trying to do : count <li> with php in css
Probleme : cannot find the right location
more information : <link rel="stylesheet" href="./css/style.php"> in my "head.php"
in my style.php
<?php
header("Content-type: text/css; charset: UTF-8");
$menu="../index.php";
$li=substr_count($menu,"<li>");
echo $menu;
?>
i tried "../index.php"; "../inc/menu.php"; "./inc/menu.php";
no one work
you can try something like this
$menu = file_get_contents('../index.php');
$liCount = substr_count($menu,'<li>');
echo $liCount;

WKHTMLTOPDF (knplabs - snappy) not showing header nor footer page

I'm using wkhtmltopdf to generate my pdf files. I've left the generation alone for some time and for whatever reason it's not generating the header and footer anymore.
Things I've tried so far (will update this when more answers come in):
Having the doctype, html, head and body tags ion the header and footer
Chaning the paths to the header and footer to absolute paths (Even using complete absolute paths from the drive forward C:/xampp... does not work.) It might be worth pointing out that changing the filename to something that doesn't exist does not throw an error. So I don't know if it finds the files. Maybe someone can tell me a good way to test this?
This is my header file:
<!DOCTYPE html>
<html>
<head>
<title>PDF header</title>
<style>
html {
display: block;
}
body {
font-family: Calibri, "Segoe Ui Regular", sans-serif;
letter-spacing: 0px;
}
</style>
</head>
<body style="padding-top: 30px">
<img src="../../images/logo_extra.jpg" style="width: 100%;"/>
</body>
</html>
This is my main file:
<?php
session_start();
require __DIR__ . '/../vendor/autoload.php';
use Knp\Snappy\Pdf;
$pdf = new Pdf('pdf\wkhtmltopdf\bin\wkhtmltopdf');
header('Content-Type: application/pdf');
// header('Content-Disposition: attachment; filename="offerte.pdf"');
$pdf->setOption('header-html', 'pdf/header.html');
$pdf->setOption('footer-html', 'pdf/footer.html');
$pdf->setOption('load-error-handling','ignore');
// I know there is a 'cover' function in WKHTMLTOPDF
$file = file_get_contents('pdf/cover.php');
echo $pdf->getOutputFromHtml($file);
?>
And as always, please:
Give me an explanation and maybe an example but not just a bunch of working code!
PS: If you see any other mistakes, please let me know.
wkhtmltopdf has an issue with header/cover/footer. I didn't dig into it very deep as adding margins did solve it for me:
<?php
session_start();
require __DIR__ . '/../vendor/autoload.php';
use Knp\Snappy\Pdf;
$pdf = new Pdf('pdf\wkhtmltopdf\bin\wkhtmltopdf');
header('Content-Type: application/pdf');
//just set margins
$pdf->setOption('margin-top', 20);
$pdf->setOption('margin-bottom', 15);
$pdf->setOption('margin-left', '0');
$pdf->setOption('margin-right', '0');
$pdf->setOption('header-html', 'pdf/header.html');
$pdf->setOption('footer-html', 'pdf/footer.html');
$pdf->setOption('load-error-handling','ignore');
$file = file_get_contents('pdf/cover.php');
echo $pdf->getOutputFromHtml($file);
?>
Description
Second problem is weird - nonexisting filename should throw an error. Comment out header and try then with false filename, snappys AbstractGenerator should say something...

Is there any other method to output variable content in php?

I did a setup of scssphp in my framework and made it working with multiple files. Below is the code of how it output itself as text/css.
<?php header('Content-type: text/css');
ob_clean();
function scss_css() {
require "../SCSS-PHP/scss.inc.php";
$import_way = "../styles/scss/";
$compression = "scss_formatter_compressed";
$compiler_script = '#import "demo.scss"';
$scss = new scssc();
$scss->setImportPaths($import_way);
$scss->setFormatter($compression);
echo $scss->compile($compiler_script);
}
scss_css();
die();
?>
The call i used in html is:
<link rel="stylesheet" href="binder/loader.scss.php">
I would like to know if echo is safe for my future working scss setup. Can there be any attack on such a file output and any other suggestions to make it better?

Why site looks differ if printed with php?

I have strange problem, I make for example 3 files main.html style.css index.php,
then when I open index.php which read main.html and print to output I get different appearance than when I opening main.html, do you know what I making wrong ?
HTML:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>aa</title>
<link rel="stylesheet" type="text/css" href="style2.css"/>
</head>
<body>
<div id="container">
a
</div>
</body></html>
CSS:
body
{
width: 100%;
padding:0;
margin: 0;
background: #f6f6f6;
}
#container
{
margin: 0 ;
padding: 0;
background: #f6f6f6;
}
PHP:
<?php
$output = file_get_contents("main2.html");
echo $output;
?>
This main2.html
This is index.php
Where you have this:
<?php
$output = file_get_contents("main2.html");
echo $output;
?>
Just simply change to
<?php include ('main2.html');
Use the PHP Require Function.
//PHP
<?php
require 'main.html';
?>
Also you should set the width: 100% on the header not on the body.
http://php.net/manual/en/function.file-get-contents.php
file_get_contents — Reads entire file into a string
http://php.net/manual/en/function.include.php
The include (or require) statement takes all the text/code/markup that exists in the specified file and copies it into the file that uses the include statement.
So better use include or require. Note : I am writing from a mobile that's why it is not so formatted.
I agree with #jordan davis "Also you should set the width: 100% on the header not on the body."
I find solution, i must set php file encoding to ANSI or I set all files encoding UTF-8 without BOM + header('Content-Type: text/html; charset=utf-8'); to php file.
In my last template system I used this function:
private function read_file($filename){
$code = '';
if(file_exists($filename)){
$templatefile = fopen($filename, 'r');
while(!feof($templatefile)){
$code = $code.fgets($templatefile, 1024);
}
fclose($templatefile);
}
return $code;
}
That works perfectly for me. Insted of file_get_content im using fopen to open the File and then with fgets im reading the Content and finally it has to be closed with fclose. See the PHP.net for further Information about the function.
You can also add some Code for a 404 Message if your Template File doesn't exist.

Wordpress - Less And PHP

I'm trying to figure out how to make a mixins.less file using PHP variables.
I made an Admin page and inside it I have colour selector. I want to use that colour inside my less files.
#colour: $mycolour;
I'm thinking to write a din_mixinds.less file using PHP and inside it to add the text;
And the to include the files in my style.less.
Or how can I do something better like this:
<?php
header("Content-type: text/css; charset: UTF-8");
$brandColor = "#990000";
$linkColor = "#555555";
$CDNURL = "http://cdn.blahblah.net";
?>
But with less not css.
Thank you
EDIT:
Based on Bass Jobsen answer.
Gives me an error:
<?php
header("Content-type: text/css; charset: UTF-8");
?>
body{
background-color: <?php echo 'red'; ?>;
}
Doesn't return any errors:
<?php
header("Content-type: text/css; charset: UTF-8");
?>
body{
background-color: red;
}
Well technical you can create a less.php file:
<?php
header("Content-type: text/css; charset: UTF-8");
$brandcolor = "#ff0099";
echo "#brandcolor: $brandcolor;";
And than serve that file on a URL (web server) and write in your Less code:
#import (less) url('http:/localhost/less.php');
p{
color: #brandcolor;
}
update
Yes ... I use less.php
I think that creates a different situation, with less.php you can use the following code to solve your issue:
$parser = new Less_Parser();
$parser->parseFile( 'style.less', 'http://example.com/mysite/' );
$parser->ModifyVars( array('brandcolor'=>'#ff00ff') );
$css = $parser->getCss();

Categories