php smart learner
Is there a possibility to create a html or a php page that will count redirects?
I mean, let's 开发者_如何学Gosay you have a page with a link in it. I want the page to count how many times the link is clicked per ip adress or username. The counting would be reported into a log file or text document.
You could use some web statistic software like Piwik.
You could also append the links so that the outlink is caught. I've included some basic code.
<a href="http://sitedomain.com/question/{url}">Link to be counted</a>
Then if you use a framework like CodeIgniter add this to your controller. add_outlink
would select a database and add a row with an ip address, the link, and the datestamp if you like.
function question($url)
{
$this->add_outlink($url);
header("Location: ".$url);
}
*. PHP solution. Create a function that will generate a link:
function getCounterLink($url)
{
if (urlLocal($url)) // no need to count the click
return $url;
return 'http://mysite.com/counter.php?url='.urlencode($url);
}
Use this function to add links in your templates:
<a href="<?php echo getCounterLink($url) ?>">Link</a>
Then create counter.php that will register the click and redirect user to the interested url.
*. JavaScript solution. If you are using Ajax, you can use JQuery and add onclick event for every link. When link is clicked, javascript makes ajax call to the counter on server. This variant can be considered as a workaround for the case when you have many templates already and do not want to modify them all.
First of all make a sql table to record;
CREATE TABLE `link_counter` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
`link` text COLLATE utf8_general_ci NOT NULL,
`ip` text COLLATE utf8_general_ci NOT NULL,
`times` int(11) NOT NULL DEFAULT '1',
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci;
than you must know to learn the ip adresses with php codes and make a global variable ip;
if (!empty($_SERVER['HTTP_CLIENT_IP']))
{
$ip=$_SERVER['HTTP_CLIENT_IP'];
}
else if (!empty($_SERVER['HTTP_X_FORWARDED_FOR']))
{
$ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
}
else
{
$ip=$_SERVER['REMOTE_ADDR'];
}
echo '<script type="text/javascript">
var ip='.$ip.';
</script>';
than make a script;
<script type="text/javascript">
function record(link)
{ //jquery
$.post("record.php",{ link: link , ip: ip}); //post link to record.php
location.href=link; //go to link
}
</script>
than make links like this ;
<a href="#" onclick="record('http://www.google.com')">GOOGLE</a>
and the last make a record.php ;
$link=$_POST['link'];
$cip=$_POST['ip'];
if ($link!='' && $cip!='')
{
$sorgu="select count(id),times,id from link_counter where link=".$link." and ip=".$cip;
$what=mysql_query($sorgu);
while ($isit=mysql_fetch_array($what))
{
$recorded=$isit['count(id)'];
$id=$isit['id'];;
if ($recorded>0)
{
$times= $isit['times'];
}
else
{
$times=0;
}
}
$add_times=$times+1;
if ($recorded>0)
{
$add="update link_counter set times='".$add_times."' where id='".$id."'";
$add_action=mysql_query($add);
}
else
{
$add="insert into link_counter times='".$add_times."' , link='".$link."' , ip='".$cip."'";
$add_action=mysql_query($add);
}
}
精彩评论