Simple php counter code example-php Tutorial

Source: Internet
Author: User
Simple php counter code example

  1. /*

  2. * Example 1: text file record data
  3. * Edit bbs.it-home.org
  4. */
  5. $ Counter = 1;
  6. If (file_exists ("mycounter.txt ")){
  7. $ Fp = fopen ("mycounter.txt", "r ");
  8. $ Counter = fgets ($ fp, 9 );
  9. $ Counter ++;
  10. Fclose ($ fp );
  11. }
  12. $ Fp = fopen ("mycounter.txt", "w ");
  13. Fputs ($ fp, $ counter );
  14. Fclose ($ fp );
  15. Echo "you are the". $ counter. "times to access this page! ";

  16. // Example 2: simple database-based counters

  17. $ Conn = mysql_connect ("localhost", "root", "abc ");
  18. $ Result = mysql_query ("use db_counter ");
  19. $ Re = mysql_query ("select * from tb_counter ");
  20. $ Result = mysql_fetch_row ($ re );
  21. $ Counter = $ result [0];
  22. Echo "you are the {$ counter} visitor! ";
  23. $ Counter + = 1; echo "{$ counter }";
  24. Mysql_query ("update tb_counter set counter = $ counter ");
  25. Mysql_close ($ conn );
  26. // It is just a simple counter. you can add the function to prevent repeated refresh.
  27. ?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.