Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

.NET 

[Snippet] Online users with MySql

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]

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 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]

Hopefully this'll help someone.
Looks nice!
And I also love the style you're using. it's Ragnarok blue right?
(Oct 20, 2012 07:16 PM)Dark_Noodles Wrote:  Looks nice!
And I also love the style you're using. it's Ragnarok blue right?

Yup, Ragnarok blue. I changed some colors to my liking though.
Aww you bastard. D:

I was doing the exact same thing, lol. Looks nice. :p
Thread: Backend Web-Dev. - by SomeWhiteGuy

WATCH OUT 'LIL HOE,
MY PARTNA STILL SMOKE ON PAROLE
selling -
megabits.org + freakin.pro
(Oct 20, 2012 07:20 PM)SomeWhiteGuy Wrote:  Aww you bastard. D:

I was doing the exact same thing, lol. Looks nice. :p

I would make a C# version(wouldn't be much different though + half of it is PHP so ._.) if I knew. I personally don't like coding in VB.Net that much, but this section is WAY more active than the C# one :(.
(Oct 20, 2012 07:23 PM)spye Wrote:  EWWW timer

learn to fucking code you spick

Ewww, -9 rep and nothing useful whatsoever..

Learn to:
1.) Use proper grammar & punctuation.
2.) Do something with your useless, no-point-in-living life.
3.) Not use racial slurs in everything; it doesn't make you cooler.
(Oct 20, 2012 07:21 PM)Pussy Cat Wrote:  I would make a C# version(wouldn't be much different though + half of it is PHP so ._.) if I knew. I personally don't like coding in VB.Net that much, but this section is WAY more active than the C# one :(.

Lol, I agree with you. The C# section does kinda dwindle in activity.
Thread: Backend Web-Dev. - by SomeWhiteGuy

WATCH OUT 'LIL HOE,
MY PARTNA STILL SMOKE ON PAROLE
selling -
megabits.org + freakin.pro
(Oct 20, 2012 07:33 PM)ᒪeumonic Wrote:  Ewww, -9 rep and nothing useful whatsoever..

Learn to:
1.) Use proper grammar & punctuation.
2.) Do something with your useless, no-point-in-living life.
3.) Not use racial slurs in everything; it doesn't make you cooler.

ololol. But seriously, good job. Maybe I missed em but I really don't see that many threads dealing with mysql in this section. Kinda surprised really. Wouldn't mind seeing more like this.
Well, thank you for this. I've been looking for something like this for a while.
Just noticed that the interval of the timer was set to 5000(5 seconds) but it was supposed to be 5 minutes. (facepalm)

Sorry about that, I wrote this at 3AM so that's probably the reason ._.
(Oct 20, 2012 06:59 PM)Pussy Cat Wrote:  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]

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 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]

Hopefully this'll help someone.

Bro Please Help Me i Need To Put This in My Application Please Ans me!
(Dec 9, 2012 04:35 PM)FsTuToRiaLs4u FeatureTeam Wrote:  Bro Please Help Me i Need To Put This in My Application Please Ans me!

you just had to quote a huge post to say something that stupid. Wow.


Possibly Related Threads...
Thread / Author Replies Views Last Post
[.NET] by еlixirSep 9, 2022 06:31 AM 0 132 Sep 9, 2022 06:31 AM
Last Post: еlixir
[Python] by datguy.devNov 28, 2019 10:37 PM 4 299 Nov 28, 2019 10:37 PM
Last Post: Eternal Darkness
[.NET] by FooterAug 17, 2019 12:02 PM 5 235 Aug 17, 2019 12:02 PM
Last Post: Fhack