View Full Version : Echo different messages based on random visitor

11-27-2007, 08:20 PM

I would like to combine a few different PHP scripts to create one solution...maybe you guys can help.

I want to start out with a counter script, to count the visits on a single page. Now, instead of "echoing/displaying" a number, I would like it to echo/display a snippet of text based on the number/visitor...

I already have a code to count the visits...it is a "write to" a text file.

But what I want to do is to use the "$fgets" to read the last character/number in the text file. If that number is a five (5) or a zero (0), then I would like to display/echo a message, if it isn't either a 5 or 0, then I would like to display/echo a different message.

So for example: If a visitor lands on the page, and the counter senses that he is visitor number 235, then it will display a message like "great for visiting", but when the next person lands on the page, and is number 236, then a message that will be diplayed will be "thank you."

I have all of the code, such as the counter and the if/then, but I am having trouble with reading the last number/character of the text file. Because the text file will always expand, from single digit for number 1-9 up to say 5 digits for the 10,000 - 99,999, how can I create a script to read the last digit...


11-27-2007, 08:40 PM
Think mathematically. You are really asking for all multiples of 5. If you divide the hit count by 5, then a count that is a multiple of 5 will have a remainder of 0. If it's not a multiple of 5, the remainder will be something other than 0.

The way to test for this in PHP is using the modulus operator (a % sign):

$remainder = $count % 5;
if ($remainder == 0) {
echo "yo";
} else {
echo "not yo";

11-27-2007, 09:46 PM
After you get the number with fgets...

$lastdigit = substr($totalhits, -1);

11-28-2007, 12:30 AM
I used "fumigators" solution...it works great. It will also work with other numbers, which is perfect as well.

One other thing is instead of "echo "yo"" is there a way that I could pop open a new window?

Thanks again.


11-28-2007, 12:38 AM
echo the javascript command you need to pop open a new window...