p2p.wrox.com Forums

Need to download code?

View our list of code downloads.


  Return to Index  

beginning_php thread: Specifics on counter problems (per request)


Message #1 by "HollyAnn Carbino" <hcarbino@n...> on Fri, 10 May 2002 20:08:04
Nik,

Thanks for your speedy reply! I didn't realize that the thread would 
dissipate so quickly.

Specifically, I programmed the following:

<?php
//hit_counter1.php

$counter_file="./count.dat";
if(!($fp=fopen($counter_file,"r"))) die ("Cannot open $counter_file.");
$counter=(int) fread($fp,20);
fclose($fp);

$counter++;

echo "You're visitor number $counter.";

$fp = fopen($counter_file,"w");
fwrite($fp,$counter);
fclose($fp);
?>

As per the book, I created a blank "count.dat" file. The browser wouldn't 
access the file, so I checked the previous threads for answers. Ali stated 
that the permissions on the "count.dat" file required amendment. I found 
the permissions screen (Windows 2000) and made the file completely 
accessible by all. Voila! The counter worked, BUT...

Much to my dismay, the "count.dat" file disappeared from its folder 
without a trace after 2 browser requests for information. I am baffled! 
Can you help?

Thanks again,
~~HollyAnn~~
Message #2 by "Ch'anyom" <chanyom@n...> on Fri, 10 May 2002 15:20:44 -0400
This is a multi-part message in MIME format.

------=_NextPart_000_0009_01C1F836.40BC76E0
Content-Type: text/plain;
	charset="Windows-1252"
Content-Transfer-Encoding: quoted-printable

Wow, that is odd.

I'm not sure why it would disappear.  There is nothing in your code that 
would instruct a deletion. 

Have tried this a couple times?  I am curious to know if this is a 
reoccurring thing.

In peace,
Ch'anyom


----- Original Message -----
From: HollyAnn Carbino
To: beginning php
Sent: Friday, May 10, 2002 8:08 PM
Subject: [beginning_php] Specifics on counter problems (per request)


Nik,

Thanks for your speedy reply! I didn't realize that the thread would
dissipate so quickly.

Specifically, I programmed the following:

<?php
//hit_counter1.php

$counter_file=3D"./count.dat";
if(!($fp=3Dfopen($counter_file,"r"))) die ("Cannot open 
$counter_file.");
$counter=3D(int) fread($fp,20);
fclose($fp);

$counter++;

echo "You're visitor number $counter.";

$fp =3D fopen($counter_file,"w");
fwrite($fp,$counter);
fclose($fp);
?>

As per the book, I created a blank "count.dat" file. The browser 
wouldn't
access the file, so I checked the previous threads for answers. Ali 
stated
that the permissions on the "count.dat" file required amendment. I found 

the permissions screen (Windows 2000) and made the file completely
accessible by all. Voila! The counter worked, BUT...

Much to my dismay, the "count.dat" file disappeared from its folder
without a trace after 2 browser requests for information. I am baffled!
Can you help?

Thanks again,
~~HollyAnn~~



Message #3 by "HollyAnn Carbino" <hcarbino@n...> on Fri, 10 May 2002 20:41:01
Yes, as a matter of fact. I tried it nearly a dozen times because I 
couldn't believe my eyes. I've done everything except reboot the computer.

------=_NextPart_000_0009_01C1F836.40BC76E0
Content-Type: text/plain;
	charset="Windows-1252"
Content-Transfer-Encoding: quoted-printable

Wow, that is odd.

I'm not sure why it would disappear.  There is nothing in your code that 
would instruct a deletion. 

Have tried this a couple times?  I am curious to know if this is a 
reoccurring thing.

In peace,
Ch'anyom
Message #4 by "Ch'anyom" <chanyom@n...> on Fri, 10 May 2002 16:17:57 -0400
This is a multi-part message in MIME format.

------=_NextPart_000_000B_01C1F83E.3F42FCA0
Content-Type: text/plain;
	charset="Windows-1252"
Content-Transfer-Encoding: quoted-printable

Well then, reboot your computer.
(When all else fails, reboot.  If that fails, hit it with a hammer ;-P)

In peace,
Ch'anyom



----- Original Message -----
From: HollyAnn Carbino
To: beginning php
Sent: Friday, May 10, 2002 8:41 PM
Subject: [beginning_php] Re: Specifics on counter problems (per request)


Yes, as a matter of fact. I tried it nearly a dozen times because I
couldn't believe my eyes. I've done everything except reboot the 
computer.

------=3D_NextPart_000_0009_01C1F836.40BC76E0
Content-Type: text/plain;
charset=3D"Windows-1252"
Content-Transfer-Encoding: quoted-printable

Wow, that is odd.

I'm not sure why it would disappear.  There is nothing in your code that 
=3D
would instruct a deletion. =3D20

Have tried this a couple times?  I am curious to know if this is a =3D
reoccurring thing.

In peace,
Ch'anyom



Message #5 by "HollyAnn Carbino" <hcarbino@n...> on Fri, 10 May 2002 22:02:28
Just tried the reboot. It appeared to work properly until I refreshed. The 
counter didn't increment (the same problem I had before). So I changed the 
properties on the count.dat file. (Sounding familiar???) It worked twice 
(again) and poof! The file disappeared! I am trying my best not to get 
frustrated, but I can't continue with Chapter 10's examples unless I can 
get this to work. Any other suggestions welcome. Thanks for everything!
~~HollyAnn~~
Message #6 by "Nikolai Devereaux" <yomama@u...> on Fri, 10 May 2002 21:27:38 -0700
Hi HollyAnn,

I just tried your code on my Win 2000 machine and it worked fine (as long as
the count.dat file existed in the first place).

I did notice that your dir separator was '/'.  On a windows machine, it
should be '\'.  Try changing $counter_file to '.\count.dat'.  (the script
worked both ways for me, but I'm using apache... maybe that has something to
do with it.)

Oh, btw -- notice I used single-quotes.  Single-quoted strings are not
parsed the same way double-quoted strings are, so we don't need to escape
the backslash.

Anyway, that the file is deleted is really odd.  When you open the file with
fopen( ..., "w") (writing), you truncate the file to 0 bytes, but it doesn't
delete the file.  On the contrary, if the file does NOT exist, it creates
it.  Or attempts to, anyway.

I would suggest using some more debugging output.  I rewrote the script
somewhat to output some more descriptive messages in case things bomb.


Good luck,

Nik

-------------------------------------------------

<?php

$counter_file = '.\count.dat';
$counter      = 0;

if(file_exists($counter_file))
{
    if(! ($fp=fopen($counter_file,"r")))
    {
        exit("Error opening $counter_file for reading.\n");
    }

    if(filesize($counter_file) > 0)
    {
        $contents = fread($fp, filesize($counter_file));

        if($contents == '') // if (strlen($contents) == 0) works too
        {
            exit("Error reading $counter_file.\n");
        }
        1
        $counter = (int)$contents;
    }

    if(! fclose($fp))
    {
        exit("Error closing $counter_file after fopen for reading.\n");
    }
}

$counter++;

echo "You're visitor number $counter.\n";

if(! ($fp = fopen($counter_file, "w")))
{
    exit("Error opening $counter_file for writing.\n");
}

$written = fwrite($fp, "$counter");
if($written == -1)
{
    echo ("Error writing to $counter_file.\n");
}
else
{
    echo ("Wrote $written bytes to $counter_file.\n");
}
if(! fclose($fp))
{
    exit("Error closing $counter_file after fopen for writing.\n");
}

?>

Message #7 by "HollyAnn Carbino" <hcarbino@n...> on Mon, 13 May 2002 20:07:09
Nik,

Thanks for the rewrites. They have proven extremely useful.

The first time I ran it, I didn't create the count.dat file before. It 
didn't create the file when it ran either. So I created the count.dat 
file. The first message I got was:

"You're visitor number 1. Error opening .\count.dat for writing."

I checked the file folder and it was still there, so I changed the 
permissions on the file (I don't know how any other way than through 
Windows Explorer) and ran it again. I got this message:

"You're visitor number 1. Wrote 1 bytes to .\count.dat."

Yea! Hurrah! Score 1 for our side! When I refreshed, I got:

"You're visitor number 2. Error opening .\count.dat for writing."

I looked in the file folder and discovered the count.dat file was missing. 
I'm just as baffled as when I started...

Thanks again for all of your help!
~~HollyAnn~~
Message #8 by "Nikolai Devereaux" <yomama@u...> on Mon, 13 May 2002 18:58:02 -0700
Out of pure curiosity, what happens if we only use the fopen() calls for
writing?  Use file() for reading.

Try this rewrite:

<?php

$counter_file = '.\count.dat';
$counter      = 0;

if(file_exists($counter_file))
{
    echo "$counter_file exists, using file() to read it.\n"
    $lines = file($counter_file);

    echo "<pre>\$lines is: ";
    print_r($lines);
    echo "</pre>\n";

    $counter = (int)$lines[0];
}

$counter++;

echo "You're visitor number $counter.\n";

if(! ($fp = fopen($counter_file, "w")))
{
    exit("Error opening $counter_file for writing.\n");
}

$written = fwrite($fp, "$counter");
if($written == -1)
{
    echo ("Error writing to $counter_file.\n");
}
else
{
    echo ("Wrote $written bytes to $counter_file.\n");
}
if(! fclose($fp))
{
    exit("Error closing $counter_file after fopen for writing.\n");
}

?>

Message #9 by "HollyAnn Carbino" <hcarbino@n...> on Tue, 14 May 2002 13:51:12
I don't get anything on the browser page at all, just a blank white screen.
Message #10 by "HollyAnn Carbino" <hcarbino@n...> on Tue, 14 May 2002 14:15:02
Could the disappearances have anything to do with my having a firewall or 
antivirus capabilities? I refuse to remove them from my system as I have a 
cable modem and get attacked by anything and everything all day long.
Message #11 by "Nikolai Devereaux" <yomama@u...> on Tue, 14 May 2002 12:19:38 -0700
> I don't get anything on the browser page at all, just a blank
> white screen.

Hmm.. did you view the source?  somewhere the script is bombing, but I
thought we'd covered all the bases with debug output...

Message #12 by "Nikolai Devereaux" <yomama@u...> on Tue, 14 May 2002 12:20:54 -0700
> Could the disappearances have anything to do with my having a firewall or
> antivirus capabilities? I refuse to remove them from my system as
> I have a
> cable modem and get attacked by anything and everything all day long.

Nah, shouldn't.  After all, you're just requesting a page of your own
webserver, and PHP is doing the work.  I don't think that your AV software
can prevent Apache or PHP from doing their jobs... and if they did prevent
it, they'd let you konw about it -- either through an interactive alert or
at the least, an entry in a log file somewhere.

Nik

Message #13 by "HollyAnn Carbino" <hcarbino@n...> on Tue, 14 May 2002 22:07:02
> 
> I don't get anything on the browser page at all, just a blank
> white screen.

Hmm.. did you view the source?  somewhere the script is bombing, but I
thought we'd covered all the bases with debug output...

The source comes up the same every time:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META http-equiv=Content-Type content="text/html; charset=windows-
1252"></HEAD>
<BODY></BODY></HTML>

I'm not quite sure why the !DOCTYPE and META tags come up as they do. I 
assume that's part of the browser "translation".

  Return to Index