Page 1 of 1

user(s) counter

Posted: Thu Mar 04, 2004 9:31 pm
by squalls_dreams
I need some help here..I have no clue how to do this.. alright this is the code for the counter but where amd I suppose to put it?or what exactly am I suppose to do? and how do I get the counter to show up on my site?

Code: Select all

<?php 
/* 
################################################################################################# 
# 
#  File name             : useronline.php 
#  Version               : 1.0 
#  Created By              : TeeJay 
#  Created on             : 07 Jan 2003 
#  e-mail                : teejay@webdesigning.demon.co.uk 
#  Copyright            : 2003 by Webdesigning Templates & Scripts 
#  Website                : http://webdesigning.demon.co.uk 
#  Description: 
#    It stores the user's IP and the page he/she is browsing and counts how 
#    many visitors are currently visiting that page, displaying the number... 
# 
################################################################################################# 

#  Start of Configuration  # 
################################################################################################# */ 
$sql_host         = "localhost";        //#    The sql server hostname, usualy "localhost" 
$sql_username     = "username";            //#    The username to the sql server 
$sql_password     = "password";            //#    The password to the sql server 
$sql_dbName     = "databse_name";            //# The database in question 

// CONNECT TO DATABASE // 
    $db_con = mysql_connect($sql_host, $sql_username, $sql_password)   
                OR die("<b><font color="red">Could not connect to MySQL server ($sql_host)...</b>\n<br>" . mysql_error() . "<br>Error @ line: " . __line__ . "</font>"); 
    $db_select = mysql_select_db($sql_dbName)   
                OR die("<b><font color="red">Could not connect to the DATABASE ($sql_dbName)...</b>\n<br>" . mysql_error() . "<br>Error @ line: " . __line__ . "</font>"); 
// END OF DATABASE CONNECTION // 
//========================================================================// 

$timeoutseconds     = 300;            //# Timeout value in seconds 

//#  End of Configuration  # 
//################################################################################################# 

$timestamp = time(); 
$timeout = ($timestamp-$timeoutseconds); 
    mysql_query("INSERT INTO useronline VALUES('$timestamp', '".$_SERVER&#1111;'REMOTE_ADDR']."', '".$_SERVER&#1111;'PHP_SELF']."')") or die("Useronline Database INSERT Error");   
    mysql_query("DELETE FROM useronline WHERE timestamp<$timeout") or die("Useronline Database DELETE Error"); 
    $result = mysql_query("SELECT DISTINCT ip FROM useronline WHERE file='$_SERVER&#1111;PHP_SELF]'") or die("Useronline Database SELECT Error"); 
    $user = mysql_num_rows($result); 
mysql_close(); 
if ($user==1) &#123; 
    echo "$user User online"; 
&#125;else &#123; 
    echo "$user Users online"; 
&#125; 
?>

Posted: Thu Mar 04, 2004 10:11 pm
by tsg
on you PHP pages .. where ever you want ...

Code: Select all

<?php
include "useronline.php ";

?>

Posted: Fri Mar 05, 2004 7:56 pm
by squalls_dreams
ok wait I got it working but I get an error that says DATABASE error

Posted: Sun Mar 07, 2004 1:00 pm
by JAM
Do you have access to a MySQL database?
If no; You need that.

Did you create a table of some sort in it?
If no; You need that also. Likely a table with timestamp (timestamp), ip (varchar(15)), and file (varchar(30)).

Posted: Sat Mar 13, 2004 2:18 pm
by squalls_dreams
ok I made a table called Useronline and I put in this.
what you said right..but it still wont work. I still get that error.

Posted: Sat Mar 13, 2004 10:13 pm
by allenmak
Hello, you better consult the datatype of each field for your script required. Maybe inconsistency of data type cause the error when inserting. :o