2010-04-16 52 views
1

是否有可能创建一个html或php页面来计算重定向?php智能学习者

我的意思是,假设您有一个包含链接的页面。我希望页面能够统计每个IP地址或用户名点击链接的次数。计数将被报告到日志文件或文本文件中。

+1

alexa或谷歌分析 – Hanseh 2010-04-16 19:37:17

+0

我不确定标题 – Joe 2010-04-28 14:46:01

回答

0

你可以使用一些网络统计软件,如Piwik

您还可以附加链接,以便捕捉链接。我已经包含了一些基本代码。

<a href="http://sitedomain.com/question/{url}">Link to be counted</a> 

然后,如果你使用像CodeIgniter这样的框架将其添加到你的控制器。 add_outlink将选择一个数据库并添加一个包含ip地址,链接和日期戳记的行(如果您喜欢的话)。

function question($url) 
{ 
    $this->add_outlink($url); 

    header("Location: ".$url); 
} 
0

*。 PHP解决方案。创建将生成的链接的功能:

function getCounterLink($url) 
{ 
    if (urlLocal($url)) // no need to count the click 
     return $url; 
    return 'http://mysite.com/counter.php?url='.urlencode($url); 
} 

使用此功能可在模板中添加链接:

<a href="<?php echo getCounterLink($url) ?>">Link</a> 

然后创建counter.php将注册点击并将用户感兴趣的网址。

*。 JavaScript解决方案。如果您使用Ajax,则可以使用JQuery并为每个链接添加onclick事件。当点击链接时,javascript会对服务器上的计数器发出ajax调用。如果已经有很多模板并且不想全部修改它们,则可以将此变体视为解决方案。

0

首先让一个sql表进行记录;

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; 

比你必须知道的学习ip代码和php代码,并使全局变量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>'; 

比做脚本;

<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> 

比做这样的链接;

<a href="#" onclick="record('http://www.google.com')">GOOGLE</a> 

and the last make 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); 
    } 
}