Simple php counter code example
/*
- * Example 1: text file record data
- * Edit bbs.it-home.org
- */
- $ Counter = 1;
- If (file_exists ("mycounter.txt ")){
- $ Fp = fopen ("mycounter.txt", "r ");
- $ Counter = fgets ($ fp, 9 );
- $ Counter ++;
- Fclose ($ fp );
- }
- $ Fp = fopen ("mycounter.txt", "w ");
- Fputs ($ fp, $ counter );
- Fclose ($ fp );
- Echo "you are the". $ counter. "times to access this page! ";
// Example 2: simple database-based counters
- $ Conn = mysql_connect ("localhost", "root", "abc ");
- $ Result = mysql_query ("use db_counter ");
- $ Re = mysql_query ("select * from tb_counter ");
- $ Result = mysql_fetch_row ($ re );
- $ Counter = $ result [0];
- Echo "you are the {$ counter} visitor! ";
- $ Counter + = 1; echo "{$ counter }";
- Mysql_query ("update tb_counter set counter = $ counter ");
- Mysql_close ($ conn );
- // It is just a simple counter. you can add the function to prevent repeated refresh.
- ?>
|