• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Dynamic CSS using session variables

#1
Hi everyone,

It's my first topic here and I'm not english-speaker so please be lenient ^^
If there are some french-speakers, I began a topic here : https://openclassrooms.com/forum/sujet/c...de-session
I'm using CI since some months to develop my own web platform. My sessions works great and I wish every user can custom his interface color. For that purpose, I store his color on my SQL DB with type "varchar(6)" and I try to reach it in a CSS sheet but it does not work... Here is my CSS code :
PHP Code:
<?php
    header
('content-type: text/css');
    header('Cache-Control: max-age=31536000, must-revalidate');
    session_start();
?>
body{
    --second-color: <?php echo "#" $_SESSION['CouleurInterface'?>;



I tried replacing by this :
PHP Code:
<?php
    header
('content-type: text/css');
    header('Cache-Control: max-age=31536000, must-revalidate');
    session_start();
?>
body{
    --second-color: <?php echo "#" "ff0000" ?>;


and it works great even if nothing is dynamic Big Grin
I naturally tested if my variable " SESSION [' CouleurInterface '] " exists, by showing it in the PHP page and it exists well, with the value which I gave it in the DB for the user (example: ff0000, I'm adding the "#" in the code). Do you know where the problem's come from ? I looked well on the web and however, with the others it works x)

Thanks by advance

PS: if you need more code, don't hesitate asking me because I didn't want to lose you ^^
Reply

#2
Not sure what all your code is doing above. Perhaps you could do a pre-controller hook to check the session variable in which you should store a theme name like 'blue_theme', not just a colour. It could set the session value to 'default_theme' if the session variable is not already set.

Then in your header you could do something like
PHP Code:
// main site css
<link rel="stylesheet" href="<?php echo base_url('css/site.css'); ?>">
// theme css overriding default main site
<link rel="stylesheet" href="<?php echo base_url('css/themes/'.$_SESSION['theme']); ?>"

Or not bother with that at all and just do a check in the header_view
PHP Code:
<?php $theme = (!empty($_SESSION['theme']) ? $_SESSION['theme'] : 'default_theme'?>
<link rel="stylesheet" href="<?php echo base_url('css/themes/'.$theme); ?>"> 

If you store it in a database (when the user logs in) the next time they log in you can check what is set and use that. Then the theme choice will be permanent too.

Now you can put as much css as you want in your theme css, altering colours, layouts, what is displayed and where etc!

Hooks in the docs:
https://www.codeigniter.com/user_guide/g...hooks.html

Hope that helps,

Paul
Reply

#3
(11-04-2017, 01:47 PM)PaulD Wrote: Not sure what all your code is doing above. Perhaps you could do a pre-controller hook to check the session variable in which you should store a theme name like 'blue_theme', not just a colour. It could set the session value to 'default_theme' if the session variable is not already set.

Then in your header you could do something like
PHP Code:
// main site css
<link rel="stylesheet" href="<?php echo base_url('css/site.css'); ?>">
// theme css overriding default main site
<link rel="stylesheet" href="<?php echo base_url('css/themes/'.$_SESSION['theme']); ?>"

Or not bother with that at all and just do a check in the header_view
PHP Code:
<?php $theme = (!empty($_SESSION['theme']) ? $_SESSION['theme'] : 'default_theme'?>
<link rel="stylesheet" href="<?php echo base_url('css/themes/'.$theme); ?>"> 

If you store it in a database (when the user logs in) the next time they log in you can check what is set and use that. Then the theme choice will be permanent too.

Now you can put as much css as you want in your theme css, altering colours, layouts, what is displayed and where etc!

Hooks in the docs:
https://www.codeigniter.com/user_guide/g...hooks.html

Hope that helps,

Paul
Hi and thanks for trying to help me but I would use directly php in my css, I saw it was possible and, in the other forum they dont understand why it do not work with me because, when we write :
PHP Code:
<?php
session_start
();
$_SESSION['color'] = (string)dechex(random_int(0,4094));
?>
<!DOCTYPE html>
 
<html>
 
<head>
    <title>Test</title>
    <meta charset="utf-8" />
    <link rel="stylesheet" href="dynamique.css.php" />
</head>
 
<body>
    <p>&nbsp;</p>
</body>
 
</html> 
and
PHP Code:
<?php
session_start
();
header('content-type: text/css');
header('Cache-Control: no-cache, must-revalidate');
     
?>
body{
    background-color: #<?php echo $_SESSION['color'?>;


it works great... My real problem is that CI sessions variables aren't reachables on my CSS
Reply

#4
If you intend to use the CI session in your CSS, you'd have to route the requests through CI and load the session library. So, in your .htaccess file you'd rewrite the request to a controller/method that CI could route to, then you'd load the session library and output the CSS as a view. With the appropriate headers of course. Technically, if you have everything set up right you could use the CI session outside of CodeIgniter, but there's a lot going on in the session library, and it's likely you haven't configured your external session to be compatible with CI's session. One does not simply call session_start().

Another thing, and it would be an obstacle for you if trying to use the CI session outside of CodeIgniter, is that CodeIgniter uses a session handler that prepends the session name to the file path, and unless you replicate this functionality, you'd never have success retrieving, using, or working with a session created in CI. It's certainly something you could do, but you'd end up creating a library outside of CodeIgniter that was more or less a copy of what CodeIgniter is using. What I'm talking about is this:

CodeIgniter creates a session and the session file is named:

ci_session209385023850293850928309528

You try to access the session outside of CodeIgniter, but your session file is named:

sess209385023850293850928309528

A MUCH easier solution for you would be to use a simple cookie for this CSS related value. You don't need the session for everything under the sun.
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.