请求帮助将 link 计数器(txt 文件)添加到 php 重定向 link 代码

Asking for help to add a link counter (txt file) to php redirect link code

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

link 重定向代码如下所示:

<?

$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 的文件中,然后我使用 links 就像 www.mysite.com/links.php?id=103 .当有人点击 link 时,代码会将他们定向到 http://wikileaks.org

现在我正在寻找的是在不使用 mysql.

的情况下计算不同 link 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);
    }
?>

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

补充一下。有什么问题?

<?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 文件中,读取它,修改它,然后再写入它。

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

<?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";
}
?>

我添加了锁定,所以两个实例不能同时read/write到同一个文件,给出一个损坏的文件!

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