简单的站点统计脚本不从文件收集数据,我有一个几乎完全可用的脚本

发布于 2024-12-19 19:20:56 字数 2143 浏览 2 评论 0原文

我不久前制作了一个写入文件的脚本,我在这里做了同样的事情,只添加了读取文件并再次写入的部分。我想要实现的目标非常简单,但问题困扰着我,我试图让我的脚本写入一个基本上包含以下信息的文件

views:{viewcount}
date-last-visited:{MM/DD/YYYY}
last-ip:{IP-Adress}

现在我做了一些研究,并尝试了几种方法来读取数据,没有人返回任何东西。我当前的代码如下。

<?php
$filemade = 0;

if(!file_exists("stats")){
    if(!mkdir("stats")){
        exit();
    }
    $filemade = 1;
}
echo $filemade;
$hwrite = fopen("stats/statistics.txt", 'w');

$icount = 0;

if(filemade == 0){

    $data0 = file_get_contents("stats/statistics.txt");

    $data2 = explode("\n", $data0);

    $data1 = $data_1[0];

    $ccount = explode(":", data1);

    $icount = $ccount[1] + 1;

    echo "<br>icount:".$icount."<br>";
    echo "data1:".$data1."<br>";
    echo "ccount:".$ccount."<br>";
    echo "ccount[0]:".$ccount1[0]."<br>";
    echo "ccount[1]:".$ccount1[1]."<br>";

}

$date = getdate();

$ip=@$REMOTE_ADDR; 

fwrite($hwrite, "views:" . $icount . "\nlast-viewed:" . $date[5] . "/" . $date[3] . $date[2] . "/" . $date[6] . "\nlast-ip:" . $ip);

fclose($hwrite);
?>

结果总是:

views:1
last-viewed://
last-ip:

浏览量永远不会上升,日期永远不会生效,IP 地址也永远不会显示。

在最终决定询问之前,我查看了许多来源,我想通过这种方式可以获得更多相关信息。

期待一些回复。 PHP 是我最新的语言,所以我了解的不多。

我已经尝试过了。

我已经尝试过:

$handle_read = fopen("stats/statistics.txt", "r");//make a new file handle in read mode
$data = fgets($handle_read);//get first line
$data_array = explode(":", $data);//split first line by ":"
$current_count = $data_array[1];//get second item, the value

并且

$handle_read = fopen("stats/statistics.txt", "r");//make a new file handle in read mode
$pre_data = fread($handle_read, filesize($handle_read));//read all the file data
$pre_data_array = explode("\n", $pre_data);//split the file by lines
$data = pre_data_array[0];//get first line
$data_array = explode(":", $data);//split first line by ":"
$current_count = $data_array[1];//get second item, the value

我也尝试过 split 而不是explode,但我被告知 split 已被弃用,explode 是最新的。

任何帮助都会很棒,谢谢您的宝贵时间。

I made a script a while ago that wrote to a file, I did the same thing here, only added a part to read the file and write it again. What I am trying to achive is quite simple, but the problem is eluding me, I am trying to make my script write to a file basically holding the following information

views:{viewcount}
date-last-visited:{MM/DD/YYYY}
last-ip:{IP-Adress}

Now I have done a bit of research, and tried several methods to reading the data, none have returned anything. My current code is as follows.

<?php
$filemade = 0;

if(!file_exists("stats")){
    if(!mkdir("stats")){
        exit();
    }
    $filemade = 1;
}
echo $filemade;
$hwrite = fopen("stats/statistics.txt", 'w');

$icount = 0;

if(filemade == 0){

    $data0 = file_get_contents("stats/statistics.txt");

    $data2 = explode("\n", $data0);

    $data1 = $data_1[0];

    $ccount = explode(":", data1);

    $icount = $ccount[1] + 1;

    echo "<br>icount:".$icount."<br>";
    echo "data1:".$data1."<br>";
    echo "ccount:".$ccount."<br>";
    echo "ccount[0]:".$ccount1[0]."<br>";
    echo "ccount[1]:".$ccount1[1]."<br>";

}

$date = getdate();

$ip=@$REMOTE_ADDR; 

fwrite($hwrite, "views:" . $icount . "\nlast-viewed:" . $date[5] . "/" . $date[3] . $date[2] . "/" . $date[6] . "\nlast-ip:" . $ip);

fclose($hwrite);
?>

the result is always:

views:1
last-viewed://
last-ip:

the views never go up, the date never works, and the IP address never shows.

I have looked at many sources before finally deciding to ask, I figured I'd get more relevant information this way.

Looking forward to some replies. PHP is my newest language, and so I don't know much.

What I have tried.

I have tried:

$handle_read = fopen("stats/statistics.txt", "r");//make a new file handle in read mode
$data = fgets($handle_read);//get first line
$data_array = explode(":", $data);//split first line by ":"
$current_count = $data_array[1];//get second item, the value

and

$handle_read = fopen("stats/statistics.txt", "r");//make a new file handle in read mode
$pre_data = fread($handle_read, filesize($handle_read));//read all the file data
$pre_data_array = explode("\n", $pre_data);//split the file by lines
$data = pre_data_array[0];//get first line
$data_array = explode(":", $data);//split first line by ":"
$current_count = $data_array[1];//get second item, the value

I have also tried split instead of explode, but I was told split is deprecated and explode is up-to-date.

Any help would be great, thank you for your time.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

酒与心事 2024-12-26 19:20:56

请尝试以下操作:

<?php
if(!file_exists("stats")){
  if(!mkdir("stats")) die("Could not create folder");
}
// file() returns an array of file contents or false
$data = file("stats/statistics.txt", FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
if(!$data){
  if(!touch("stats/statistics.txt")) die("Could not create file");
  // Default Values
  $data = array("views:0", "date-last-visited:01/01/2000", "last-ip:0.0.0.0");
}
// Update the data
foreach($data as $key => $val){
  // Limit explode to 2 chunks because we could have
  // IPv6 Addrs (e.x ::1)
  $line = explode(':', $val, 2);
  switch($key){
    case 0:
      $line[1]++;
      break;
    case 1:
      $line[1] = date('m/d/Y');
      break;
    case 2:
      $line[1] = $_SERVER['REMOTE_ADDR'];
      break;
  }
  $data[$key] = implode(':', $line);
  echo $data[$key]. "<br />";
}
// Write the data back into the file
if(!file_put_contents("stats/statistics.txt", implode(PHP_EOL, $data))) die("Could not write file");
?>

Try the following:

<?php
if(!file_exists("stats")){
  if(!mkdir("stats")) die("Could not create folder");
}
// file() returns an array of file contents or false
$data = file("stats/statistics.txt", FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
if(!$data){
  if(!touch("stats/statistics.txt")) die("Could not create file");
  // Default Values
  $data = array("views:0", "date-last-visited:01/01/2000", "last-ip:0.0.0.0");
}
// Update the data
foreach($data as $key => $val){
  // Limit explode to 2 chunks because we could have
  // IPv6 Addrs (e.x ::1)
  $line = explode(':', $val, 2);
  switch($key){
    case 0:
      $line[1]++;
      break;
    case 1:
      $line[1] = date('m/d/Y');
      break;
    case 2:
      $line[1] = $_SERVER['REMOTE_ADDR'];
      break;
  }
  $data[$key] = implode(':', $line);
  echo $data[$key]. "<br />";
}
// Write the data back into the file
if(!file_put_contents("stats/statistics.txt", implode(PHP_EOL, $data))) die("Could not write file");
?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文