Solved

page visits counter

Posted on 2011-02-22
15
302 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:Dushan911
Comment Utility
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
Comment Utility
its just index.php where I need to count the unique visits..
0
 
LVL 27

Expert Comment

by:Lukasz Chmielewski
Comment Utility
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
 
LVL 11

Expert Comment

by:mattibutt
Comment Utility
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 108

Expert Comment

by:Ray Paseur
Comment Utility
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
Comment Utility
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 108

Expert Comment

by:Ray Paseur
Comment Utility
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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 1

Author Comment

by:XK8ER
Comment Utility
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
Comment Utility
ok, I got it like this.. $yesterday = time()-3600*24;
0
 
LVL 1

Author Comment

by:XK8ER
Comment Utility
how can I do something like this?


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

Expert Comment

by:Ray Paseur
Comment Utility
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 108

Expert Comment

by:Ray Paseur
Comment Utility
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
Comment Utility
is there anyone that help me out here?
0
 
LVL 1

Author Comment

by:XK8ER
Comment Utility
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 108

Accepted Solution

by:
Ray Paseur earned 500 total points
Comment Utility
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Showing your events from Google Calendar in Google Maps Why? I travel all week and I thought it would be ideal if staff in office knew where I was based on my calendar. (OK real reason: my son wanted to see where I would be working, and I thoug…
SASS allows you to treat your CSS code in a more OOP way. Let's have a look on how you can structure your code in order for it to be easily maintained and reused.
In this tutorial viewers will learn how to style transparent/translucent elements using alpha transparency in CSS Start with a normal styled element, such as a div.: Define its "background-color" property as "rgba (255, 255, 255, .5): The numbers in…
In this tutorial viewers will learn how to embed an audio file in a webpage using HTML5. Ensure your DOCTYPE declaration is set to HTML5: : The declaration should display (CODE) HTML5 is supported by the most recent versions of all major browsers…

771 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

12 Experts available now in Live!

Get 1:1 Help Now