Solved

Counter in PHP

Posted on 2001-08-22
6
257 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 

Author Comment

by:amara_78456
Comment Utility
Thank you
0
 

Author Comment

by:amara_78456
Comment Utility
Thank you
0
 

Expert Comment

by:SpideyMod
Comment Utility
Force Accepted

SpideyMod
Community Support Moderator @Experts Exchange
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Consider the following scenario: You are working on a website and make something great - something that lets the server work with information submitted by your users. This could be anything, from a simple guestbook to a e-Money solution. But what…
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…
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 …

743 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now