• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 317
  • Last Modified:

page visits counter

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
XK8ER
Asked:
XK8ER
1 Solution
 
Dushan De SilvaTechnology ArchitectCommented:
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
 
XK8ERAuthor Commented:
its just index.php where I need to count the unique visits..
0
 
Lukasz ChmielewskiCommented:
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
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
mattibuttCommented:
All you have to do is store user ip address date/time in a table then do the query for distinct ip address
0
 
Ray PaseurCommented:
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
 
XK8ERAuthor Commented:
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
 
Ray PaseurCommented:
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
 
XK8ERAuthor Commented:
the date its store like this: time()

so $yesterday = date('c', strtotime("Now - 24 hours"));
its not working
0
 
XK8ERAuthor Commented:
ok, I got it like this.. $yesterday = time()-3600*24;
0
 
XK8ERAuthor Commented:
how can I do something like this?


$sqlSelect = "SELECT * FROM counter WHERE dateTime >= '$hoursix' and $hourfive;
0
 
Ray PaseurCommented:
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
 
Ray PaseurCommented:
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
 
XK8ERAuthor Commented:
is there anyone that help me out here?
0
 
XK8ERAuthor Commented:
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
 
Ray PaseurCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now