Zend - The PHP Company




Counters

Add Code


Named Counter for all pages  

Type: code fragment
Added by: lexandro
Entered: 06/02/2001
Last modified: 02/12/2000
Rating: - (fewer than 3 votes)
Views: 5875
This is a special counter routine. Simple add name for your counter and this code snippet stores all counter in one file. Use counter on your all pages and analyzing stats is very simple by on file.


<?php 
/*
  Lexike NamedCounter kezel�je phpben :)
*/
$counterfile "./ncounter.txt";
$countercontent = array();

function 
readcounterfile()
{
  global 
$counterfile$countercontent;
  
  
$tempcontent file ($counterfile);  
  foreach (
$tempcontent as $key => $value
  { 
    
// �t�rni
    
$value trim($value);
    
$location strpos($value"=");
    
$key trim(substr($value0$location 1));
    
$value trim(substr($value,$location+1strlen($value) - $location ) );
    
$countercontent[$key] = $value;
  } 
// foreach
  
return (0);
// readcounterfile


function writecounterfile()
{
  global 
$counterfile$countercontent;
  
$filehandler fopen ($counterfile"w");
  
  
reset ($countercontent);
  foreach (
$countercontent as $key => $value
  {
    
fwrite ($filehandler$key." = ".$value."n");
  } 
// fireach
  
fclose($filehandler);
  return (
0);
// writecounterfile


function setcounter ($countername$newvalue)
{
  global 
$countercontent;
  
 
  
readcounterfile();
  
$countercontent[$countername] = $newvalue;
  
writecounterfile();
  

  return (
$newvalue);
// setcounter

function getcounter($countername)
{
  global 
$counterfile$countercontent;
  
readcounterfile();  
  if (
in_array($counternamearray_keys($countercontent)))
  {
    return (
$countercontent[$countername]);
  }
  else
  {
    return (
$countercontent[$countername] = 0);
  }

// get_counter

function inccounter($countername)
{
  return (
setcounter($counternamegetcounter($countername)+1));
// inccounter

function deccounter($countername)
{
  return (
setcounter($counternamegetcounter($countername)-1));
// deccounter


?> 


Usage Example


deccounter() decrease
inccounter() increase
setcounter() setting initial value
getcounter() get counter value or -if not present-create a new counter


using:
setting $counterfile value is the
filename of counter.
$foo = inccounter("tester");
echo $tester."<br>";


the form of stored counters file:

name1 = value2
name2 = value2


Rate This Script





Search



This Category All Categories