No of visits of a page

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
omidkamangar
Forum Newbie
Posts: 16
Joined: Sun Jul 30, 2006 2:51 pm

No of visits of a page

Post by omidkamangar »

Hi,
I need to create a PHP script which stores the No of visits a person has made to my web site. I mean if someone visits my page now and then re-visits tomorrow he/she should see 2 as the counter. It is not depended on other visits between this two visits. I think I should do it this way:
Use Session and a database table.
The table has this columns :
ip -> the ip of the visitor
no_of_visits ->No of visits this IP has made to my page.

And whenever someone loads the page I increment the no_of_visits for the referring IP (or insert a new row in the table if it is his/her first visit).

But this has a problem and it is if several users are behind a proxy server and have the same IP the no of visits is for more than one user.
Does anyone know a better approach?
Thanks in advance.
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

Sessions store a cookie, there's no need to do some bizarre IP storage thing.

If you must store the value on the server for some reason, give them an auto-generated ID as a never-expiring cookie and store the number next to that in the DB.
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Post by Jonah Bron »

I made a counter that uses XML. It doesn't do exactly what you specified, but it is close. Every time the page is loaded, it counts 1 up in an xml file, and sets a cookie. If the cookie is not found, then it also counts 1 up in another xml file, but if there is a cookie found, it doesn't. The final output, is the number of hits, and the number of people that initiated thos hits:
Hit xml (count.xml):

Code: Select all

<?xml version="1.0"?>
<counter>
  <page>0</page>
</counter>
Hit PHP:

Code: Select all

<?php
//COUNTER
//New Document
$xml = new DOMDocument('1.0');
//Load XML
$xml->load('count.xml');
//format to true
$xml->formatOutput = true;
//set root element
$root = $xml->documentElement;
//set count element
$count = $root->firstChild;
//while there are nodes
while ($count) { 
//if element is home
    if (($count->nodeType == XML_ELEMENT_NODE) && ($count->nodeName == 'page')) {
    //count up on scale
      $count->firstChild->nodeValue +=1;
      //output count
      $output = $count->nodeValue;
    }
    //go to next counter
    $count = $count->nextSibling; 
}
//save file
$xml->save('count.xml');
//finish
$xml = $xml->saveXML();
?>
//This goes where you want to output the hit number
<?php echo $output; ?>
Hiters XML (users.xml):

Code: Select all

<?xml version="1.0"?>
<usercount>
  <users>4</users>
</usercount>
Hiters PHP:

Code: Select all

<?php
//HOME PAGE COUNTER
//New Document
$xml = new DOMDocument('1.0');
//Load XML
$xml->load('users.xml');
//format to true
$xml->formatOutput = true;
//set root element
$root = $xml->documentElement;
//set count element
$users = $root->firstChild;
//while there are nodes
while ($users) { 
//if element is home
    if (($users->nodeType == XML_ELEMENT_NODE) && ($users->nodeName == 'users')) { 
    //count up on scale
      if ($_COOKIE['jblog_visitor']!="jblog_visitor"){
        $users->firstChild->nodeValue +=1;
      }
      //output count
      $output = $users->nodeValue;
      //update cookie
      setcookie('jblog_visitor', 'jblog_visitor', time()+9999999999);
    }
    //go to next counter
    $users = $users->nextSibling; 
}
//save file
$xml->save('users.xml');
//finish
$xml = $xml->saveXML();
?>
//this goes where you want the hiters number
<?php echo $output; ?>
I recommend placing these in they're own seperate files, and using the include() function.
(note: you must have PHP5)[/syntax]
omidkamangar
Forum Newbie
Posts: 16
Joined: Sun Jul 30, 2006 2:51 pm

Post by omidkamangar »

Kieran Huggins wrote:If you must store the value on the server for some reason, give them an auto-generated ID as a never-expiring cookie and store the number next to that in the DB.
Thanks for the help. I did it well using your approach.
Post Reply