Solved

Counter in PHP

Posted on 2001-08-22
6
264 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
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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

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: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying 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

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

829 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