simpel-user-stats-scriptje
Gesponsorde koppelingen
PHP script bestanden
::: proces.php :::
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
<?php
# Made by Mitch Vroege
# Distributed under GNU General Public License @ phphulp.nl
# Filename : proces.php
### !!! WARNING !!! DO NOT EDIT THIS FILE UNLESS YOU KNOW WHAT YOU ARE DOING !!! ###
# Include database connection file
include 'connect.php';
# Check whether the session has been set or not
if($_SESSION['onti'] && $_SESSION['idno'])
{
# Make sure the sessions aren't currupt, attempt to select from database to check
$sql1 = 'SELECT id FROM stats WHERE id = ' . $_SESSION['idno'] . ' AND ip = \'' . $_SERVER['REMOTE_ADDR'] . '\'';
$res1 = mysql_query($sql1);
# Check for a match
if(mysql_num_rows($res) > 0)
{
# Calculate online time in seconds
$time = time() - $_SESSION['onti'];
# Update table
$sql2 = 'UPDATE stats SET last = UNIX_TIMESTAMP(), views = views + 1, online = online + ' . $time . ' WHERE ip = \'' . $_SERVER['REMOTE_ADDR'] . '\'';
$res2 = mysql_query($sql2);
# Update session
$_SESSION['onti'] = time();
}
else
{
# Session data got currupt, unset them!
unset($_SESSION['onti'], $_SESSION['idno']);
}
}
else
{
# Attempt to select IP from database
$sql1 = 'SELECT id FROM stats WHERE ip = \'' . $_SERVER['REMOTE_ADDR'] . '\'';
$res1 = mysql_query($sql1);
# Check for a match
if(mysql_num_rows($res1) > 0)
{
# User is already present in database, update stats
$sql2 = 'UPDATE stats SET last = UNIX_TIMESTAMP(), views = views + 1 WHERE ip = \'' . $_SERVER['REMOTE_ADDR'] . '\'';
$res2 = mysql_query($sql2);
# Get id number from database
$data = mysql_fetch_array($res1);
# Set sessions
$_SESSION['onti'] = time();
$_SESSION['idno'] = $data['id'];
}
else
{
# Add new row for user
$sql2 = 'INSERT INTO stats (ip, first, last) VALUES (\'' . $_SERVER['REMOTE_ADDR'] . '\', UNIX_TIMESTAMP(), UNIX_TIMESTAMP())';
$res2 = mysql_query($sql2);
# Select id of newly added user
$sql3 = 'SELECT id FROM stats WHERE ip = \'' . $_SERVER['REMOTE_ADDR'] . '\'';
$res3 = mysql_query($sql3);
# Fetch it
$data = mysql_fetch_array($res3);
# Set sessions
$_SESSION['onti'] = time();
$_SESSION['idno'] = $data['id'];
}
}
?>
# Made by Mitch Vroege
# Distributed under GNU General Public License @ phphulp.nl
# Filename : proces.php
### !!! WARNING !!! DO NOT EDIT THIS FILE UNLESS YOU KNOW WHAT YOU ARE DOING !!! ###
# Include database connection file
include 'connect.php';
# Check whether the session has been set or not
if($_SESSION['onti'] && $_SESSION['idno'])
{
# Make sure the sessions aren't currupt, attempt to select from database to check
$sql1 = 'SELECT id FROM stats WHERE id = ' . $_SESSION['idno'] . ' AND ip = \'' . $_SERVER['REMOTE_ADDR'] . '\'';
$res1 = mysql_query($sql1);
# Check for a match
if(mysql_num_rows($res) > 0)
{
# Calculate online time in seconds
$time = time() - $_SESSION['onti'];
# Update table
$sql2 = 'UPDATE stats SET last = UNIX_TIMESTAMP(), views = views + 1, online = online + ' . $time . ' WHERE ip = \'' . $_SERVER['REMOTE_ADDR'] . '\'';
$res2 = mysql_query($sql2);
# Update session
$_SESSION['onti'] = time();
}
else
{
# Session data got currupt, unset them!
unset($_SESSION['onti'], $_SESSION['idno']);
}
}
else
{
# Attempt to select IP from database
$sql1 = 'SELECT id FROM stats WHERE ip = \'' . $_SERVER['REMOTE_ADDR'] . '\'';
$res1 = mysql_query($sql1);
# Check for a match
if(mysql_num_rows($res1) > 0)
{
# User is already present in database, update stats
$sql2 = 'UPDATE stats SET last = UNIX_TIMESTAMP(), views = views + 1 WHERE ip = \'' . $_SERVER['REMOTE_ADDR'] . '\'';
$res2 = mysql_query($sql2);
# Get id number from database
$data = mysql_fetch_array($res1);
# Set sessions
$_SESSION['onti'] = time();
$_SESSION['idno'] = $data['id'];
}
else
{
# Add new row for user
$sql2 = 'INSERT INTO stats (ip, first, last) VALUES (\'' . $_SERVER['REMOTE_ADDR'] . '\', UNIX_TIMESTAMP(), UNIX_TIMESTAMP())';
$res2 = mysql_query($sql2);
# Select id of newly added user
$sql3 = 'SELECT id FROM stats WHERE ip = \'' . $_SERVER['REMOTE_ADDR'] . '\'';
$res3 = mysql_query($sql3);
# Fetch it
$data = mysql_fetch_array($res3);
# Set sessions
$_SESSION['onti'] = time();
$_SESSION['idno'] = $data['id'];
}
}
?>
::: show.php :::
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
<?php
# Made by Mitch Vroege
# Distributed under GNU General Public License @ phphulp.nl
# Filename : show.php
### !!! WARNING !!! DO NOT EDIT THIS FILE UNLESS YOU KNOW WHAT YOU ARE DOING !!! ###
# Include database connection file
include 'connect.php';
# Select all visitors from database
$sql = 'SELECT id, ip, DATE_FORMAT(FROM_UNIXTIME(first), \'%H:%i %e/%c/%Y\') as firstt, DATE_FORMAT(FROM_UNIXTIME(last), \'%H:%i %e/%c/%Y\') as lastt, views, SEC_TO_TIME(online) as onlinet FROM stats ORDER BY id ASC';
$res = mysql_query($sql);
# Begin table to put data in
echo '<table>';
# First row, to describe the data in the columns
echo '<tr><td><b>#</b></td><td><b>IP address</b></td><td><b>First visit</b></td><td><b>Last visit</b></td><td><b>Pageviews</b></td><td><b>Online time</b></td></tr>';
# Loop, and put in table
while($data = mysql_fetch_array($res))
{
# Echo data, corresponding to columnnames echo'ed earlier
echo '<tr><td>' . $data['id'] . '</td><td>' . $data['ip'] . '</td><td>' . $data['firstt'] . '</td><td>' . $data['lastt'] . '</td><td>' . $data['views'] . '</td><td>' . $data['onlinet'] . '</td></tr>';
}
# End table
echo '</table>';
?>
# Made by Mitch Vroege
# Distributed under GNU General Public License @ phphulp.nl
# Filename : show.php
### !!! WARNING !!! DO NOT EDIT THIS FILE UNLESS YOU KNOW WHAT YOU ARE DOING !!! ###
# Include database connection file
include 'connect.php';
# Select all visitors from database
$sql = 'SELECT id, ip, DATE_FORMAT(FROM_UNIXTIME(first), \'%H:%i %e/%c/%Y\') as firstt, DATE_FORMAT(FROM_UNIXTIME(last), \'%H:%i %e/%c/%Y\') as lastt, views, SEC_TO_TIME(online) as onlinet FROM stats ORDER BY id ASC';
$res = mysql_query($sql);
# Begin table to put data in
echo '<table>';
# First row, to describe the data in the columns
echo '<tr><td><b>#</b></td><td><b>IP address</b></td><td><b>First visit</b></td><td><b>Last visit</b></td><td><b>Pageviews</b></td><td><b>Online time</b></td></tr>';
# Loop, and put in table
while($data = mysql_fetch_array($res))
{
# Echo data, corresponding to columnnames echo'ed earlier
echo '<tr><td>' . $data['id'] . '</td><td>' . $data['ip'] . '</td><td>' . $data['firstt'] . '</td><td>' . $data['lastt'] . '</td><td>' . $data['views'] . '</td><td>' . $data['onlinet'] . '</td></tr>';
}
# End table
echo '</table>';
?>
::: connect.php :::
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<?php
# Made by Mitch Vroege
# Distributed under GNU General Public License @ phphulp.nl
# Filename : connect.php
# Variables, edit them to your database data
$username = 'username';
$password = 'password';
$host = 'localhost'; // if your not sure aabout this one, leave it :)
$database = 'nameofyourdatabase';
### !!! WARNING !!! DO NOT EDIT BELOW THIS WARNING UNLESS YOU KNOW WHAT YOU ARE DOING !!! ###
# Make connection and select database
mysql_select_db($database, mysql_connect($host, $username, $password)) or die('Database connection failed!');
?>
# Made by Mitch Vroege
# Distributed under GNU General Public License @ phphulp.nl
# Filename : connect.php
# Variables, edit them to your database data
$username = 'username';
$password = 'password';
$host = 'localhost'; // if your not sure aabout this one, leave it :)
$database = 'nameofyourdatabase';
### !!! WARNING !!! DO NOT EDIT BELOW THIS WARNING UNLESS YOU KNOW WHAT YOU ARE DOING !!! ###
# Make connection and select database
mysql_select_db($database, mysql_connect($host, $username, $password)) or die('Database connection failed!');
?>