Page 1 of 1

PHP Log every click

Posted: Fri Oct 21, 2011 5:46 am
by v3inTe
hello there,
im thinking of a Logger script for php..
what it does is,

If a user click a specific link like [Hello] or [Welcome] << Example Link
the IP of the User will be log in the database, including the time that he click the link..

and in admin side,
i can view the logs..
Format:
[time] [ip] Clicks [what link]

example:

[22:21:03] 123.044.21.192 Clicks Hello link.
[22:22:05] 192.168.65.200 Clicks Welcome link.

Thank you.

Re: PHP Log every click

Posted: Fri Oct 21, 2011 10:26 am
by John Cartwright
and what is your question?

Re: PHP Log every click

Posted: Fri Oct 21, 2011 5:01 pm
by v3inTe
i dont know how to code it..
can you help me with this?

thank you sir.

Re: PHP Log every click

Posted: Fri Oct 21, 2011 10:03 pm
by twinedev
My first question is to what purpose are you doing this, as the web server itself will be logging this information each page call anyhow. Depending on need, you may just be able to use data already logged there.

Then, if you still do need your own logging, the question comes, do you require it to log on the click, or are you good enough with logging it when the next page loads.

The first way, there are two methods, 1. AJAX calls back to the server , 2. Specific URL parameters to indicate it needs to log
If I was going this route, I would actually do both, set all links to be http://domain.com/page.php?logme=true then have javascript go though all links and if it ends in either ?logme=true or &logme=true (in case you have a link with other QS items) and strip it off. This way, if they have javscript enabled, it will use AJAX to log and end up at cleaner URLs, but if they have javascript disabled, they will get the URLs with the parameter to tell it to log.

The method of just logging when you hit a page just on every page load, you could do it to say as long as the referer ($_SERVER['HTTP_REFERER'] ) is the same site, consider it a link they clicked on. Note, this required it not to blocked.

-Greg

Re: PHP Log every click

Posted: Sat Oct 22, 2011 4:09 am
by martelo59
Hello, i have "spy" for users in one application, very easy.

Code: Select all

$url = (!empty($_SERVER['HTTPS'])) ? "https://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'] : "http://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
$today = date("d.m.Y H:i");
$ip = $_SERVER['REMOTE_ADDR'];
$enter = "$url|$today|$ip";
you just write this in database using user id; when you showing result you can use substr replace for good show.

Re: PHP Log every click

Posted: Sun Oct 23, 2011 5:45 am
by mikeashfield
Something along the lines of ...

Code: Select all

<?php
//Test if it is a shared client
if (!empty($_SERVER['HTTP_CLIENT_IP'])){
  $ip=$_SERVER['HTTP_CLIENT_IP'];
//Is it a proxy address
}elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])){
  $ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
}else{
  $ip=$_SERVER['REMOTE_ADDR'];
}
$con = mysql_connect("server_name/ip","username","password");
mysql_select_db("DB_NAME_HERE", $con);
$sql="INSERT INTO tbl_click (click_ip, click_referer)
VALUES
('$ip', '$_SERVER[HTTP_REFERER]')";

if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }
mysql_close($con)
?>
This along with a TIMESTAMP field in the same database with default value CURRENT_TIMESTAMP should do the trick just nicely.


To get the results, use something along these lines

Code: Select all

<?php
$con = mysql_connect("server_name/ip","username","password");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
mysql_select_db("DB_NAME_HERE", $con);
$result = mysql_query("SELECT click_ip, click_referrer, click_timestamp");
while($row = mysql_fetch_array($result))
  {
echo $row[2] . " ";
echo	$row[0]; . "Clicks ";
echo $row[1] . "Link</br>";
  }
echo "</div>";
mysql_close($con);
?>

I'm pretty new to PHP so please excuse me if any of this code is wrong/improper; I use a similar version of this code in my own project with no problems. :-)

Re: PHP Log every click

Posted: Sun Oct 23, 2011 7:37 pm
by v3inTe
Thank you for your help guys..
but how do i set a specific link only??
im planning to put it inside my staff area, so i know what links they're clicking.. i have other purpose of using this so please help me. :)

Re: PHP Log every click

Posted: Sun Oct 23, 2011 11:32 pm
by egg82
code I found after doing a quick google on ip validation. This will get a user's IP even if they're hiding behind a proxy.

Code: Select all

$real_ip = $_SERVER['REMOTE_ADDR'];
foreach (array('HTTP_CLIENT_IP', 'HTTP_X_FORWARDED_FOR', 'HTTP_X_FORWARDED', 'HTTP_X_CLUSTER_CLIENT_IP', 'HTTP_FORWARDED_FOR', 'HTTP_FORWARDED', 'REMOTE_ADDR') as $key) {
	if (array_key_exists($key, $_SERVER) === true) {
		foreach (explode(',', $_SERVER[$key]) as $ip) {
			if (filter_var($ip, FILTER_VALIDATE_IP) !== false) {
				$real_ip = $ip;
			}
		}
	}
}