Guest User

Advanced Flat Php Counter

a guest
Nov 13th, 2014
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.37 KB | None | 0 0
  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['thispage'])) {
  14.       $_SESSION['thispage'] = $thispage;
  15.     }
  16.     if ($_SESSION['thispage'] != $thispage) {
  17.       $_SESSION['thispage'] = $thispage;
  18.       $new_page = TRUE;
  19.     } else {
  20.       $new_page = FALSE;
  21.     }
  22.     $pagecnt = rtrim(file_get_contents($file));
  23.     if ((!$reloaded) && ($new_page == TRUE)) {
  24.       $fd = fopen($file, 'w+');
  25.       flock($fd, LOCK_EX);
  26.       fwrite($fd, ++$pagecnt);
  27.       flock($fd, LOCK_UN);
  28.       fclose($fd);
  29.     }
  30.   }
  31. ?>
  32.  
  33. showcnt.php
  34.  
  35. <?php
  36.   ##############################################################################
  37.  # Show Counter Results - v.1.4 - 13.11.2014 By Alessandro Marinuzzi [Alecos] #
  38.  ##############################################################################
  39.  function gfxcnt($file) {
  40.     global $number;
  41.     $number = rtrim(file_get_contents($file));
  42.     $lenght = strlen($number);
  43.     $gfxcnt = "";
  44.     for ($i = 0; $i < $lenght; $i++) {
  45.       $gfxcnt .= $number[$i];
  46.     }
  47.     $gfxind = "<span class=\"counter\"><span class=\"number\">$gfxcnt</span></span>";
  48.     echo $gfxind;
  49.   }
  50. ?>
  51. #######################################
  52. ############## Under GPL ##############
  53. #######################################
  54.  
  55. Usage:
  56.  
  57. 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:
  58.  
  59. <?php session_start(); include("cnt/cnt.php"); cnt("log/mypage.txt"); include("cnt/showcnt.php"); ?>
  60.  
  61. Then, where you like show the counter using this function in mypage.php:
  62.  
  63. <?php gfxcnt("log/mypage.txt"); ?>
  64.  
  65. Make sure you have in the root these folders: cnt, log. Otherwise you'll get an error... Have fun ;)
  66.  
  67. 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