Go Back   Internet Business Forums > Design & Development

Reply
 
Thread Tools Display Modes
Old 24-10-2006, 03:24 PM   #1 (permalink)
Member
 
Ghezus's Avatar
 
Join Date: Oct 2006
Posts: 62
Send a message via ICQ to Ghezus Send a message via AIM to Ghezus Send a message via MSN to Ghezus Send a message via Yahoo to Ghezus
Default [SCRIPT] Visitor Counter

This counter counts the number of unique visitors by logging the IP's.

It uses 2 function's:

count() - Counts the user.

visits() - Shows the number of visits

I hope you know how to connect to a mysql database. But if you don't there is a tutorial somewhere that i can't link to.... due to some restriction's...

The table structure is following:

Code:
CREATE TABLE `counter` (
  `ip` varchar(15) NOT NULL default '',
  `number` int(11) NOT NULL default '0'
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
You can insert this using PHPMyAdmin or another simular tool.

The code:

PHP Code:
<?php


    
function count(){
        if(
mysql_result(mysql_query("SELECT COUNT(1) FROM counter WHERE ip='".$_SERVER['REMOTE_ADDR']."';"), 0) > 0){
            if(
mysql_query("UPDATE counter SET number=number+1 WHERE ip='".$_SERVER['REMOTE_ADDR']."';")){
                return 
true;
            }
        }else{
            if(
mysql_query("INSERT INTO counter SET number=1, ip='".$_SERVER['REMOTE_ADDR']."';")){
                return 
true;
            }
        }
        return 
false;        
    }
    
    function 
visits(){
        if(
$result mysql_query("SELECT COUNT(1) as unique, SUM(number) as total FROM count")){
            return 
mysql_fetch_assoc($result);
        }else{
            return array(
'unique' => 0'total' => 0);
        }
    }

?>
I didn't insert the connection code because I hope you can do that yourself by read the tutorial i linked to below.

Now you can count a visitor by adding:

PHP Code:
<?php count(); ?>
to a page. And you can call the counts with the array function visits();

PHP Code:
<?php

echo "<pre>";
print_r(visits());
echo 
"</pre>";

count();

echo 
"<pre>";
print_r(visits());
echo 
"</pre>";
?>
should display something like:

Code:
Array
(
    [unique] => 10
    [total] => 37
)

Array
(
    [unique] => 10
    [total] => 38
)
You can display it in a fancy way using simple PHP to display your count like this:

We have had 3 unique visit's and a total of 10 visit's.

You would use the code :

PHP Code:
<?php 
echo'We have had '.visits['unique'].' unique visit'and a total of '.visits['total'].' visit's. ';
?>
Ghezus is offline   Reply With Quote
Old 24-10-2006, 05:20 PM   #2 (permalink)
I am not *****
 
Join Date: Aug 2006
Location: Pakistan
Posts: 370
Default Re: [SCRIPT] Visitor Counter

Nice one . And helpful too. Its a nice tutorial for Newbies.

Quote:
I hope you know how to connect to a mysql database. But if you don't there is a tutorial somewhere that i can't link to.... due to some restriction's...
Yeah there is already a tutorial available . and heres the link to it:-
http://www.betlik.com/showthread.php?t=17

BTW, where are the restrictions?
-FR- is offline   Reply With Quote
Old 24-10-2006, 06:47 PM   #3 (permalink)
Member
 
Ghezus's Avatar
 
Join Date: Oct 2006
Posts: 62
Send a message via ICQ to Ghezus Send a message via AIM to Ghezus Send a message via MSN to Ghezus Send a message via Yahoo to Ghezus
Default Re: [SCRIPT] Visitor Counter

I can't post links well I can now because my postcount is higher...
Ghezus is offline   Reply With Quote
Old 24-10-2006, 07:14 PM   #4 (permalink)
I am not *****
 
Join Date: Aug 2006
Location: Pakistan
Posts: 370
Default Re: [SCRIPT] Visitor Counter

Yeah, that was turned ON for just 5 mins. Now users with 1 posts can also post links.
-FR- is offline   Reply With Quote
Old 11-11-2006, 04:58 PM   #5 (permalink)
Senior Member
 
Join Date: Nov 2006
Posts: 119
Default Re: [SCRIPT] Visitor Counter

great share .. and useful to me.. given rep to you ..

thx
DoomGuard is offline   Reply With Quote
Old 22-11-2006, 04:32 AM   #6 (permalink)
Senior Member
 
Join Date: Nov 2006
Posts: 225
Default Re: [SCRIPT] Visitor Counter

Hey army! first thanks for all the tutorials - i finished the first one, connecting to my MYsql successfully, yet now, when i try to make the new table in this tutorial i get this error:

SQL query:

CREATE TABLE `counter` (
`ip` varchar( 15 ) NOT NULL default '',
`number` int( 11 ) NOT NULL default '0'
) ENGINE = MYISAM DEFAULT CHARSET = latin1;

MySQL said: Documentation
#1064 - You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near 'ENGINE=MyISAM DEFAULT CHARSET=latin1' at line 4

Can you tell me what i did wrong?? thanks sooo much!
roy77
roy77 is offline   Reply With Quote
Old 22-11-2006, 04:14 PM   #7 (permalink)
co-admin
 
army's Avatar
 
Join Date: Oct 2006
Location: Belgium
Posts: 666
Default Re: [SCRIPT] Visitor Counter

Quote:
Originally Posted by roy77 View Post
Hey army! first thanks for all the tutorials - i finished the first one, connecting to my MYsql successfully, yet now, when i try to make the new table in this tutorial i get this error:

SQL query:

CREATE TABLE `counter` (
`ip` varchar( 15 ) NOT NULL default '',
`number` int( 11 ) NOT NULL default '0'
) ENGINE = MYISAM DEFAULT CHARSET = latin1;

MySQL said: Documentation
#1064 - You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near 'ENGINE=MyISAM DEFAULT CHARSET=latin1' at line 4

Can you tell me what i did wrong?? thanks sooo much!
roy77
I tried it myself on my phpMyAdmin, and it works fine ... no error at all.

Btw, this is Ghezus' tutorial .
army is offline   Reply With Quote
Old 23-11-2006, 01:15 AM   #8 (permalink)
Senior Member
 
Join Date: Nov 2006
Posts: 225
Default Re: [SCRIPT] Visitor Counter

lol yes i Realized it now, sorry Ghezus, thanks for the tutorial - ill try again.
thanks
roy77
roy77 is offline   Reply With Quote
Old 23-11-2006, 02:30 PM   #9 (permalink)
Junior Member
 
Join Date: Nov 2006
Posts: 27
Default Re: [SCRIPT] Visitor Counter

Nice Sharing, Is there any version for ASP.
Can it be possible with javascript with simple HTML coding.
munmun is offline   Reply With Quote
Old 23-11-2006, 10:06 PM   #10 (permalink)
co-admin
 
army's Avatar
 
Join Date: Oct 2006
Location: Belgium
Posts: 666
Default Re: [SCRIPT] Visitor Counter

Quote:
Originally Posted by munmun View Post
Nice Sharing, Is there any version for ASP.
Can it be possible with javascript with simple HTML coding.
As you can see here: http://www.betlik.com/need-script-ja...985.html?t=985

It is not possible in simple HTML/JavaScript source .

For an ASP counter, check here: http://www.betlik.com/script-asp-hit-counter-t1027.html
army is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Lord of the Rings saves TimeWarner Brian Turner Business Forum 3 31-12-2007 03:43 PM
Choosing business software? Brian Turner Business Forum 8 30-08-2004 10:48 AM
AdSense and spidering Brian Turner Search Engine Optimisation 1 07-06-2004 05:35 PM
Past my driving test today JamesSmith General 2 02-06-2004 04:51 PM
Exploding Whale hits Taiwan Brian Turner General 1 31-01-2004 01:14 PM


All times are GMT +1. The time now is 07:50 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.1.0 ©2007, Crawlability, Inc.