Counter  

CPT 106 · Franklin College · Erich Prisner · 2002-2006

as an example of how to work with text files.

This webpage has been visited
Warning: fopen(number.txt) [function.fopen]: failed to open stream: Permission denied in /www/htdocs/w006e96f/CPT108/Php3.php on line 24

Warning: fgets(): supplied argument is not a valid stream resource in /www/htdocs/w006e96f/CPT108/Php3.php on line 25

Warning: rewind(): supplied argument is not a valid stream resource in /www/htdocs/w006e96f/CPT108/Php3.php on line 28

Warning: fputs(): supplied argument is not a valid stream resource in /www/htdocs/w006e96f/CPT108/Php3.php on line 29

Warning: fclose(): supplied argument is not a valid stream resource in /www/htdocs/w006e96f/CPT108/Php3.php on line 30
times so far.

your browser cannot display this hint on single steps

For a counter you need an additional file, a text file, let's call it "number.txt", which contains the number of visits so far. The PHP functions needed are

The function "fopen" generates a counter, which we name "$pointer" here, that points at the first character at the beginning. The modus could be "r" (read only), "r+" (read and write), "w" (write only), or "a" (append), for instance. The "fgets" function gives you the corresponding number of bytes, starting with your counter and moves the counter as many bytes to the back. "rewind($pointer)" puts the counter back to the beginning. "fputs($pointer,$counter)" writes the changed variable $counter on the position after the present $pointer position. It overwrites what was there before.


Erich Prisner, December 2003