dsf323233323332 2015-11-09 15:54
浏览 51
已采纳

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

Im asking for help to add a counter for my link redirect code.

The link redirect code looks like this:

<?

$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;
?>

That code lies in a file called link.php, and then I use links like www.mysite.com/links.php?id=103 . When someone clicks that link the code will direct them to http://wikileaks.org.

Now what im looking for is to count the hits for the different links 101 to 104 without using mysql.

Maybe something like this? 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);
    }
?>

I have no idea how to add it to my redirect code, and also hopefully make it work for all the links. Counting each link, and hopefully not creating a 100 different text files, as the real site has lots more links than my example code above. Any ideas?

  • 写回答

2条回答 默认 最新

  • doufangyan6862 2015-11-09 16:11
    关注

    How about adding it to an array, increment it and serialize the array to a file?

    <?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>
    ";
        else
            echo 'Site ' . $v . ' has no hits!' . "<br>
    ";
    }
    ?>
    

    I have added locking, so two instances can not read/write to the same file at the same time, giving a corrupt file!

    Final Edit Working version which saves hits into an array, serializes them to a single file and reads the array back next time. Also a hits display example at the end. Simplified version!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 phython如何实现以下功能?查找同一用户名的消费金额合并—
  • ¥15 孟德尔随机化怎样画共定位分析图
  • ¥18 模拟电路问题解答有偿速度
  • ¥15 CST仿真别人的模型结果仿真结果S参数完全不对
  • ¥15 误删注册表文件致win10无法开启
  • ¥15 请问在阿里云服务器中怎么利用数据库制作网站
  • ¥60 ESP32怎么烧录自启动程序
  • ¥50 html2canvas超出滚动条不显示
  • ¥15 java业务性能问题求解(sql,业务设计相关)
  • ¥15 52810 尾椎c三个a 写蓝牙地址