[Home]  [Edit this page]  [Recent Changes]  [Special Pages]  [Help
PHPAdvanceCode

Advanced PHP Programming

--------------------------------------------------

This tutorial covers advanced issues of PHP programming. It assumes that you know at least how to deal with files in PHP.

Writing a Counter for your Homepage

One of the easiest things to do with PHP is, writing a counter for your homepage.

 //the filename for storing the counter 
$filename = "guestbook.txt";
 //our counter variable 
$counter = 0;
 //checking if the file exists. if so, write the counter into it
if(file_exists($filename))
{
  $file = fopen($filename,"r");
   if($file)
    {
      $counter = fgets($file,255);
      fclose($file);
    }
  
}
 //the page has been updated, so increase the counter 
$counter++;
$file = fopen($filename,"w");
if($file)
{
  fputs($filename,"w");
  fclose($file);
}
//Here comes the HTML part
<html>
<head><title></title></head>
<body>
<? 
echo "You are visitor number: $counter on this page";
?>
</body>
</html>


This script is rather straightforward. We store the counter variable in a file, "guestbook.txt" and increase the variable each time the site is updated or loaded again. The HTML part needs no further explanation. We simply print the value of the $counter variable on the screen. Thats it.

last edited (June 7, 2005) by ITA, Number of views: 6399, Current Rev: 8 (Diff)

[Edit this page]  [Page history]  [What links here]  [Discuss this topic]  [Printer Friendly]  

Members

Username:

Password:


Register
Forgot Password?




Programmers Heaven - for .NET, Java, C/C++ and WEB Developers!
© 1996-2008 Community Networks Ltd. All rights reserved. Reproduction in whole or in part, in any form or medium without express written permission is prohibited. Violators of this policy may be subject to legal action. Please read Terms Of Use and Privacy Statement for more information. Development by Tore Nestenius at .NET Consultant - Synchron Data.