Reputation: 458
Currently I am creating my website totally dynamic with PHP. Now i want to edit some CSS properties through my Index.php
Inside Index.php i am sending the color of the border to the css file with an session.
session_start();
$_SESSION['CssBorder'] = 'red';
And in my Css file, i take the session
<?php
header('Content-type: text/css');
$CssBorder = $_SESSION['CssBorder'];
?>
And then use it inside css as follow
.Container{
border-left: 1px solid <?php echo $CssBorder ?>;
This is not working, it is not showing the color. and i must have done something else wrong too, since the first CSS property who follows is not being used and everything after that looks as it should be.
Also, i know it works, when i replace the session with just a normal string as following. It works perfect.
$string = 'red';
Why is it not working, and how do i solve this problem?
Thank you very much.
Upvotes: 0
Views: 172
Reputation: 20885
Generating CSS with PHP is an uncommon but feasible approach. I think in some circumstances there may be a race condition, especially because you explicitely state that using $_SESSION
is the problem.
What I mean is that I am not sure that when the PHP-generated stylesheet is requested $_SESSION['CssBorder']
has been flushed and can be read from the other process. For example
browser >>>: GET index.php
server <<<: 200 OK
server <<<: Set-Cookie: PHPSESSIONID=blah
server <<<: HTML response body here... until some style.php
browser <<<: GET style.php
browser <<<: Cookie: PHPSESSIONID=blah
At this point $_SESSION['CssBorder']
may not yet be visible to the process that handles the second request. But it's just a guess.
Upvotes: 0
Reputation: 458
As @Nikil stated, 'I guess you are missing start_session() in your css file?? ' I missed a start session inside my CSS File.
I am sorry that i created a question with this kind of error in my problem. I am a beginner PHP coder and never used Session before.
I read and thaught you only needed a session_start inside the file where you create your seassion variable.
Thanks everybody, and my sincere apologies. (and my english)
Upvotes: 0
Reputation: 5716
No you want be able do access an external style sheet this way afaik, the best solution for you is,
in the header, set style tag and before the header you should assign $CssBorder
the value.
<head>
<style type="text/css">
h1 {color:red;}
p {color:blue;}
.Container {border-left: 1px solid <?php echo $CssBorder ?>;
</style>
</head>
Note: As a matter of best practices, i believe you should rename the class in to something meaningful. dynamic-border
so it will look like,
.dynamic-border {border-left: 1px solid <?php echo $CssBorder ?>;
Upvotes: 2