Echo domain name in css stylesheet

hi all

i have a comman variable on top of every page


$domain = "http://localhost/myfolder";

and i m writing addresses of css files and images as


<link rel="stylesheet" type="text/css" href="<? echo $domain ?>/styles.css" />

But how would i call $domain variable in my stylesheet


body{font-size:12px; background:url(../images/bg.gif) repeat-x;}

vineet

Short answer: can’t be done.

Long answer: using a bit of black magic you can add variables to your CSS Files by asking Apache to redirect any stylesheet to a special PHP script which will open the stylesheet, find and replace any user-defined variables, then serve up the parsed content will be displayed as pure CSS.

Here’s a tutorial that describes how to do exactly that: http://net.tutsplus.com/tutorials/html-css-techniques/how-to-add-variables-to-your-css-files/

Similar could be done using JS I suppose.


<?php
$domain = "mywebsite1";
?>

// then elsewhere, building up some html/JS output

$('#dvBackGround').css('background','path/to/image/<?php echo $domain; ?>.jpg');

// back on with the page


You cloud create additional file called custom.css and place it in a folder relative to your PHP $domain var path. Inside it, you can put your CSS and always be sure what path to use in url(), like in background CSS property.

thank pullo

thats a long article. i will spend time learning it

vineet

I might be wrong, but I would think so long as you return a mime type header of text/css the browser will render it as a CSS instruction.

So the following could be done (in theory)

Variable setting

$domain = "http://localhost/myfolder";  

HTML markup

<link rel="stylesheet" type="text/css" href="<? echo $domain ?>/stylesheet.php?domain=<?php echo $domain; ?>" />

Filename: stylesheet.php

<?php 
  $domain = $_REQUEST['domain']; // Add validation to make sure someone doesn't redirect their to their own domain
  header('Content-Type: text/css;');
?>
body{font-size:12px; background:url(<?php echo $domain; ?>/images/bg.gif) repeat-x;}

Thanks cpradio

thats short and works for me

exactly what i wanted.

vineet

+1 for proving me wrong.
That’s a neat trick!

Couple of points maybe worth noting:

I am curious to know why you want a $domain variable at the top of every page?
I envisage "file not found " problems will arise when the files are uploaded to www.your-online-site.com.

The way I get round this is to test and see if platform is localhost and to define relevant constants.

// Typical file


<?php 

  // $domain = "http://localhost/myfolder";   
  require '/your-include-path/constants.php'

  <link rel="stylesheet" type="text/css" href="<?php echo DOMAIN ?>/styles.css" /> 

// constants.php


<?  
  defined('LOCALHOST') ?: define('localhost', 'localhost' == $_SERVER['SERVER_NAME']);

  if (LOCALHOST)
  {
    defined('DOMAIN') ?: define('DOMAIN', 'http://localhost/');
    defined('P_IMGS') ?: define('P_IMGS', 'http://localhost/path-to-images/');
  }
  else
  {
    defined('DOMAIN') ?: define('DOMAIN', 'http://your-online-site.com/');
    defined('P_IMGS') ?: define('P_IMGS', 'http://your-online-site.com/path-to-images/');
  }


<hr />

But a far easier solution is to create a localhost/assets-folder/ which is duplicated online. This method does not require any constants or variables…

Test the following code to ensure that PHP prepends the URL on both localhost and online platforms.

typical files


<?php

  <link rel="stylesheet" type="text/css" href="/assets-folder/css/styles.css" /> 

// stylesheet
  body{font-size:12px; background:url( /assets-folder/images/bg.gif ) repeat-x;}


@cpradio, @vinpkl, @Pullo

I would think so long as you return a mime type header of text/css the browser will render it as a CSS instruction.

I used the above method for developing but unfortunately www.validation.w3.org does not recognise the .php extension.

To get round this I view the source using the FireFox browser then click on the stylesheet.PHP file and the source appears in another window. Source then copied, pasted and saved to stylesheet.css

Validation is only a guide. Something like this won’t even matter whether or not it pases validation. Unless you just can’t live without da badge.

I prefer to have a site validated because there are usually fewer visual browser discrepancies.

Saving the generated PHP stylesheet source to a stylesheet.css file eliminates this problem and being ready for the next hurdle :slight_smile:

I think “da badge” is off-putting and frequently when testing validation links, numerous glaring errors are shown.

I did not know that. That is good information to have. I’m sure there are ways that can be worked around it (if need be), but it is something to keep in mind that you will have to do a workaround to validate your stylesheets.

Why are you using the domain variable? Start the url with a forward slash and it uses the root anyway.

http://localhost/myfolder === /myfolder

yes, i also tried to validate it

but it didnt get validated

vineet

Please supply a link to the site.