2010-04-16 47 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); 
    } 
}