Solved

Counter in PHP

Posted on 2001-08-22
6
265 Views
Last Modified: 2012-06-21
How can we create the counter in PHP for counting the number of people who conneted to my server ?
0
Comment
Question by:amara_78456
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 1

Expert Comment

by:calumscott
ID: 6414070
The best way is probably to use a database to keep track of the number of people.  Simply keep a value in a database and increment it from your start page.

Use a database query to get the value back out of the DB and display it in whatever format you require.

If you need any help with this get back to me.
0
 

Expert Comment

by:TarikS
ID: 6423659
Another way if you do not want to use a database is to to use a function that every time your page is loaded will write to a file. This file has the current count and will increment it everytime the page is opened. If you need a code for that please reply to that message.
0
 
LVL 4

Accepted Solution

by:
heddesheimer earned 10 total points
ID: 6431009
Try this:
====================
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN">
<HTML>
<HEAD>
  <TITLE>PHP counter example</TITLE>
</HEAD>
<BODY>
  <H1>Welcome</H1>
  You are vistor number: <? counter() ?>
</BODY>
</HTML>

<?
function counter()
{
  $name = "counter.txt";
  $count = 0;
  // Get last counter value
  if (file_exists($name))
  {
    $file = fopen($name, "r");
    if ($file)
    {
      $count = fgets($file, 20);
      fclose($file);
    }
  }

  // increment and display current value
  $count = $count + 1;
  echo $count;

  // store the new value
  $file = fopen($name, "w");
  if ($file)
  {
    fputs($file, $count);
  } else {
    // if an error occurs here,
    // the write permisson must be set for the directory
    // in wich the script is installed (chmod 707)
    echo "<b>error:</b> cannot open file $name
         for writing<br>\n";
  }
  fclose($file);
}

?>
====================
hope that helps

Marian
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:amara_78456
ID: 6431383
Thank you
0
 

Author Comment

by:amara_78456
ID: 6431388
Thank you
0
 

Expert Comment

by:SpideyMod
ID: 8287164
Force Accepted

SpideyMod
Community Support Moderator @Experts Exchange
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question