Page 1 of 1

Count and display next page in php

Posted: Wed Jun 05, 2002 4:22 am
by ReBrand
This sounds like it should be quite easy but I don't know where to start. Any pointers would be great.


Say I have 12 pages (numbered 1.htm to 12.htm) in a folder (/mydir) - although I want to be free to add more at any time without altering anything else.

Here's what I want to do:

pickone.htm will have a text link that opens count.php into a new window

When a visitor clicks that link, count.php needs to:

- read the contents of count.txt (1 line text file containing a number - initially set to 0) into a variable (count)

- add 1 to count

- look in /mydir and check if 'count' + '.htm' exists.

- if it exists, display the page, add 1 to count and save count.txt ready for the next visitor.

Next visitor would therefore get 2.htm and the next gets 3.htm, etc. But say I have 28 pages in the folder I want it to reset count to 0 if 29.htm doesn't exist and then repeat so that the 29th visitor gets 1.htm again.

Is that possible with a simple txt file in PHP? Or is there a much better way?

count and open

Posted: Wed Jun 05, 2002 9:03 am
by akoessler
First of all: of course it's possible!

Some ideas I had while I read your post:

I took an old counterscript and modified it a bit:

function countandopen()
{
$counterFile = "count.txt";
$fp = fopen($counterFile, "r+");
$count = fgets($fp,5); //I think 99999 Pages should be enough
$count += 1;
rewind($fp);
$file=$count."htm";
if (!file_exists($file))
$count = "0";
fputs($fp,$count,5);
fclose($fp);
//now open the file $file with javascript or something else
}

I havn't tested this script! But it should work!
Be carefull! You have to be able to write in this directory (mod777)!
nice day koe

Posted: Wed Jun 05, 2002 9:27 am
by ozmodiar
Try this, I think its what you were looking for:

Code: Select all

<?php

$count_file="mydir/count.txt";

//Read current value from count.txt
if(!($fp = fopen($count_file, "r"))) die ("Cannot Open $count_file.");
$count= fread($fp, 10);
fclose($fp);

//Increment count
$count++;

$display_page = "mydir/$count.htm";

//check if html file exists
if(file_exists($display_page))
&#123;

//Write new count value to count.txt and redirect to "$count".htm
if(!($fp = fopen($count_file, "w"))) die ("Cannot Open $count_file.");
fwrite($fp, $count);
fclose($fp);
header("Location: $display_page");
&#125;
else
&#123;

//If html file does not exist, reset count to 0 and refresh page
if(!($fp = fopen($count_file, "w"))) die ("Cannot Open $count_file.");
fwrite($fp, 0);
fclose($fp);
header("Location: count.php");
&#125;

?>
If you need any more help, don't hesitate to ask

Posted: Wed Jun 05, 2002 10:48 am
by ReBrand
Thanks a lot to both of you. I opted for the second one, though, because I'm very new to this and it looked nearer to what I want.

I tested it but I get a 404. I uploaded 4 test pages for it to find but it doesn't find them. Is it something to do with combining $count and '.htm'?

Posted: Wed Jun 05, 2002 11:28 am
by ozmodiar
I tested this script on my server and it worked. On my server:

count.txt
1.htm
2.htm
3.htm
4.htm

are all in the mydir directory.

count.php
pickone.htm

are in the same directory as the mydir folder.

If you want all these files in the same folder, you have to change the path from mydir/$count.htm to $count.htm and mydir/count.txt to count.txt in count.php.

Does this fix the problem?

Posted: Wed Jun 05, 2002 11:41 am
by ReBrand
Ah! That's almost working. After page 4 I get a 404, though. Then next time I get 1.htm.

Posted: Wed Jun 05, 2002 11:57 am
by ReBrand
Oops! Changed count.php to display.php and didn't change it in the script.

Works great now. Thanks!

Just wondering if it's an easy job to keep count.php (or display.php as it is now) showing in the address bar. I'd rather 1.htm or 7.htm wasn't visible if possible. Can the page I want to show somehow be included in count.php (or display.php) so that they don't see the name of the actual page selected?

Happy with that, though, if it can't. I really appreciate your help.

Posted: Wed Jun 05, 2002 12:58 pm
by ReBrand
Just wanted to add: what does 'Be careful!' mean? I have CHMODed the folder to 777 and count.txt to 666. Is that right or does this now mean someone can do something malicious?

Posted: Thu Jun 06, 2002 6:49 am
by ozmodiar
The permissions sound ok.

you can keep count.php in the address bar using frames. Rather than opening 1.htm in a new window, you could open it in another frame on count.php.

Let me know if you need any help doing this.

Posted: Thu Jun 06, 2002 7:44 am
by ReBrand
I'd rather not use frames if poss so I'm looking into a way of including the pages into count.php.

I'll play around with the code and see what I can come up with, although it works fine as it is so it isn't essential. I saw something elsewhere on this board about including files so I'll study that later.

Thanks again for your help here.

Posted: Thu Jun 06, 2002 11:19 am
by ozmodiar
I think this is what you're looking for. you just have to open the $count.htm page and echo the contents.

Count.php should look like this:

Code: Select all

<?php
$count_file="mydir/count.txt";

if(!($fp = fopen($count_file, "r"))) die ("Cannot Open $count_file.");
$count= fread($fp, 10);
fclose($fp);

$count++;

$display_page = "mydir/$count.htm";

if(file_exists($display_page))
&#123;
if(!($fp = fopen($count_file, "w"))) die ("Cannot Open $count_file.");
fwrite($fp, $count);
fclose($fp);


//Read in $count.htm and echo contents
if(!($fp = fopen($display_page, "r"))) die ("Cannot Open $display_page.");
$content= fread($fp, 10000);
echo $content;
fclose($fp);

&#125;
else
&#123;
if(!($fp = fopen($count_file, "w"))) die ("Cannot Open $count_file.");
fwrite($fp, 0);
fclose($fp);
header("Location: count.php");
&#125;

?>