Editing CSS via PHP?

I’m not sure if this is the right section to put this in but my question is this:

Bascially I have an admin section I’m creating with PHP on my site and what I would like to do is be able to call upon my external stylesheet and edit it via it’s own php page (ie. stylesheet.php)

Is this possible? Does anyone have any examples of how to best implement this? Is it best to do something like this via php or should I be going another route?

You can just use:
<LINK href=“stylesheet.php” rel=“stylesheet” type=“text/css”>

PHP will execute fine, but you won’t have any information about the page you’re on. Technically, I think you can use $_SERVER[‘HTTP_REFERER’] but it’s probably not the most helpful.

I think the more expected approach is to select a different stylesheet from within the page your building. So in pageA.php you will have:
<LINK href=“stylesheetA.css” rel=“stylesheet” type=“text/css”>
In pageB.php:
<LINK href=“stylesheetB.css” rel=“stylesheet” type=“text/css”>

Another way is to just output the CSS directly in the head, but if your pages are dynamic you’re better off with an external stylesheet so you can take advantage of browser caching.

Yet another is to just use different classes and ids on the page, and throw everything in the same CSS file.

We’d be able to better answer the question if we knew what you were doing. I’ve only had to dynamically generate CSS files once, and it’s probably not the best solution.


Is this what you’re looking for?

     echo '<link href="adminpage.css" rel="stylesheet" type="text/css">';
    echo '<link href="general.css" rel="stylesheet" type="text/css">';

Beware that if you do this, you must include the following line as the first line of the PHP:

header('Content-Type: text/css');

If you don’t, firefox won’t recognise the file as CSS and discard it (don’t apply any rules). At least FF2 won’t, not sure about FF3. Haven’t been serving up any css files through PHP lately :slight_smile:

i think she is looking to edit the css via a .php file kinda like wordpress does in the appearance editor / theme-editor.php for editing the css of themes
not display different styles per page file
not sure tho

but if its the other why dont you just use a separate header for admin being it will most likely have lots of stuff that is not needed

and as for the css in php
u can just require different styles.php files per that display css as you would in the header
i have put just my css & js in a styles.php file and it worked with no problem that i know of i just required the style.php file in the header tho im no pro so…

Yes, eFingerz that is exactly what I’m trying to accomplish.

Yes, it’s possible but I wouldn’t recommend it (I’ll explain later why). Just use fread() or some other function to read the contents of CSS and load it into a string:

$filename = "/usr/local/something.txt";
$handle = fopen($filename, "r");
$contents = fread($handle, filesize($filename));

Then just have a form with textarea (you can convert the textarea to some nice CSS editor with JavaSript):

<form enctype="application/x-www-form-urlencoded" method="post" action="process.php">
      <label for="css">Stylesheet</label>
      <textarea name="css" id="css" rows="10" cols="50"><?php echo $contents; ?></textarea>
      <input type="submit" name="csssubmit" id="csssubmit" value="Submit" />

Then when processing the form, just save the new CSS to the file again, for instance like this:

$contents = $_POST['css'];
$filename = "/usr/local/something.txt";
$fp = fopen($filename, 'w');
fwrite($fp, $contents);

But this is not a good idea because malevolent users with ability to edit CSS files means a greater possibility of XSS attack.