0

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.

6
  • 2
    I take it your css file is being processed as PHP (e.g. you've done AddHandler .css php-script-type stuff in apache, or named it whatever.php so it'll be executed as php code? Commented Dec 16, 2013 at 19:52
  • Try this link Commented Dec 16, 2013 at 19:53
  • Thank you, but as i said i got it working. But i am doing something wrong with the session. If i just create a string inside my css with value of 'red' it works. WHen i try the same with an session than it wont work. Thanks for your response @NikhilKudtarkar Commented Dec 16, 2013 at 19:56
  • I guess you are missing session_start() in your css file?? Commented Dec 16, 2013 at 19:59
  • @NikhilKudtarkar can you make a answer so i can flag that as solved? Commented Dec 16, 2013 at 20:05

3 Answers 3

2

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 ?>;
Sign up to request clarification or add additional context in comments.

5 Comments

Sorry, I am no fan of using CSS inside my HTML. Thank you for your possible solution but this won't work for me. Thank you very much
Exactly why he can't?
@Raffaele is it possible ? i am not aware of such a way.
@Mazraara there are various apche edits to make this work, or even making your .css file a .php with css inside it. There are various ways, all of which are sort of "hackish" so to speak. LESS is a much better solution.
Why wouldn't you just use best practice and give something the class border-red or something that tells you that's a class that gets a red border to begin with. I upvoted your answer because it works, but the sensible thing to do here is tell OP that he's wrong for even doing it this way.
0

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.

1 Comment

I am flagging this as solved. This is a part of the solution. The solution was found through the comment and i can not flag that as solver and do not want people to unnecessary wander in my post. Thank you all. And my deepest apologies.
0

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.