coldvein Posted May 3, 2010 Share Posted May 3, 2010 hi, I want use a php file to rotate/randomise a css background image. it is a premade file (http://www.alistapart.com/d/randomizer/rotate.txt), i know nothing about php. the problem is that firefox aggressively caches the image so that it screws up on firefox alot. i apparently found a solution on google: http://hivelogic.com/articles/the-cache-trick but i cannot get it to work. the image doesnt show at all when i try to put this in. im not sure where to put the "<?php $randy = md5( uniqid (rand(), 1) ); ?>" part. ive tried putting it in the body of the html file but no luck. i tried putting it in the php file and no luck. i put the "<?php echo($randy); ?>" part in the css file which i think is right. anyone know how to get this little bit of code working? many thanks EDIT: it is as if the "<?php $randy = md5( uniqid (rand(), 1) ); ?>" php code is not valid, as dreamweaver does not colour code it at all like it usually does. unless i'm just totally using it wrong Link to comment https://forums.phpfreaks.com/topic/200540-simple-i-think-php-help-please/ Share on other sites More sharing options...
oh_boy123 Posted May 3, 2010 Share Posted May 3, 2010 Because your given code will not work really. and don't put php script on .css file put this in your .php file Try this one <? $background[] = 'images/yourbackground1'; // change the values inside ' ' $background[] = 'images/yourbackground2'; $background[] = 'images/yourbackground3'; $background[] = 'images/yourbackground4'; $background[] = 'images/yourbackground5'; $r = rand (0,4); // for as many as you want $background[] = 'images/yourbackground6'; etc ?> <body background="<?=$background[$r]?> "> Link to comment https://forums.phpfreaks.com/topic/200540-simple-i-think-php-help-please/#findComment-1052307 Share on other sites More sharing options...
ChemicalBliss Posted May 3, 2010 Share Posted May 3, 2010 Yes if your not using PHP to display the actual image then go for oh_boy123's suggestion. On the other hand if you do want to use that script you found (rotate.txt/php) then read on: The code is valid, what that code does though is just create the unique id for the image. You need to add that unique id to the url of the image, eg. if this was your image url: mydomain.com/images/somebackground.jpg Then you would add the id to the end (with a ? before the id) like so: mydomain.com/images/somebackground.jpg?8rt8gycbweycwtejsa839oasnlu39dnwhd39d This way the browser thinks its a totally new file, so it will reload (download) the file every single time unless the rare event happens when its already cached that id (within its cache lifetime). Also, any PHP Code will have to go into a .php file (by default). This means the CSS that defines your background must also be in the .php file. Turn your .html file into a php file (don't worry, PHP files can handle native HTML content). Then find the CSS code for your background rotater script - rotate.php. Then add this directly after the filename: ?<?php echo(md5( uniqid (rand(), 1) )); ?> So it would look like; somepath/rotate.php?<?php echo(md5( uniqid (rand(), 1) )); ?> -cb- Link to comment https://forums.phpfreaks.com/topic/200540-simple-i-think-php-help-please/#findComment-1052311 Share on other sites More sharing options...
coldvein Posted May 3, 2010 Author Share Posted May 3, 2010 thank you both very much for helping but i am now very confused. what i have now is random.php (from rotate.txt) in it's own directory alongside all of the images i want randomised. then, according to the guy that wrote it, i simply need to replce any <img src> i want with the location of the .php file, and it will be randomised. including css, so i have: #container { width: whatever; height: whatever else; background: url(blah/random.php) no-repeat; in my css file, which works great, apart from in firefox. what i cannot work out is where exactly to place this code to get this unique id thing working. do i put it in the html file? in the css? in the .php file? or something else entirely? apologies for my extreme newbiness, i am a lowly designer. i would appreciate any idiot-proof clarification! Link to comment https://forums.phpfreaks.com/topic/200540-simple-i-think-php-help-please/#findComment-1052320 Share on other sites More sharing options...
ChemicalBliss Posted May 3, 2010 Share Posted May 3, 2010 any PHP Code will have to go into a .php file (by default). This means the CSS that defines your background must also be in the .php file. Once you have sorted the above problem; (Put the css background code inside the .html file - in a <script> tag in the header, rename the .html file to a .php file. Voila. Then re-follow the post i made above which explains where to put that code (use the code i gave, it echos out straight away and is easier for you). -cb- Link to comment https://forums.phpfreaks.com/topic/200540-simple-i-think-php-help-please/#findComment-1052341 Share on other sites More sharing options...
coldvein Posted May 3, 2010 Author Share Posted May 3, 2010 thank you so much! i got it working just to clarify: there is no way for me to have this work *and* keep the style in an external stylesheet? someone else suggested renaming the whatever.css to whatever.php but that caused no styles at all to be applied to the page. thanks again, i've been banging my head against a wall with this for the best part of a day. Link to comment https://forums.phpfreaks.com/topic/200540-simple-i-think-php-help-please/#findComment-1052389 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.