Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Counter in PHP

Posted on 2001-08-22
6
Medium Priority
?
271 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 30 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: 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.

Question has a verified solution.

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

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…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to dynamically set the form action using jQuery.
Suggested Courses

636 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