Just Another Blog 4 Shared

why do we have the curiosity?

Simple PHP Trick for Changing Web Style Every Reloaded

Feel boring, when I opened my site and always seen the same color. I Tried to make changing for it. After surfing and asking uncle google, then I modified the php script. Here is my web if you wanna see the demo, http://the2ndroom.host898.net. I just changed the color, but with this script you can modify by your self what do you wanna be changed.

Take a look for this script :

<?php
$file = 'hits.txt';
if (!is_writable($file)) die('not writable');
$current = trim(file_get_contents($file)) + 1;
fwrite(fopen($file, 'w'), $current);
?>

This script will write a number on file hits.txt every page that load this script reloaded. And the number will grow. We can use variable $current to make changing the web style by using  mod (%) mathematic operation. This is the script that I used on my web :

<?php
$file = 'hits.txt';
if (!is_writable($file)) die('not writable');
$current = trim(file_get_contents($file)) + 1;
fwrite(fopen($file, 'w'), $current);
if ($current%2==0){
echo "<link href='style1.css' rel='stylesheet' type='text/css'>";}
else {
echo "<link href='style.css' rel='stylesheet' type='text/css'>";}

?>

At first, I duplicated file style.css. That file is the file that manage my web style. Then I changed some color variable on the duplicated file. You can make changing by your self, even totally changing for the layout. And the changing is not just twice, depend on the mod by number. For example :

if ($current%4==0){
echo "<link href='style1.css' rel='stylesheet' type='text/css'>";}
elseif ($current%4==1){
echo "<link href='style2.css' rel='stylesheet' type='text/css'>";}
elseif ($current%4==2){
echo "<link href='style3.css' rel='stylesheet' type='text/css'>";}
elseif ($current%4==3){
echo "<link href='style4.css' rel='stylesheet' type='text/css'>";}

If you wanna change web style by the day, you can use time() function. For example :

$hari = date("w");
if ($hari==0){
echo "<link href='style1.css' rel='stylesheet' type='text/css'>";}
elseif ($hari==1){
echo "<link href='style2.css' rel='stylesheet' type='text/css'>";}
elseif ($hari==2){ etc...

Well, this is just litle thing that I can share, because only thing like this that can be done by loser like me :). But I hope with my full hoping this thing will be useful for you. Good luck.......

0 comments:

Post a Comment