Jump to content

[SOLVED] Very simple counter


Asday

Recommended Posts

I have this code:

 

<?php
$fp=fopen("count.txt", "w");
fread($fp, $count194);
$count194++;
fwrite($fp, $count194);
echo $count194;
?>

 

Which is meant to count + 1 every time the page is accessed.  As I said, simple.

 

At the moment, I don't want to know if there's a better way to do it.  I just want to know why this goes wrong.

 

The count starts at 0, and goes up to 1, but then doesn't go any higher, even with cache-less refreshes.

 

Thanks for looking

Link to comment
Share on other sites

no loop or the variable values reset because the page maybe refresh

 

But I'm saving to a file, which stores the value for later use...

 

EDIT:  Just checked.  I changed the value in the file to 1651, and the echo still outputs 1.

Link to comment
Share on other sites

Try this:

<?php
$fp=fopen("count.txt", "w");
$count194=fread($fp, filesize("count.txt"));
$count194++;
fwrite($fp, $count194);
echo $count194;
?>

 

What does the bit you changed mean/meant to do.

 

Also, that doesn't work.  Thanks anyway!

 

EDIT: Ooh, editing posts?  cheating!

 

EDIT2:  Nope, that deletes the contents of the file.

Link to comment
Share on other sites

<?

  $filename = "some.dat" ;

  $dataFile = fopen( $filename, "r" ) ;

 

  if ( $dataFile )

  {

    while (!feof($dataFile))

    {

      $buffer = fgets($dataFile, 4096);

      echo $buffer;

  echo $ctr+=1;

    }

 

    fclose($dataFile);

  }

  else

  {

    die( "fopen failed for $filename" ) ;

  }

try that sample

Link to comment
Share on other sites

With fread the second parameter should be the number of bytes you want to read from the file and the value returned is the string read from the file or false on failure.

 

Let's have a go at some error checking:

<?php
if ($fp=fopen("count.txt", "r+")) {
  if ($count194=fread($fp, filesize("count.txt"))) {
    echo 'Counter before: '.$count194.'<br />';
    $count194++;
    fwrite($fp, $count194);
    echo 'Counter after: '.$count194;
  } else {echo 'Unable to read from file';}
} else {echo 'Unable to open the file';}
?>

 

This will tell you what it is reading from the file. I've also changed the open mode to "r+" which opens the file for reading and writing as you only had it open for writing only.

Link to comment
Share on other sites

<?

  $filename = "some.dat" ;

  $dataFile = fopen( $filename, "r" ) ;

 

  if ( $dataFile )

  {

    while (!feof($dataFile))

    {

      $buffer = fgets($dataFile, 4096);

      echo $buffer;

  echo $ctr+=1;

    }

 

    fclose($dataFile);

  }

  else

  {

    die( "fopen failed for $filename" ) ;

  }

try that sample

 

Well, that tried to count to infinity.  Thanks for that.

 

 

 

With fread the second parameter should be the number of bytes you want to read from the file and the value returned is the string read from the file or false on failure.

 

Let's have a go at some error checking:

<?php
if ($fp=fopen("count.txt", "r+")) {
  if ($count194=fread($fp, filesize("count.txt"))) {
    echo 'Counter before: '.$count194.'<br />';
    $count194++;
    fwrite($fp, $count194);
    echo 'Counter after: '.$count194;
  } else {echo 'Unable to read from file';}
} else {echo 'Unable to open the file';}
?>

 

This will tell you what it is reading from the file. I've also changed the open mode to "r+" which opens the file for reading and writing as you only had it open for writing only.

 

Just about to try that.

 

EDIT:  "Unable to read from file".  I commented out the reamaining "echo $count194;" simply because it was there, and I didn't know why.

Link to comment
Share on other sites

btw, having a file open for writing only and trying to read from it will result in nothing. Adding 1 to nothing will always be 1 which is why you were always getting 1.

 

It's also a good idea to get into the practice of closing the file when you've finished with it using fclose($fp);

 

By adding error checking you can also see where scripts go wrong easier plus it's the best way to handle errors. Another good habit to get into.

Link to comment
Share on other sites

oops ignore that - you're using Windoze - just read back!

 

As you're running from Windoze (lol) change the open mode from "r+" to "r+b" and try again.

 

I just read up on fread in the PHP manual and it says use "b" if running on a system that differentiates between text and binary files.

Link to comment
Share on other sites

Haha, forgot to set $count194!

 

<?php
$fp=fopen("count.txt", "r+b");
$count194=fread($fp, filesize("count.txt"));
$count194++;
fwrite($fp, $count194);
echo $count194;
?>

 

But...  That does something silly.

 

"1"

"2"

"13"

"1214"

"12131215"

"1213121412130000"

"1.21312141213E+31"

"1.21312141213E+63"

"0121312141213121512131214121300001.21312141213E+311.21312141213E+64"

"0121312141213121512131214121300001.21312141213E+311.21312141213E+630121312141213121512131214121300001.21312141213E+311.21312141213E+65"

 

Wtf?

Link to comment
Share on other sites

O_o

 

w00t, did it!

 

Spoke to a PHP genius at work (I'm on work ex) and he came up with the idea, open, read, close, unlink, increment, echo, touch, open, write.

 

r+b worked, in the end, thanks Yesideez.

 

On the site I use it on, I'll squeeze in credits, if I can.  It's only a counter, remember.  (^-^ )

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.