PHP generated style sheets
Moderator: General Moderators
PHP generated style sheets
Is it possible to generate a style sheet using a form and php? for accessibility purposes for example, i could have an "accessibility options" link to the form which will generate their personal style sheet.
Yes that's possible. However I don't think it's easy. Both for developing something like this and for the end user.
For a visitor of your site you vould provide a simple stylesheet switcher, of which there are several good ones available. Both javascript based and PHP.
Then on your webpage show a coulpel of buttons or dropdown menu to let people choose. With cookies you can make the change persistant
For a visitor of your site you vould provide a simple stylesheet switcher, of which there are several good ones available. Both javascript based and PHP.
Then on your webpage show a coulpel of buttons or dropdown menu to let people choose. With cookies you can make the change persistant
Yes, it's possible.dhrosti wrote:Is it possible to generate a style sheet using a form and php? for accessibility purposes for example, i could have an "accessibility options" link to the form which will generate their personal style sheet.
A stylesheet is a text file that obeys some syntax rules. PHP can write text files.
Here's a very simple example of how you can create a stylesheet that allows the user to choose the background colour of the web-page:
User Form:
Code: Select all
<form id="stylesheet_creator" method="post" action="create_stylesheet.php">
Background Colour:<input name="bg_colour" type="text" value="#FFFFFF" />
<input type="submit" name="submit" value="Create" />
</form>Code: Select all
if(isset($_POST['bg_colour'])) {
// form is being submitted.
// get a handle to a text file, die if we can't open it.
if(!$handle = fopen('my_stylesheet.css', 'a')) {
die('Unable to open file for writing!');
}
$bg_colour = $_POST['bg_colour'];
// don't forget to clean any user values
// write out the css syntax to the text file
fwrite($handle, "body {\n");
fwrite($handle, "\tbackground-color: ".$bg_colour.";\n");
fwrite($handle, "}\n");
}You would then need some way of linking the stylesheet to the user (database entry, cookie etc.), and a way of setting the stylesheet to the user's stylesheet. You can do this by using PHP to write the <link rel="stylesheet" href="$user_stylesheet" type="text/css" /> line in the HTML head.
- Ollie Saunders
- DevNet Master
- Posts: 3179
- Joined: Tue May 24, 2005 6:01 pm
- Location: UK
You can dynamically generate them in the same way as you dynamically generate HTML.
Filename: somecss.css.php
Filename: somehtml.html
Filename: somecss.css.php
Code: Select all
header('Content-type: text/css');
?>
body {
background:#faa;
}Code: Select all
<html>
<head>
<link rel="stylesheet" href="somecss.css.php" />
</head>
<body>...- Kieran Huggins
- DevNet Master
- Posts: 3635
- Joined: Wed Dec 06, 2006 4:14 pm
- Location: Toronto, Canada
- Contact:
- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
If this route is taken, remember to always include the header() content-type call. ..Kieran Huggins wrote:with either .htaccess or your apache config you can make php parse your CSS files. Search for "addType"