Solved

page visits counter

Posted on 2011-02-22
15
304 Views
Last Modified: 2013-12-13
hello there,
I am wondering how can I make a simple counter that can display unique visits per 24hrs basis..
for example this is the web software that I am using: http://img219.imageshack.us/img219/9586/counterke.jpg
(01h) 1st hour, being always the current hour.. I like that but its a massive software..
I would like to create something like that but instead of the fancy graphs something very simple and with number instead..
how can I do that?
something like this I just need help with the PHP code


<table border="1" cellpadding="0" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" width="100%" id="AutoNumber1">
  <tr>
    <td width="100%" colspan="24">
    <p align="center">Real Time Counter Stats</td>
  </tr>
  <tr>
    <td width="4%" align="center">20</td>
    <td width="4%" align="center">205</td>
    <td width="4%" align="center">350</td>
    <td width="4%" align="center">450</td>
    <td width="4%" align="center">436</td>
    <td width="4%" align="center">389</td>
    <td width="4%" align="center">345</td>
    <td width="4%" align="center">332</td>
    <td width="4%" align="center">310</td>
    <td width="4%" align="center">280</td>
    <td width="4%" align="center">285</td>
    <td width="4%" align="center">295</td>
    <td width="4%" align="center">310</td>
    <td width="4%" align="center">311</td>
    <td width="4%" align="center">317</td>
    <td width="4%" align="center">248</td>
    <td width="4%" align="center">241</td>
    <td width="4%" align="center">230</td>
    <td width="4%" align="center">213</td>
    <td width="4%" align="center">246</td>
    <td width="5%" align="center">268</td>
    <td width="5%" align="center">285</td>
    <td width="5%" align="center">245</td>
    <td width="5%" align="center">262</td>
  </tr>
  <tr>
    <td width="4%">
    <p align="center">01h</td>
    <td width="4%">
    <p align="center">02h</td>
    <td width="4%">
    <p align="center">03h</td>
    <td width="4%">
    <p align="center">04h</td>
    <td width="4%">
    <p align="center">05h</td>
    <td width="4%">
    <p align="center">06h</td>
    <td width="4%">
    <p align="center">07h</td>
    <td width="4%">
    <p align="center">08h</td>
    <td width="4%" align="center">09h</td>
    <td width="4%" align="center">10h</td>
    <td width="4%" align="center">11h</td>
    <td width="4%" align="center">12h</td>
    <td width="4%" align="center">13h</td>
    <td width="4%" align="center">14h</td>
    <td width="4%" align="center">15h</td>
    <td width="4%" align="center">16h</td>
    <td width="4%" align="center">17h</td>
    <td width="4%" align="center">18h</td>
    <td width="4%" align="center">19h</td>
    <td width="4%" align="center">20h</td>
    <td width="5%" align="center">21h</td>
    <td width="5%" align="center">22h</td>
    <td width="5%" align="center">23h</td>
    <td width="5%" align="center">24h</td>
  </tr>
  <tr>
    <td width="100%" colspan="24">
    <p align="center">Total in Past 24hrs: 6873</td>
  </tr>
</table>

Open in new window

0
Comment
Question by:XK8ER
15 Comments
 
LVL 17

Expert Comment

by:Dushan De Silva
ID: 34957183
You may use sessions or getenv function on the required pages. Then write IP address with the time to text file on the server. Then use another script to read that text file and generate the statistics.  

<?php
session_start();

if(isset($_SESSION['views']))
$_SESSION['views']=$_SESSION['views']+1;
else
$_SESSION['views']=1;
echo "Views=". $_SESSION['views'];
?>


<?php
function getIPfromXForwarded() {
    $ipString=@getenv("HTTP_X_FORWARDED_FOR");
    $addr = explode(",",$ipString);
    return $addr[sizeof($addr)-1];
}
?>
0
 
LVL 1

Author Comment

by:XK8ER
ID: 34957296
its just index.php where I need to count the unique visits..
0
 
LVL 27

Expert Comment

by:Lukasz Chmielewski
ID: 34958480
Use cookies set to 24h time, and write the result to the database.
http://www.toxiclab.org/tutorial.asp?ID=192
If you need any additional help just ask.
0
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
LVL 11

Expert Comment

by:mattibutt
ID: 34959320
All you have to do is store user ip address date/time in a table then do the query for distinct ip address
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 34960256
I've done it the way mattibutt describes.  It's not very sophisticated and not perfectly accurate but meets all of my requirements.

But why bother writing code?  Why not use the industry standard?  Millions of highly professional web sites use this.  Since it is free and widely considered the best-of-breed, you would have to give me a lot of justification if you wanted me to use your code instead of Google's code!
http://www.google.com/analytics/
0
 
LVL 1

Author Comment

by:XK8ER
ID: 34961995
mattibutt, so create a mysql database like this will work? I added unique key so that it records unique ips stats..
how can I make sure that the records expire after 24hrs of being added?
CREATE TABLE `counter` (`ipAddress` int(11) NOT NULL,  `dateTime` time NOT NULL,  UNIQUE KEY `ipAddress` (`ipAddress`)) ;

Open in new window

0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 34962288
It's usually a good idea to avoid using MySQL reserved words for column names.

The IP address will usually be in $_SERVER["REMOTE_ADDR"] and it is a string, not an integer.  If you want to keep this as an integer it needs to be unsigned.

To make the records expire at 24 hours, you can do something like this...

$yesterday = date('c', strtotime("Now - 24 hours"));
$sql = "DELETE FROM hitCounter WHERE whenHit < '$yesterday' ";

Your INSERT queries will fail if the "ipAddress" column contains a duplicate.  To isolate these failures, so you can ignore them, test mysql_errno() for 1062.  Other failures probably need to be handled somehow.
CREATE TABLE `hitCounter` (`ipAddress` VARCHAR(16) NOT NULL DEFAULT '?',  `whenHit` DATETIME NOT NULL,  UNIQUE KEY `ipAddress` (`ipAddress`)) ;

Open in new window

0
 
LVL 1

Author Comment

by:XK8ER
ID: 34962576
the date its store like this: time()

so $yesterday = date('c', strtotime("Now - 24 hours"));
its not working
0
 
LVL 1

Author Comment

by:XK8ER
ID: 34962644
ok, I got it like this.. $yesterday = time()-3600*24;
0
 
LVL 1

Author Comment

by:XK8ER
ID: 34962722
how can I do something like this?


$sqlSelect = "SELECT * FROM counter WHERE dateTime >= '$hoursix' and $hourfive;
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 34963657
Regarding this: the date its store like this: time()

That is wrong.  Instead, store the date and time as a DATETIME column.  See the code snippet at http:#34962288 where I showed how to do this.  Use the ISO-8601 datetime string, which is date('c') in PHP.

This is much easier than you might think.  Take a while to read over this article and you will see the special meanings of certain terms in PHP.  It will really save you a lot of time and debugging effort if you use the built-in definitions.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_201-Handling-date-and-time-in-PHP-and-MySQL.html
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 34963690
Regarding this, $sqlSelect = "SELECT * FROM counter WHERE dateTime >= '$hoursix' and $hourfive;

After you read that article it probably will not be a question any more.  But if you're looking for a way to talk about 6 hours ago, the thing you want (ISO-8601) is this:

$hoursix = date('c', strtotime('Now - 6 hours'));

You can test strtotime() here on my web site. http://www.laprbass.com/RAY_strtotime.php

And all that said, I think I would still use Google Analytics.  It would be up and running for you by now!
0
 
LVL 1

Author Comment

by:XK8ER
ID: 34963937
is there anyone that help me out here?
0
 
LVL 1

Author Comment

by:XK8ER
ID: 34964036
ok I got it like this..


$sq2 = mysql_query("SELECT count(dateTime) as KTotal FROM counter WHERE dateTime BETWEEN '".$hr24."' AND '".$hr23."'", $connection);

Open in new window

0
 
LVL 109

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 34965215
You're still trying to use MySQL reserved words as column names?  Good grief, why?? Please read the first sentence of the post at ID:34962288.  If you're still wondering what the MySQL reserved words are, this might be able to help:
http://lmgtfy.com?q=MySQL+Reserved+Words

I don't know what further help I can give you at this point.  I have showed you how to create the table.  I have showed you how to run queries that are responsive to your interests.  I have showed you an article about how to handle date and time questions in PHP and MySQL.  I have even told you the professional solution would be to use Google Analytics instead of wasting a day of your life writing this code yourself.  What is still missing?  If this is not enough for you to get the page hit counters working you might want to hire a professional developer.  There is nothing wrong with acknowledging that time is money, and if you find you're wasting your time on something that doesn't make sense to you, well, a little money could get you back to more productive activities.

EE is a good place to get answers to your questions (and you have gotten that and more in this question).  But it is not a good place to get someone to write your code for you for free.  Best of luck with your project, ~Ray

0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

This article discusses how to create an extensible mechanism for linked drop downs.
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to count occurrences of each item in an array.
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.

786 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