【问题标题】:Asking for help to add a link counter (txt file) to php redirect link code寻求帮助以将链接计数器(txt 文件)添加到 php 重定向链接代码
【发布时间】:2015-11-09 15:54:31
【问题描述】:

我请求帮助为我的链接重定向代码添加一个计数器。

链接重定向代码如下所示:

<?

$id = preg_replace("/[^0-9]/","",$_GET['id']);

$x[101] = "http://www.ebay.com";
$x[102] = "http://www.google.com";
$x[103] = "http://wikileaks.org";
$x[104] = "http://potato.com";

if (isset($x[$id])){}
    else {
     die (header("Location: http://www.google.com"));}

header("Location: $x[$id]");
exit;
?>

该代码位于名为 link.php 的文件中,然后我使用诸如 www.mysite.com/links.php?id=103 之类的链接。当有人点击该链接时,代码会将他们定向到http://wikileaks.org

现在我正在寻找的是在不使用 mysql 的情况下计算不同链接 101 到 104 的点击次数。

也许是这样的? http://www.stevedawson.com/scripts/text-counter.php

<?php

    if (file_exists('count_file.txt')) 
    {
        $fil = fopen('count_file.txt', r);
        $dat = fread($fil, filesize('count_file.txt')); 
        echo $dat+1;
        fclose($fil);
        $fil = fopen('count_file.txt', w);
        fwrite($fil, $dat+1);
    }

    else
    {
        $fil = fopen('count_file.txt', w);
        fwrite($fil, 1);
        echo '1';
        fclose($fil);
    }
?>

我不知道如何将它添加到我的重定向代码中,并希望它适用于所有链接。计算每个链接,希望不要创建 100 个不同的文本文件,因为真实站点的链接比我上面的示例代码多得多。有什么想法吗?

【问题讨论】:

  • 您想计算每个网站有多少重定向,或者总共有多少?如果总共有很多,那么您添加的第二个 sn-p 应该可以工作。如果你想要每个链接有多少个,你应该有尽可能多的 txt 文件作为可能的链接,使用相同的代码。
  • 我想计算每个链接,它重定向了多少次。希望没有 100 个文本文件,我用于我的网站的文件有很多链接。

标签: php redirect counter


【解决方案1】:

如何将它添加到数组中,增加它并将数组序列化到文件中?

<?php
$id = preg_replace("/[^0-9]/","",$_GET['id']);

$x[101] = "http://www.ebay.com";
$x[102] = "http://www.google.com";
$x[103] = "http://wikileaks.org";
$x[104] = "http://potato.com";

// Open and lock file
$filename = 'count_file.dat';
$fil  = fopen($filename, 'c+');
while(!flock($fil, LOCK_EX)) 
    usleep(rand(1, 10000));

// Read data    
$dat = unserialize( fread($fil, filesize($filename) ) );
if ( !is_array( $dat ) )
    $dat = array();

// Count
if ( isset($dat[$id]) )
    $dat[$id]++;
else
    $dat[$id] = 1;

// Write and unlock file
fseek( $fil, 0 );
fwrite($fil, serialize($dat));
fflush($fil);
flock($fil, LOCK_UN);
fclose( $fil );

if (isset($x[$id])) {
    header("Location: $x[$id]");
} else {
     header("Location: http://www.google.com");
}

exit;

// Count hits
foreach( $x as $k => $v )
{
    if( $dat[$k] > 0 )
        echo 'Site ' . $v . ' has ' . $dat[$k] . ' hits!' . "<br>\n";
    else
        echo 'Site ' . $v . ' has no hits!' . "<br>\n";
}
?>

我添加了锁定,所以两个实例不能同时读取/写入同一个文件,导致文件损坏!

最终编辑工作版本,将命中保存到一个数组中,将它们序列化到一个文件中,并在下次读取该数组。最后还有一个点击显示示例。简化版!

【讨论】:

  • aww 没用。将它用于链接 103 两次,它在第一次和第二次之后说 a:1:{i:103;i:1;}。然后对于链接 102,它也更改为 a:1:{i:102;i:1;} 但在文件中仍然只保留一行
  • 感谢您的尝试,但 dat 文件只说 a:1:{i:103;i:1;} 唯一的链接是数字部分 (103),它更改为最后一个链接使用过。
  • 很抱歉,我不得不离开一段时间,但我已经简化了代码并对其进行了更新,使其功能齐全!
  • 太棒了。不知道你是如何打算重定向后的回声“计数”的,但我让它在一个单独的文件上工作,专门用于显示命中。 Stort tack Hasse :)
  • 点击计数只是一个例子!您需要读取文件,添加原始数组 ($x) 并显示内容。我注释掉了 header end exit 并得到了一个不错的调试输出。这种类型的“文件存储”不能很好地扩展,所以只能用于小型网站。删除数据文件以重置计数器!瓦塞戈 ;)
【解决方案2】:

只需添加它。有什么问题?

<?php

$id = preg_replace("/[^0-9]/","",$_GET['id']);

$x[101] = "http://www.ebay.com";
$x[102] = "http://www.google.com";
$x[103] = "http://wikileaks.org";
$x[104] = "http://potato.com";

if (isset($x[$id])){
  $filename = 'count_file_'.$id.'.txt';
}else{
  $filename = 'count_file_default.txt';
}
if (file_exists($filename)) 
{
    $fil = fopen($filename, r);
    $dat = fread($fil, filesize($filename)); 
    fclose($fil);
    $fil = fopen($filename, w);
    fwrite($fil, $dat+1);
}
else
{
    $fil = fopen($filename, w);
    fwrite($fil, 1);
    fclose($fil);
}

if (isset($x[$id])){}
    else {
     die (header("Location: http://www.google.com"));}

header("Location: $x[$id]");
exit;
?>

编辑:我刚刚看到您不想拥有那么多的文本文件。也许您最好的选择是将其存储在数据库中。或者也许将 json 存储在您的 txt 文件中,读取它,修改它,然后再次写入。

【讨论】:

  • 我希望计算每个链接,而不是总数。
  • 现在检查一下。这样一来,您将为每个链接创建一个计数文件,并使用一个默认值,以防这些链接都不是被重定向到的链接。
  • 正如我所说,您可以在单个 txt 文件中写入 json。但是你需要熟悉它。
猜你喜欢
  • 2011-02-02
  • 1970-01-01
  • 1970-01-01
  • 2023-04-01
  • 2016-04-28
  • 1970-01-01
  • 2019-05-15
  • 2011-01-01
  • 2011-05-14
相关资源
最近更新 更多