Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6901049
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T07:37:28+00:00 2026-05-27T07:37:28+00:00

I made a script a while ago that wrote to a file, I did

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T07:37:28+00:00Added an answer on May 27, 2026 at 7:37 am

    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");
    ?>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So I have a validation script I made a while ago. I can pass
I create backups using the same .bak file. I've made a script to automagically
I made a script that spawns a remote shell or runs a local shell
What happes when the script that made @mysql_connect($server, $user, $password) dies? How long does
While importing a python script from another script I want the script code that
I've made two versions of a script that submits a (https) web page form
I ran a script I made a couple of minutes ago and it still
I've made a simple bash script that need to keep it's super-user privileges throughout
i made a php script as below ran it and ouput is Record Added
Yesterday I made a small script with some help to read .csv files. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.