I did an Internet search but didn't find exactly what I was looking for. I did find an old tutorial, from which I've pasted the code below.
I need it to be session controlled and I only want it to count unique visitors. I want to use a database to keep track of the number of unique visitors to the website - not a particular page.
I use a functions.php page for my websites that contains the head and foot sections of the website:
Expand|Select|Wrap|Line Numbers
- <?php
- $page_title = "Home";
- include("functions.php");
- do_html_header($page_title);
- echo "<h1>$page_title</h1>
- Home page website stuff
- ";
- footer();
- ?>
Expand|Select|Wrap|Line Numbers
- <?php
- function do_html_header($page_title)
- {
- global $counter;
- include("includes/dbconnect.php");
- session_start();
- if (!isset($_SESSION[countme])) {
- $_SESSION[countme]="set";
- $sql="SELECT * FROM $c";
- $result = mysql_query($sql);
- $row = mysql_fetch_assoc($result);
- $count = $row['value'];
- //Increment Counter
- $count = $count + 1;
- //Update Database
- $sql="UPDATE $c SET value=".$count."";
- $result = mysql_query($sql);
- }
- echo "
- <!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.01 Transitional//EN' 'http://www.w3.org/TR/html4/loose.dtd'>
- <html>
- <head>
The counter table has these: cid, value.
This script may be old and outdated, which may be why it's not working. Anyone see a quick fix to this script or can point me in the direction of a better php, mysql, sessions counter script?
Thanks
DavidPr
** UPDATE **
It may be working after all. I started out with the number 7595 in the "value" column in the database table, now I have 7598. When I first installed the script and went to the website the value number did not increase in the database table. I don't know why there would be a delay in the number increase, but perhaps I jumped the gun on this.
But if anyone can see where this script could be improved I'd like to hear about it. Thanks, and sorry for the confusion.
DavidPr