I've seen someone post a tutorial that showed how to do this with textfiles and without any checks at all, so I've made this to show how to do it the better way.
This'll show the users online in last 5 minutes (could be easily modified by changing two values, one in the PHP script and one in the VB class). Even if someone somehow got link to the script he couldn't just easily spam it and fuck up our counting because there's only one record per IP. With the data it's storing you could easily make this also show the total amount of users that ran the application, when they last ran it and things like that.
We'll need a table in our MySql database with two rows(one for IPs and one for date and time)
Sorry about the language, but I couldn't figure out how to change the phpMyAdmin to English >.>
![[Image: 1gZ3w]](https://puu.sh/1gZ3w)
Our PHP file
Make a new class
Sample usage (you would probably be using this with a label or something such as that, just put it on a label instead of writing it out with a console and the event will update the label whenever the amount of online users changes):
![[Image: 1gYmx]](https://puu.sh/1gYmx)
Hopefully this'll help someone.
This'll show the users online in last 5 minutes (could be easily modified by changing two values, one in the PHP script and one in the VB class). Even if someone somehow got link to the script he couldn't just easily spam it and fuck up our counting because there's only one record per IP. With the data it's storing you could easily make this also show the total amount of users that ran the application, when they last ran it and things like that.
We'll need a table in our MySql database with two rows(one for IPs and one for date and time)
Sorry about the language, but I couldn't figure out how to change the phpMyAdmin to English >.>
Our PHP file
PHP Code
<?php
$con = mysql_connect("<HOST GOES HERE>","<USERNAME GOES HERE>","<PASSWORD GOES HERE>") or die ('MySQL connection error.'.mysql_error());
mysql_select_db('<DATABASE NAME GOES HERE>', $con);
if(isset($_GET['update'])){
$q = mysql_query("INSERT INTO usersonline (ip, date) VALUES ('".$_SERVER['REMOTE_ADDR']."', NOW()) ON DUPLICATE KEY UPDATE date=NOW()",$con) or die ('MySQL connection error.'.mysql_error());
}
if(isset($_GET['check'])){
$q = mysql_query("SELECT ip FROM usersonline WHERE DATE_SUB(NOW(), INTERVAL 5 MINUTE) <= date",$con) or die ('MySQL connection error.'.mysql_error());
echo mysql_num_rows($q);
}
mysql_close($con);
?>Make a new class
Code
Public Class UserOnline
Private Const URL As String = "LINK TO YOUR PHP SCRIPT GOES HERE"
Public OnlineUsers As Integer = 0
Public Event OnlineUsersChanged(ByVal sender As System.Object, ByVal e As UserOnlineArgs)
Public Sub New()
Dim t As System.Timers.Timer = New System.Timers.Timer(5 * 60000)
AddHandler t.Elapsed, AddressOf Updater
t.Start()
End Sub
Private Sub Updater()
Dim n As Integer
Dim s As String
Try 'Just to make sure our whole application doesn't crash if there'll be any problems with the server
s = New System.Net.WebClient().DownloadString(URL & "?check&update")
Catch ex As System.Net.WebException
End Try
If Integer.TryParse(s, n) AndAlso Not n = OnlineUsers Then
RaiseEvent OnlineUsersChanged(Me, New UserOnlineArgs(n))
OnlineUsers = n
End If
End Sub
End Class
Public Class UserOnlineArgs : Inherits System.EventArgs
Public NewValue As Integer
Public Sub New(ByVal i As Integer)
NewValue = i
End Sub
End ClassSample usage (you would probably be using this with a label or something such as that, just put it on a label instead of writing it out with a console and the event will update the label whenever the amount of online users changes):
Hopefully this'll help someone.


