Edgein |
15th September 2009 20:18 |
[FTS 1.1] userhits in profile
this mod will show the number of times a user's profile has been viewed, and by who.
First the database
PHP Code:
CREATE TABLE `userhits` ( `id` int(10) unsigned NOT NULL auto_increment, `userid` int(10) unsigned NOT NULL default '0', `hitid` int(10) unsigned NOT NULL default '0', `number` int(10) unsigned NOT NULL default '0', `added` datetime NOT NULL default '00-00-00 00:00:00', PRIMARY KEY (`id`) );
then add
PHP Code:
ALTER TABLE `users` ADD `hits` int(10) NOT NULL default '0';
then in userdetails.php find
PHP Code:
$id = 0 + $_GET["id"]; int_check($id,true); if($id != $CURUSER['id']) if($usergroups['canviewotherprofile'] != 'yes') ug(); $r = @sql_query("SELECT * FROM users WHERE id=".mysql_real_escape_string($id)) or sqlerr(__FILE__, __LINE__); $user = mysql_fetch_array($r) or bark(str11); if ($user["status"] == "pending") die;
and add after it
PHP Code:
if (!$_GET["hit"] && $CURUSER["id"] <> $user["id"]) { $res = mysql_query("SELECT added FROM userhits WHERE userid = $CURUSER[id] AND hitid = $id LIMIT 1") or sqlerr(); // *3 $row = mysql_fetch_row($res); // *3 if ($row[0] > get_date_time(gmtime() - 3600)) { // *3 header("Location: $BASEURL$_SERVER[REQUEST_URI]&hit=1"); // *3 } else { // *3 // $hitnumber = $userhits + 1; // *1 $hitnumber = $user["hits"] + 1; // *2 mysql_query("UPDATE users SET hits = hits + 1 WHERE id = $id") or sqlerr(); // *2 mysql_query("INSERT INTO userhits (userid, hitid, number, added) VALUES($CURUSER[id], $id, $hitnumber, '".get_date_time()."')") or sqlerr(); header("Location: $BASEURL$_SERVER[REQUEST_URI]&hit=1"); } // *3 }
then find in userdetails.php
PHP Code:
$ip_res = sql_query("SELECT * FROM ips WHERE userid = $id") or die(mysql_error());
print("<tr><td class=rowhead>".str47."</td><td align=left>"); while ($arr = mysql_fetch_assoc($ip_res)) { echo " $arr[ip] ::"; } print("</td></tr>\n");
And add above
PHP Code:
print("<tr><td class=rowhead>Profile views</td><td align=left><a href=userhits.php?id=$id>".number_format($user["hits"])."</a></td></tr>\n");
Create userhits.php:
PHP Code:
<? require_once("include/bittorrent.php"); dbconn(); loggedinorreturn();
$id = 0 + $_GET["id"];
if (!is_valid_id($id) || $CURUSER["id"] <> $id && get_user_class() < UC_MODERATOR) $id = $CURUSER["id"];
$res = mysql_query("SELECT COUNT(*) FROM userhits WHERE hitid = $id") or sqlerr(); $row = mysql_fetch_row($res); $count = $row[0]; $perpage = 100; list($pagertop, $pagerbottom, $limit) = pager($perpage, $count, "?id=$id&");
if (!$count) stderr("No views", "This user has had no profile views yet.");
$res = mysql_query("SELECT username FROM users WHERE id = $id") or sqlerr(); // remove 'hits' if you do NOT use the cleanup code $user = mysql_fetch_assoc($res);
stdhead("Profile views of $user[username]"); print("<h1>Profile views of <a href=\"userdetails.php?id=$id\">$user[username]</a></h1>\n"); print("<h2>In total $count views</h2>\n"); // replace $user[hits] with $count if you do NOT use the cleanup code if ($count > $perpage) print("$pagertop"); print("<table border=0 cellspacing=0 cellpadding=5>\n"); print("<tr><td class=colhead>Nr.</td><td class=colhead>Username</td><td class=colhead>Viewed at</td></tr>\n");
$res = mysql_query("SELECT uh.*, username, users.id as uid FROM userhits uh LEFT JOIN users ON uh.userid = users.id WHERE hitid = $id ORDER BY uh.id DESC") or sqlerr(); while ($arr = mysql_fetch_assoc($res)) print("<tr><td>".number_format($arr["number"])."</td><td><b><a href=\"userdetails.php?id=$arr[uid]\">$arr[username]</a></b></td><td>$arr[added]</td></tr>\n");
print("</table>\n"); if ($count > $perpage) print("$pagerbottom"); stdfoot(); ?>
good luck
grtzz edgein
Big thanx to the original makers :drink:
|