Singularity Posted August 25, 2006 Share Posted August 25, 2006 Does anyone know how to make a script that generates a random number and stores it into a variable, but generates a random number for that variable every 24 hours? This is what I want to accomplish:----When ANY user visits my site on for example Monday, they see the number "5" (it is randomly generated out of 100 numbers). Any user who visits my site on Monday will see the number "5".After 24 hours, the system will randomly choose another number out of 100, for example, 67, and every person who visits my site on Tuesday, will see the number 67.After another 24 hours, the system will randomly choose another number out of 100, for example, 54, and every person who visits my site on Wednesday will see the number 54.----I hope you guys can help me out. Quote Link to comment https://forums.phpfreaks.com/topic/18605-random-number-every-24-hours/ Share on other sites More sharing options...
jvalarta Posted August 25, 2006 Share Posted August 25, 2006 Well, you could do something like this:1) Create a cron to run every 24 hours to call a file that does this:2)$RandomNumber = rand(1,100); //PICK A NUMBER BETWEEN 1-100$NumberFile = fopen("numberfile.txt", "w+"); //CREATE A FLATFILE TO STORE THE RANDOM NUMBERfwrite($NumberFile, "$RandomNumber"); //WRITE THE NUMBER TO THE FILEfclose($NumberFile); //CLOSE THE FILE3) Have your script read the flat file "numberfile.txt" Quote Link to comment https://forums.phpfreaks.com/topic/18605-random-number-every-24-hours/#findComment-80151 Share on other sites More sharing options...
Singularity Posted August 25, 2006 Author Share Posted August 25, 2006 Im looking for another way to do it, preferrably a method that uses only PHP without having to use cronjobs. I heard someone say that you can use srand and microtime or something to do this, but I am not sure. Remember that the random number is stored in a variable. So like $therandomnumber is equal to for example, 64 one day, and then 30 the next day, 40 the day after, etc. As long as it keeps the same number for 24 hours, and then the number changes after 24 hours.If someone can help me come up with a solution to this small problem using only PHP without the use of Cronjobs or database, please let me know. Much appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/18605-random-number-every-24-hours/#findComment-80154 Share on other sites More sharing options...
jvalarta Posted August 25, 2006 Share Posted August 25, 2006 If you are going to create a number that needs to last for 24 hours for any user who visits, you need to store that number somehwere -- a flat file or a database. You can't use a session, or cookie, as that's a per user variable. The example I gave you would pick a number and store it in a flatfile...then you can very easily just read into a web page.And as for the cron, if you want to automate this, you really need to use a cron to have it kick off whatever process you end up choosing to create the number. I don't know of any other method that you could automate something like this. i suppose to could create a quasi-daemon in PHP by creating a loop and using sleep()...but this is hooky at best.Good luck. Quote Link to comment https://forums.phpfreaks.com/topic/18605-random-number-every-24-hours/#findComment-80164 Share on other sites More sharing options...
AndyB Posted August 25, 2006 Share Posted August 25, 2006 The only alternative to using a cron to kick-start the process is basically to include some code on your index page (or every page if you want) - to read a text file. If today's date does not match the date stored in a small text file then re-write that text file with a new date AND a new random number, otherwise just read the random number of the day from that text file. Quote Link to comment https://forums.phpfreaks.com/topic/18605-random-number-every-24-hours/#findComment-80167 Share on other sites More sharing options...
.josh Posted August 25, 2006 Share Posted August 25, 2006 only alternative to a cronjob is to have your script for the webpage itself check the time and update the file/db based on the time, whenever any user accesses the webpage. But at best, this is extra code and time wasted, and will never be accurate, because it will be executed everytime someone(s) accesses the page, and unless someone happens to access it at exactly midnight or whatever, it will not update until the first person of the next day accesses it. I guess in reality if there is nothing else involved in this-- that is, all you want is this random number to be displayed, and this number will not be time sensitive for some other script running at certain times, then this method might actually be okay..if you have a relatively small amount of users.. Quote Link to comment https://forums.phpfreaks.com/topic/18605-random-number-every-24-hours/#findComment-80168 Share on other sites More sharing options...
Singularity Posted August 25, 2006 Author Share Posted August 25, 2006 i understand that i must use a flatfile db. can someone please post the method to do iT w/o cron? or if any other solutions are out there, please inform. Quote Link to comment https://forums.phpfreaks.com/topic/18605-random-number-every-24-hours/#findComment-80174 Share on other sites More sharing options...
Ifa Posted August 25, 2006 Share Posted August 25, 2006 You could just change the number at midnight (if time = midnight). It won't change it until the first person visits the site though, but then again, what's the need for a chage if there's no one to see it :) Quote Link to comment https://forums.phpfreaks.com/topic/18605-random-number-every-24-hours/#findComment-80177 Share on other sites More sharing options...
Singularity Posted August 25, 2006 Author Share Posted August 25, 2006 Ok, what exactly is the command that I have to run in the Cron to call up the numberfile.txt located in the public_html folder? Quote Link to comment https://forums.phpfreaks.com/topic/18605-random-number-every-24-hours/#findComment-80293 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.