Solved

Counter in PHP

Posted on 2001-08-22
6
259 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

These days socially coordinated efforts have turned into a critical requirement for enterprises.
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to count occurrences of each item in an array.
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.

920 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

17 Experts available now in Live!

Get 1:1 Help Now