Scripts

Simpel user stats scriptje

Ik had ff geen zin om te vertalen, lijkt me wel duidelijk :) Made by Mitch Vroege Distributed under GNU General Public License @ phphulp.nl Filename : readme.txt Good, you've just got yourself a copy of my lil stats script! Now, what to do? First, run this SQL code on your MySQL database. CREATE TABLE stats ( ## Unique id number id int(11) not null auto_increment, ## IP address ip varchar(16) not null default '000.000.000.000', ## First visit, in seconds after epoch first int(20) not null default '0', ## Last visit, in seconds after epoch last int(20) not null default '0', ## Pageviews views int(6) not null default '1', ## Time spent on site in seconds online int(7) not null default '0', ## Set id to be primary key primary key(id) ) TYPE=MyISAM; Now, you are nearly done already ;) Check your files, make sure they have this line in them: session_start(); Otherwise the script won't work. To use the stats script, just put this line in every file: include 'proces.php'; To view the data, goto www.yoursite.com/show.php Well, that wasn't very hard, was it? HF & GL, SH4D3H :) Proces.php in actie Show.php in actie Download .rar Note : in het voorbeeld is 'session_start()' niet opgenomen, de online tijd zal dus niet werken ;) Daar heb ik dus gewo0n wat ingevuld ^^ Als ik weer eens wat tijd heb, breidt ik hem wel ff wat uit ofsow. En met enige PHP ervaring kun je het zelf ook wel voor bijvoorbeeld je profielen systeem aanpassen :)

simpel-user-stats-scriptje
[b]::: proces.php :::[/b]
<?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'];
	}
}
?>

[b]::: show.php :::[/b]
<?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>';
?>

[b]::: connect.php :::[/b]
<?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!');
?>

Reacties

0
Nog geen reacties.