Guest User

Advanced Flat Php Counter Fix

a guest
Nov 13th, 2014
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. cnt.php
  2.  
  3. <?php
  4.   ##############################################################################
  5.  # Php Counter With Advanced Technology For The Prevention Of Reloading Pages #
  6.  # Version: 1.4 - Date: 13.11.2014 - Created By Alessandro Marinuzzi [Alecos] #
  7.  ##############################################################################
  8.  function cnt($file) {
  9.     session_start();
  10.     global $pagecnt;
  11.     $reloaded = isset($_SERVER['HTTP_CACHE_CONTROL']) && $_SERVER['HTTP_CACHE_CONTROL'] === 'max-age=0';
  12.     $thispage = basename($_SERVER['SCRIPT_FILENAME']);
  13.     if (!isset($_SESSION['first_go'])) {
  14.       $_SESSION['first_go'] = 1;
  15.       $first_go = TRUE;
  16.     } else {
  17.       $first_go = FALSE;
  18.     }
  19.     if (!isset($_SESSION['thispage'])) {
  20.       $_SESSION['thispage'] = $thispage;
  21.     }
  22.     if ($_SESSION['thispage'] != $thispage) {
  23.       $_SESSION['thispage'] = $thispage;
  24.       $new_page = TRUE;
  25.     } else {
  26.       $new_page = FALSE;
  27.     }
  28.     $pagecnt = rtrim(file_get_contents($file));
  29.     if ((!$reloaded) && ($new_page == TRUE) || ($first_go == TRUE)) {
  30.       $fd = fopen($file, 'w+');
  31.       flock($fd, LOCK_EX);
  32.       fwrite($fd, ++$pagecnt);
  33.       flock($fd, LOCK_UN);
  34.       fclose($fd);
  35.     }
  36.   }
  37. ?>
  38.  
  39. showcnt.php
  40.  
  41. <?php
  42.   ##############################################################################
  43.  # Show Counter Results - v.1.4 - 13.11.2014 By Alessandro Marinuzzi [Alecos] #
  44.  ##############################################################################
  45.  function gfxcnt($file) {
  46.     global $number;
  47.     $number = rtrim(file_get_contents($file));
  48.     $lenght = strlen($number);
  49.     $gfxcnt = "";
  50.     for ($i = 0; $i < $lenght; $i++) {
  51.       $gfxcnt .= $number[$i];
  52.     }
  53.     $gfxind = "<span class=\"counter\"><span class=\"number\">$gfxcnt</span></span>";
  54.     echo $gfxind;
  55.   }
  56. ?>
  57. #######################################
  58. ############## Under GPL ##############
  59. #######################################
  60.  
  61. Usage:
  62.  
  63. Put cnt.php and showcnt.php into a web root of your site inside a folder called "cnt". Then put at the top of your php web page (before calling other scripts) this piece of code:
  64.  
  65. <?php session_start(); include("cnt/cnt.php"); cnt("log/mypage.txt"); include("cnt/showcnt.php"); ?>
  66.  
  67. Then, where you like show the counter using this function in mypage.php:
  68.  
  69. <?php gfxcnt("log/mypage.txt"); ?>
  70.  
  71. Make sure you have in the root these folders: cnt, log. Otherwise you'll get an error... Have fun ;)
  72.  
  73. Now you can stylize your php counter using css and the css class "counter" and "number" ;)
Advertisement
Add Comment
Please, Sign In to add comment