rgnelson Posted August 23, 2006 Share Posted August 23, 2006 I have many users.Each user is logged in using session.I would like to have form that could modify an existing css file based on logged in user.Could someone point me in the right direction.I have googled. I can't find a place to start ;) Quote Link to comment Share on other sites More sharing options...
syed Posted August 23, 2006 Share Posted August 23, 2006 Hi can you explain it a bit more please. Do you want to display different styles for different users? Quote Link to comment Share on other sites More sharing options...
rgnelson Posted August 23, 2006 Author Share Posted August 23, 2006 Hi,Thank you,Yes. Different styles for different users.Each user should have their own syle sheet: {user_id}_style.cssEach user would have a form where they could modify some of the rules in the style sheet. Quote Link to comment Share on other sites More sharing options...
SammyP Posted August 23, 2006 Share Posted August 23, 2006 I have done this on my own site.All I did was create a file style.php and used it as a style sheet on all my pages. It outputs a css file, rather than an html file. A table holds default values for all the style elements and would create a normal looking page if no overrides existed. But in querying the table it also looks for user-specific overrides as well, and used them instead wherever they existed.So a page could offer the user the ability to enter a new value for whichever elements you opened up to them.This is a lot easier than trying to edit style sheet files.So I could add to my table Quote Link to comment Share on other sites More sharing options...
rgnelson Posted August 23, 2006 Author Share Posted August 23, 2006 That sounds like what I want to do.So, you link to the php page?<link href="/templates/style.php" rel="stylesheet" type="text/css"> Quote Link to comment Share on other sites More sharing options...
rgnelson Posted August 23, 2006 Author Share Posted August 23, 2006 I'm a little lost here.Could someone get me going in the right direction?Any clue to get me going?Thanks Quote Link to comment Share on other sites More sharing options...
448191 Posted August 23, 2006 Share Posted August 23, 2006 Use the html you posted.In style.php[code]<?phpfile_get_contents('css/'.$_SESSION['user'].'.css');?>[/code] Quote Link to comment Share on other sites More sharing options...
rgnelson Posted August 23, 2006 Author Share Posted August 23, 2006 Sorry,I'm new.There wasn't enough for me to go on.Thanks. Quote Link to comment Share on other sites More sharing options...
Caesar Posted August 23, 2006 Share Posted August 23, 2006 Sounds like you will need to go through a few more PHP tutorials or do some reading before tackling such issues. As the examples provided only require the bare minimum (Basic understanding) of PHP to grasp. Good luck.http://www.php.net Quote Link to comment Share on other sites More sharing options...
rgnelson Posted August 23, 2006 Author Share Posted August 23, 2006 Wow, that's it. :oI'm not asking you to write it for me.I'm just asking for a little more detail. ;)I can get around in php.I just don't see how this is the whole picture:[code]<?phpfile_get_contents('css/'.$_SESSION['user'].'.css');?>[/code]Just looking for a few more details.Thanks Quote Link to comment Share on other sites More sharing options...
rgnelson Posted August 23, 2006 Author Share Posted August 23, 2006 Is it a secret or is it too simple to exlain? Quote Link to comment Share on other sites More sharing options...
rgnelson Posted August 23, 2006 Author Share Posted August 23, 2006 448191I was wondering if you could elaborate a little more.I would appreciate it! :)Thanks Quote Link to comment Share on other sites More sharing options...
448191 Posted August 23, 2006 Share Posted August 23, 2006 I didn't actually test it (except for the regex), but here's code you could use to create user specific css files:[code]<?php$prop = 'color';$style = 'green';$element = 'body';$file = 'css/'.$_SESSION['user'].'.css';//Check if the user already has a preference file:if(!file_exists($file)){ //Create a new file: $fp = fopen($file,'w'); fwrite($fp,$element.' { '.$prop.': '.$style.'; }') or trigger_error('Couldn\'t write to file.');} else { //File already exists, get contents. $str = file_get_contents($file); $regPattern = '/('.$element.'\s*\{[\.#@a-z:\s-;]*)('.$prop.'\s*:\s[\.#@a-z]+;)([\.#@a-z:\s-;]*\})/i'; //Check if the property is already set: if(preg_match($regPattern)){ //If so replace the current style. $currStyles = preg_replace($regPattern,'$1'.$prop.': '.$style.'$3',$str); } else { //If not add the style. $currStyles = preg_replace($regPattern,'$1$2'.$prop.': '.$style.'$3',$str); } //Get rid of old file, and write new: unlink($file); $fp = fopen($file,'w'); fwrite($fp,$currStyles) or trigger_error('Couldn\'t write to file.');}?>[/code]I can understand if you don't immidiately grasp how it works, but I did do some heavy commenting, so you should get the basics.$prop, $style and $element you have to assign the right values yourself in a form or where ever. Loading css:<link href="/css/default.css" rel="stylesheet" type="text/css"><link href="/css/usrsheets.php" rel="stylesheet" type="text/css">usrsheets.php:[code]<?phpecho file_get_contents('css/'.$_SESSION['user'].'.css');?>[/code] Quote Link to comment Share on other sites More sharing options...
rgnelson Posted August 23, 2006 Author Share Posted August 23, 2006 448191Thank you,This gives me a start at my goal.Thanks again! :) Quote Link to comment Share on other sites More sharing options...
448191 Posted August 24, 2006 Share Posted August 24, 2006 Just thought of something, if you use php to mimic a css file, you have to use the correct MIME type....[code]<?phpheader('Content-Type: text/css');echo file_get_contents('css/'.$_SESSION['user'].'.css');?>[/code]Alternatively (and probably better/faster), simply assemble the HTML pointing to the right file.[code]<?phpecho '<link href="/css/'.$_SESSION['user'].'.css" rel="stylesheet" type="text/css">';?>[/code]Unless ofcourse you want php to handle client/proxy caching:[code]<?phpheader('Content-Type: text/css');header('Cache-Control: max-age=6000 must-revalidate');echo file_get_contents('css/'.$_SESSION['user'].'.css');?>[/code] Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.