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.
No of visits of a page
Moderator: General Moderators
- Kieran Huggins
- DevNet Master
- Posts: 3635
- Joined: Wed Dec 06, 2006 4:14 pm
- Location: Toronto, Canada
- Contact:
- Jonah Bron
- DevNet Master
- Posts: 2764
- Joined: Thu Mar 15, 2007 6:28 pm
- Location: Redding, California
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):
Hit PHP:
Hiters XML (users.xml):
Hiters PHP:
I recommend placing these in they're own seperate files, and using the include() function.
(note: you must have PHP5)[/syntax]
Hit xml (count.xml):
Code: Select all
<?xml version="1.0"?>
<counter>
<page>0</page>
</counter>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; ?>Code: Select all
<?xml version="1.0"?>
<usercount>
<users>4</users>
</usercount>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; ?>(note: you must have PHP5)[/syntax]
-
omidkamangar
- Forum Newbie
- Posts: 16
- Joined: Sun Jul 30, 2006 2:51 pm