PDA

View Full Version : Users Online?


Andres
04-20-2001, 03:51 PM
Hello

I have a question about php. Is there anyone who knows where i can find a script that tells me how many visitors are on y site on that moment? Or how i can wright it?

Thanks!

graham
04-20-2001, 05:31 PM
This is a script that i found on phpbuilder.com. Here is the address:
http://www.phpbuilder.com/snippet/detail.php?type=snippet&id=134

You should check out the code library at phpbuilder you can usually find what your looking for.

<?php
/******

Online User Counter. 1.0

This script was created by Andreas Hillberg (andreas@hillberg.nu)
Please tell me that you are using my script, so I'll find a reason to release
other scripts I've made.

This script is a beta just yet, I am not 100% certain it will work in all curcumstainces.
Feel free to alter the code as you see fit, but if you do I would be glad that you
send me the altered code. So I'll learn more of this.

What it does is that it registers a PHP session with the ID of session and a time stamp.
It registers it to a MySQL database and later pulls out all entries and counts the
number of ID's that was registered 15 minutes ago.
You view the page again, and the MySQL will get updated.
Entries registered before 15 minutes ago will get deleted so it won't overload your database.

SET UP MYSQL LIKE THIS!!

create table YOURTABLE (
SID varchar(100) NOT NULL,
time varchar(15) NOT NULL,
day varchar (3) NOT NULL);

Config your settings below
*****/
$Session_name = "Your_session"; // The Sessions name, write "default" for default name.
$host = "localhost"; // Your host
$username = "username"; // Your MySQL username
$password = "password"; // Your MySQL password
$database = "database"; // Your Database of choice
$table = "table"; // Your Table of choice, ex. "online_users"

/***
Don't mess with the code below if you don't know what you're doing!
**/

// Starts Session
if ($Session_name == "default") {
session_start();
}
else {
session_name("$Session_name");
session_start("$Session_name");
}

$SID = session_id();
$time = time();
$dag = date("z");
$nu = time()-900;


//This connects to the MySQL server
mysql_connect ($host, $username, $password) OR DIE ("Could not connect to MySQL");
mysql_select_db($database) OR DIE ("Can't select database.");

// Check to see if the session_id is already registerd
$sidcheck = mysql_query("SELECT count(*) FROM $table WHERE SID='$SID'");
$sid_check = mysql_result($sidcheck,0);

if ($sid_check == "0") {
// If not, the session_id will be stored in MySQL
mysql_query("INSERT INTO $table VALUES ('$SID','$time','$dag')");
} else {
// If it is, it will register a new time to the session.
mysql_query("UPDATE $table SET time='$time' WHERE SID='$SID'");
}

// This is it, this counts the users currently online
$count_users = mysql_query("SELECT count(*) FROM $table WHERE time>$nu AND day=$dag");
$users_online = mysql_result($count_users,0);

// This deletes old ids, so your db will not get overloaded.

mysql_query("DELETE FROM $table WHERE time<$nu");
mysql_query("DELETE FROM $table WHERE day != $dag");

mysql_close();

if ($users_online == "1") {
echo "You are alone to view this page right now.\n";
}
else {
echo "There's $users_online people viewing this page right now.\n";
}

?>

Andres
04-20-2001, 05:36 PM
Thank you very much
I will test it now!

Thanks for the tip!!

winzor
04-20-2001, 06:08 PM
It was just updated at Hotscripts.
http://www.netone.at/no_prod_useronline.html

Fill out a form and they will email it to you.

kylecool
04-21-2001, 12:57 AM
I got that one, but it doesnt work right for me. Im going to try the other one.
KYLE

Andres
04-21-2001, 07:26 AM
Hey
I tested the one from Layer84 but that scripts looks not at that moment, but a everybody who was there within 15 minutes, so if you were there only 2 minutes you always wil be counted for 15 minutes,

Is there a better one??

graham
04-21-2001, 10:30 AM
this was in the comments:

"Entries registered before 15 minutes ago will get deleted so it won't overload your database."

kylecool
04-22-2001, 01:36 AM
I like this online thing. But, it only works if you goto that online page. I want it to be whos online at my whole site right now. The url is

http://www.kylecool.com/online/onlinenow.php

DO I have to insert some type of code on each page? Or, could the server just know? It works right, but on ly that page. I want my whole website. Anyone know how to do this?

KYLE

graham
04-22-2001, 08:27 AM
You may be able to do a include() function of the php file that displays who is online. Just put the code below on all your sites and that might work.

<?php
virtual('path/to/online.php');
?>

graham
04-22-2001, 08:28 AM
But if you want a different readout on each page you have then you will probably have to make multiple tables in your database for each page.

kylecool
04-22-2001, 01:03 PM
What if the other pages are *.html or *.shtml? HOw would I do that?

KYLE

graham
04-22-2001, 07:41 PM
convert the pages to .php. Just about anything you can do with SSI you can do with PHP. This is the same problem i had with my site and realized I could just include the file using a simple PHP command.

kylecool
04-22-2001, 08:01 PM
Originally posted by layer84
convert the pages to .php. Just about anything you can do with SSI you can do with PHP. This is the same problem i had with my site and realized I could just include the file using a simple PHP command. So you think I should re-code my whole site in php?

Kyle
P.S.-All i have to do it put php headers on it, whouldn't tak e too long, right? THen I could include php files easier.

graham
04-22-2001, 08:35 PM
Thats what i did to my site. I was using SSI for what I could have done easily in PHP. PHP also gives me room to expand and add more cool features to my site. But yes, it is pretty easy to convert your site to php if you have a basic understanding of HTML and PHP.

kylecool
04-22-2001, 10:32 PM
Originally posted by layer84
Thats what i did to my site. I was using SSI for what I could have done easily in PHP. PHP also gives me room to expand and add more cool features to my site. But yes, it is pretty easy to convert your site to php if you have a basic understanding of HTML and PHP.

I think I'll do that on my new layout. All I need to do is put <? php thing, its not that hard. Then, I can integrate the whos oline thing on all the pages, and everything would be better in the long run.

Thanks,
Kyle

eric418
04-23-2001, 10:55 PM
I'm using one too on my site.

http://leungeric.com.

I got it from uhome.net and just edit it a bit.

Damian
04-23-2001, 11:41 PM
Eric,
That's a sweet site you got there. The design/layout blows my mind as I'm not even HALF that creative. :)

kylecool
04-24-2001, 01:08 AM
Eric, you have a pretty cool website. But one question. WHat's with the you are using this browser... etc. on the first page? to redirect you to different pages depending on what browser? Not me, but some poeple probably wouldb't like that. I could care less.

Kyle Reilly

eric418
04-24-2001, 03:26 AM
wackyman4937:

thx...


kylecool:

actually thatz just a design.. it doesn't do much. one thing is that my page includes too much in the layout, and it takes quite long for ppl using modem connection, so index page makes ppl feel better as to reach the site first..