views:

51

answers:

3

hi,

what's the best approach to change the background of my website at each visit ?

1) write php code, loading a random css file containing the background property 2) write php code, generating different html (and including the background property directly into html code 3) something else ?

thanks

A: 

I would probably:

  1. Use hook_user op login to detect the login and then store the background color code in the user object.
  2. In your page template create an inline style for the background color that uses the value stored on the user object. For anonymous users don't do anything and have default defined in a style sheet.
googletorp
A: 

Use a session cookie. Could be set either via js (client side) or something like php (server-side). Here's an example of a js-only solution:

<!doctype html>
<html><head><script>

var backgrounds=['foo.png', 'bar.png', 'hahah.png'];

function setBg () {
  var currentBg=readCookie('which_bg');
  if (!currentBg) {
    currentBg=backgrounds[Math.random()*backgrounds.length|0];
    createCookie('which_bg', currentBg);
  }
  document.body.style.backgroundImage='url('+currentBg+')';
}

// from http://www.quirksmode.org/js/cookies.html

function createCookie(name,value,days) {
  if (days) {
    var date = new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    var expires = "; expires="+date.toGMTString();
  }
  else var expires = "";
  document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
  var nameEQ = name + "=";
  var ca = document.cookie.split(';');
  for (var i=0;i < ca.length;i++) {
    var c = ca[i];
    while (c.charAt(0)==' ') c = c.substring(1,c.length);
    if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
  }
  return null;
}

function eraseCookie(name) {
  createCookie(name,"",-1);
}

</script></head>
<body onload="setBg();">
...
</body></html>
no
+1  A: 

This can be done in your theme's page.tpl.php variable preprocessor. Store the random style in the $_SESSION array to re-use for all pages in the same user session. And append the markup to the $head variable used in the template.

YOURTHEME_preprocess_page(&$variables) {
  $style = $_SESSION['YOURTHEME_background_style'];
  if (!$style) {
    $style = array();
    //Generate your random CSS here
    $style = "background-image: url('bg-". rand(0,10) .".png')";
    $_SESSION['YOURTHEME_background_style'] = $style;
  }
  $variables['head'] .= '<style type="text/css">body {'. implode("\n", $style) .'}</style>';
}

Usually, $head is placed before $style in the page.tpl.php templaye, so CSS rules from any .css files will overrides your random rule. You may have to use !important in your random CSS to avoid this.

mongolito404
Somehow I missed the fact that this question was aimed specifically at drupal (probably because drupal wasn't mentioned in the title or body of the post, only the tags). I see now that it is, but... wouldn't it make more sense from a code reuse standpoint to adopt a non-drupal-specific solution if possible, even though OP tagged this as a drupal question? The q really has nothing to do with drupal. Also, why use PHP for this when the burden of processing can be pushed onto the client (js)? Doesn't this seem like exactly the kind of progressive enhancement stuff js should be doing (not php)?
no
I agree. The question sounds purely client-side in a way that is primarily relevant to folks that *probably* have javascript available. For a one-off solution though, this is fine. Especially for the less js-savvy.
Grayside

related questions