[Reposting as all was lost in the recent board hack]
OK. So you've made a dozen or so cool skins for your b2evolution blog, and you want to make sure your loyal readers see all of them. Watcha gonna do?
This Random skin chooser HACK will present visitors with one of your skins, at random, on each visit to your blog. The user may chose a different skin through a on-page skin-chooser (if you present that option), but the next time they visit, they'll be presented with a new skin at random.
Here's how to implement it.
[u]STEP 1[/u]
Copy and paste the following code into a file called cdRandomSkin.php in your blog's root directory (or whatever directory you prefer).
<?php
////////////////////////////////
//
// Random skin chooser HACK for b2evolution
//
// by Rob M. Worley
// http://www.thanator.com
//
////////////////////////////////
$cookie_random_skin = 'cookie'. $b2evo_name. 'rsch_skin';
// If the user is explictly asking for a skin via
// the skin chooser then we don't
// want to force a random skin on it...
if( (!empty($_GET['skin'])) || (!empty($_POST['skin'])) )
{
// Get the skin out of the GET/POST
param( 'skin', 'string', '' );
// Set our random skin cookie to the selected skin
if( ! setcookie( $cookie_random_skin, $skin,
0, $cookie_path, $cookie_domain) )
{ // This damn failed !
echo "<p>setcookie failed!</p>";
}
}
else
{
// Fill the random skin flag from the cookie cookie setting
param( $cookie_random_skin, 'string', '' );
// If the random skin flag hasn't been set...
if (empty($$cookie_random_skin))
{
// We are going to select a random skin...
// Cycle through the skin list...
skin_list_start();
while (($rsch_file = skin_list_next()) !== false)
{
// If the directory we found is in the
// exclude list the we can skip it
//
// NOTE: The exclude list argument should be defined in
// conf/_config.php
if (!empty($rsch_exclude_list) &&
in_array ($rsch_file, $rsch_exclude_list))
continue;
$rsch_choices[] = $rsch_file;
}
// Now we can select a skin at random for the list of
// skin choices...
$rsch_selection = rand(0, (count($rsch_choices) - 1));
$skin = $rsch_choices[$rsch_selection];
// We only want to do this once per user session,
// so let's set the Random Skin Flag in a cookie
if( ! setcookie( $cookie_random_skin, $skin,
0, $cookie_path, $cookie_domain) )
{ // This damn failed !
echo "<p>setcookie failed!</p>";
}
}
else
// We already selected a random skin for this user
// once this session, so we'll just fill the
// $skin global override with the random skin
// we cookie-fied earlier...
param( 'skin', 'string', $$cookie_random_skin );
}
?>
[u]STEP 2[/u]
Copy and paste the following code into your _config.php file (in the /conf directory off of your blog's root).
/**
* RANDOM SKIN CHOOSER HACK
*
* if you want certain skins excluded
* from Random skin selection put
* the skin names in this exclude list.
*/
$rsch_exclude_list = array("generic", "basic");
[u]STEP 2b[/u]
Adjust the random skin chooser settings: You can use the $rsch_exclude_list to list any skins that you DON'T want to serve to the user at random. The user may still select these skins from a skin selector later, but they will never see them through the randomizer.
[u]STEP 3[/u]
HACK b2evolution. This is the scary part. Edit your index.php file in the blog's root. Look for the following comment lines:
# You could *force* a specific skin here with this setting:
# $skin = 'basic';
After these lines, copy and past the following HACK code:
#
# RANDOM SKIN CHOOSER HACK
#
require_once dirname(__FILE__).'/cdRandomSkin.php';
Obviously, that last "require" line should be altered if you decided to store your cdRandomSkin.php file in a directory other than the root.
That should do it! Now with each session, users will see a different skin. The skin will persist throughout the session, but if they leave and come back to the site later, they'll see something new.
Enjoy.
Will this work with the newest version of b2evo? I tried a hack very similar to this one and it gave me errors.