f



Failed to write to a text file (text file is RW)

Hi,

I dig up the following script.  Added a simple HTML file with a form
field named "vote" and have it send to the following php script.
However, it failed to write to the poll_result.txt file.  Why?

Thanks as usual.

<?php
$vote = $_REQUEST['vote'];

//get content of textfile
$filename = "poll_result.txt";
$content = file($filename);

//put content in array
$array = explode("||", $content[0]);
$yes = $array[0];
$no = $array[1];

if ($vote == 1)
  {
  $yes = $yes + 1;
  }
if ($vote == 0)
  {
  $no = $no + 1;
  }


// debug
echo $yes;
echo $no;

// output and comments
/*
Seed poll_result.txt to read  0||0

It's always 10 even though I checked the YES option several times
obviously it failed to write to the poll_result.txt file.

Checked the poll_result.txt, it's 0||0

*/


//insert votes to txt file
$insertvote = $yes."||".$no;
$fp = fopen($filename,"w");
fputs($fp,$insertvote);
fclose($fp);
?>
0
donli (155)
1/2/2011 8:19:06 PM
comp.lang.php 32646 articles. 0 followers. Post Follow

29 Replies
1462 Views

Similar Articles

[PageSpeed] 57

On 1/2/2011 3:19 PM, justaguy wrote:
> Hi,
>
> I dig up the following script.  Added a simple HTML file with a form
> field named "vote" and have it send to the following php script.
> However, it failed to write to the poll_result.txt file.  Why?
>
> Thanks as usual.
>
> <?php
> $vote = $_REQUEST['vote'];
>
> //get content of textfile
> $filename = "poll_result.txt";
> $content = file($filename);
>
> //put content in array
> $array = explode("||", $content[0]);
> $yes = $array[0];
> $no = $array[1];
>
> if ($vote == 1)
>    {
>    $yes = $yes + 1;
>    }
> if ($vote == 0)
>    {
>    $no = $no + 1;
>    }
>
>
> // debug
> echo $yes;
> echo $no;
>
> // output and comments
> /*
> Seed poll_result.txt to read  0||0
>
> It's always 10 even though I checked the YES option several times
> obviously it failed to write to the poll_result.txt file.
>
> Checked the poll_result.txt, it's 0||0
>
> */
>
>
> //insert votes to txt file
> $insertvote = $yes."||".$no;
> $fp = fopen($filename,"w");
> fputs($fp,$insertvote);
> fclose($fp);
> ?>

Are you sure you're looking at the same file you're writing to?  You're 
using a relative path, after all (which would place the data file in 
your web path, where it could be downloaded by anyone - not a good idea).

Does the user (if this is running under the web server, the web server 
user) have permission to write to the file?

And finally, this is totally unsafe.  There is nothing stopping two 
different people from opening the file at the same time, potentially 
corrupting your date.

-- 
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
0
jstucklex (14659)
1/2/2011 8:35:14 PM
On Jan 2, 3:35=A0pm, Jerry Stuckle <jstuck...@attglobal.net> wrote:
> On 1/2/2011 3:19 PM, justaguy wrote:
>
>
>
> > Hi,
>
> > I dig up the following script. =A0Added a simple HTML file with a form
> > field named "vote" and have it send to the following php script.
> > However, it failed to write to the poll_result.txt file. =A0Why?
>
> > Thanks as usual.
>
> > <?php
> > $vote =3D $_REQUEST['vote'];
>
> > //get content of textfile
> > $filename =3D "poll_result.txt";
> > $content =3D file($filename);
>
> > //put content in array
> > $array =3D explode("||", $content[0]);
> > $yes =3D $array[0];
> > $no =3D $array[1];
>
> > if ($vote =3D=3D 1)
> > =A0 =A0{
> > =A0 =A0$yes =3D $yes + 1;
> > =A0 =A0}
> > if ($vote =3D=3D 0)
> > =A0 =A0{
> > =A0 =A0$no =3D $no + 1;
> > =A0 =A0}
>
> > // debug
> > echo $yes;
> > echo $no;
>
> > // output and comments
> > /*
> > Seed poll_result.txt to read =A00||0
>
> > It's always 10 even though I checked the YES option several times
> > obviously it failed to write to the poll_result.txt file.
>
> > Checked the poll_result.txt, it's 0||0
>
> > */
>
> > //insert votes to txt file
> > $insertvote =3D $yes."||".$no;
> > $fp =3D fopen($filename,"w");
> > fputs($fp,$insertvote);
> > fclose($fp);
> > ?>
>
> Are you sure you're looking at the same file you're writing to? =A0You're
> using a relative path, after all (which would place the data file in
> your web path, where it could be downloaded by anyone - not a good idea).
>
> Does the user (if this is running under the web server, the web server
> user) have permission to write to the file?
>
> And finally, this is totally unsafe. =A0There is nothing stopping two
> different people from opening the file at the same time, potentially
> corrupting your date.
>
> --
> =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
> Remove the "x" from my email address
> Jerry Stuckle
> JDS Computer Training Corp.
> jstuck...@attglobal.net
> =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D

Well, on "where it could be downloaded by anyone", how come?  The
processPoll.php file or a similar process php script is a server
script,
average user can't mess up with it.

On file permission, sorry, I forgot to mention, it's Read and Write.
A similar php script executes fine.

On your last point of " this is totally unsafe.  There is nothing
stopping two
> different people from opening the file at the same time, potentially
> corrupting your date."
Once we fix the above two problems what could we do about it?  I'm
totally new to php btw.

Many thanks.


0
donli (155)
1/2/2011 9:07:50 PM
On 02/01/11 20:19, justaguy wrote:

> I dig up the following script.  Added a simple HTML file with a form
> field named "vote" and have it send to the following php script.
> However, it failed to write to the poll_result.txt file.  Why?

The following seems to work for me.

<?php
$filename = "poll_result.txt";
if (isset($_POST['vote'])) {
  $vote = intval($_POST['vote']); // get this vote
  $content = file($filename); // read result file
  $array = explode("||", $content[0]);
  $yes = intval($array[0]); // get stored results
  $no = intval($array[1]);
  if ($vote == 1) $yes++; // increment one
  if ($vote == 0) $no++;
  $insertvote = $yes."||".$no; // new result string
  }
else {
  $insertvote = "0||0\n"; // reset result string
  $yes = 0;
  $no = 0;
  }
$fp = fopen($filename,"w"); // store results
fputs($fp,$insertvote);
fclose($fp);
echo <<<EOT
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Yes-No Poll</title>
</head>
<body style="padding:1em 3em">
<form method="post" action="quickpoll.php" name="f1" id="f1">
<p>Yes: <input type="radio" name="vote" id="v1_1" value="1"><br>
No: <input type="radio" name="vote" id="v1_0" value="0"><br>
<input type="submit" value="Vote Now" name="s1" id="s1"></p>
</form>
<p>The results so far:<br>
Yes: $yes votes<br>
No: $no votes</p>
</body>
</html>
EOT;
?>

Rgds

Denis McMahon
0
1/2/2011 10:00:08 PM
On 1/2/2011 4:07 PM, justaguy wrote:
> On Jan 2, 3:35 pm, Jerry Stuckle<jstuck...@attglobal.net>  wrote:
>> On 1/2/2011 3:19 PM, justaguy wrote:
>>
>>
>>
>>> Hi,
>>
>>> I dig up the following script.  Added a simple HTML file with a form
>>> field named "vote" and have it send to the following php script.
>>> However, it failed to write to the poll_result.txt file.  Why?
>>
>>> Thanks as usual.
>>
>>> <?php
>>> $vote = $_REQUEST['vote'];
>>
>>> //get content of textfile
>>> $filename = "poll_result.txt";
>>> $content = file($filename);
>>
>>> //put content in array
>>> $array = explode("||", $content[0]);
>>> $yes = $array[0];
>>> $no = $array[1];
>>
>>> if ($vote == 1)
>>>     {
>>>     $yes = $yes + 1;
>>>     }
>>> if ($vote == 0)
>>>     {
>>>     $no = $no + 1;
>>>     }
>>
>>> // debug
>>> echo $yes;
>>> echo $no;
>>
>>> // output and comments
>>> /*
>>> Seed poll_result.txt to read  0||0
>>
>>> It's always 10 even though I checked the YES option several times
>>> obviously it failed to write to the poll_result.txt file.
>>
>>> Checked the poll_result.txt, it's 0||0
>>
>>> */
>>
>>> //insert votes to txt file
>>> $insertvote = $yes."||".$no;
>>> $fp = fopen($filename,"w");
>>> fputs($fp,$insertvote);
>>> fclose($fp);
>>> ?>
>>
>> Are you sure you're looking at the same file you're writing to?  You're
>> using a relative path, after all (which would place the data file in
>> your web path, where it could be downloaded by anyone - not a good idea).
>>
>> Does the user (if this is running under the web server, the web server
>> user) have permission to write to the file?
>>
>> And finally, this is totally unsafe.  There is nothing stopping two
>> different people from opening the file at the same time, potentially
>> corrupting your date.
>>
>> --
>> ==================
>> Remove the "x" from my email address
>> Jerry Stuckle
>> JDS Computer Training Corp.
>> jstuck...@attglobal.net
>> ==================
>
> Well, on "where it could be downloaded by anyone", how come?  The
> processPoll.php file or a similar process php script is a server
> script,
> average user can't mess up with it.
>

That's fine - but the data file itself is in the same directory - where 
a knowledgeable user has direct access to it.

> On file permission, sorry, I forgot to mention, it's Read and Write.
> A similar php script executes fine.
>

read/write for whom?

> On your last point of " this is totally unsafe.  There is nothing
> stopping two
>> different people from opening the file at the same time, potentially
>> corrupting your date."
> Once we fix the above two problems what could we do about it?  I'm
> totally new to php btw.
>
> Many thanks.
>
>

Has nothing to do with PHP - this can occur in ANY language.  You need 
to learn all about how to work in a multitasking environment. Basically, 
you have to limit access to the file so that only one script can access 
it at a time.  How you do it is dependent on the OS.

Or you can use a database which is built for multitasking (most are).

-- 
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
0
jstucklex (14659)
1/2/2011 10:13:31 PM
On Jan 2, 5:00=A0pm, Denis McMahon <denis.m.f.mcma...@googlemail.com>
wrote:
> On 02/01/11 20:19, justaguy wrote:
>
> > I dig up the following script. =A0Added a simple HTML file with a form
> > field named "vote" and have it send to the following php script.
> > However, it failed to write to the poll_result.txt file. =A0Why?
>
> The following seems to work for me.
>
> <?php
> $filename =3D "poll_result.txt";
> if (isset($_POST['vote'])) {
> =A0 $vote =3D intval($_POST['vote']); // get this vote
> =A0 $content =3D file($filename); // read result file
> =A0 $array =3D explode("||", $content[0]);
> =A0 $yes =3D intval($array[0]); // get stored results
> =A0 $no =3D intval($array[1]);
> =A0 if ($vote =3D=3D 1) $yes++; // increment one
> =A0 if ($vote =3D=3D 0) $no++;
> =A0 $insertvote =3D $yes."||".$no; // new result string
> =A0 }
> else {
> =A0 $insertvote =3D "0||0\n"; // reset result string
> =A0 $yes =3D 0;
> =A0 $no =3D 0;
> =A0 }
> $fp =3D fopen($filename,"w"); // store results
> fputs($fp,$insertvote);
> fclose($fp);
> echo <<<EOT
> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
> "http://www.w3.org/TR/html4/strict.dtd">
> <html>
> <head>
> <title>Yes-No Poll</title>
> </head>
> <body style=3D"padding:1em 3em">
> <form method=3D"post" action=3D"quickpoll.php" name=3D"f1" id=3D"f1">
> <p>Yes: <input type=3D"radio" name=3D"vote" id=3D"v1_1" value=3D"1"><br>
> No: <input type=3D"radio" name=3D"vote" id=3D"v1_0" value=3D"0"><br>
> <input type=3D"submit" value=3D"Vote Now" name=3D"s1" id=3D"s1"></p>
> </form>
> <p>The results so far:<br>
> Yes: $yes votes<br>
> No: $no votes</p>
> </body>
> </html>
> EOT;
> ?>
>
> Rgds
>
> Denis McMahon

Denis,

I still failed to write to the poll_result.txt file and I noticed
there's a slight difference between this file's access attributes
compared to a similar target text file with a similar script and
process.  The other target text file has rwxrwxrwx attributes while
this one has rwrwrw attributes.  I thought the global writable would
suffice but it seems I was incorrect.  My ftp client failed to do
chmod 755 to the global x (execute) attribute to the file.

Thanks though.

Don
0
donli (155)
1/2/2011 11:26:10 PM
On 02/01/11 23:26, justaguy wrote:

> I still failed to write to the poll_result.txt file and I noticed
> there's a slight difference between this file's access attributes
> compared to a similar target text file with a similar script and
> process.  The other target text file has rwxrwxrwx attributes while
> this one has rwrwrw attributes.  I thought the global writable would
> suffice but it seems I was incorrect.  My ftp client failed to do
> chmod 755 to the global x (execute) attribute to the file.

Shouldn't need execute access to write, can you check the logs and see
if there's a php error being recorded when you try and write the file?

Rgds

Denis McMahon
0
1/2/2011 11:51:18 PM
justaguy wrote:

> Denis McMahon wrote:
>> On 02/01/11 20:19, justaguy wrote:
>> > I dig up the following script.  Added a simple HTML file with a form
>> > field named "vote" and have it send to the following php script.
>> > However, it failed to write to the poll_result.txt file.  Why?
>>
>> The following seems to work for me.
>>
>> <?php
>> $filename = "poll_result.txt";
>> if (isset($_POST['vote'])) {
>>   $vote = intval($_POST['vote']); // get this vote
>>   $content = file($filename); // read result file
>>   $array = explode("||", $content[0]);
>>   $yes = intval($array[0]); // get stored results
>>   $no = intval($array[1]);
>>   if ($vote == 1) $yes++; // increment one
>>   if ($vote == 0) $no++;
>>   $insertvote = $yes."||".$no; // new result string
>> }
>> else {
>>   $insertvote = "0||0\n"; // reset result string
>>   $yes = 0;
>>   $no = 0;
>> }
>> $fp = fopen($filename,"w"); // store results
>> fputs($fp,$insertvote);
>> fclose($fp);
>> echo <<<EOT
>> […]
>> EOT;
>> ?>
> 
> I still failed to write to the poll_result.txt file and I noticed
> there's a slight difference between this file's access attributes
> compared to a similar target text file with a similar script and
> process.  The other target text file has rwxrwxrwx attributes while
> this one has rwrwrw attributes. 

This is commonly written `rw-rw-rw-' or `chmod 666' instead.  Rule of thumb: 
r = 4, w = 2, x = 1; addition provides the number.

> I thought the global writable would suffice but it seems I was incorrect. 
> My ftp client failed to do chmod 755 to the global x (execute) attribute
> to the file.

You would be well-advised to get the basics right first.

  1. If you had previously `rw-rw-rw-' or `chmod 666', you would need
     to do `chmod o+x' or `chmod 777' to get `rwxrwxrwx'.

  2. In which mode you can open a file has not so much to do with that
     file's permissions.  More important are the ownership of the file,
     the permissions of the directory the file is located in, and who/
     what else has that file already open.

  3. A text file's *executable* bit of course has nothing to do with
     whether or not you can read from or write to this file (regarding
     PHP, it is relevant for CGI scripts only).  However, the read and
     executable permission bits of its directory are relevant for reading,
     and the directorie's write permission bit is relevant for creating
     a new file.

  4. There is no "global writable" or "global x (execute) attribute".
     The first _permissions_ triple applies only to the owner of the file,
     the second to all users of the group which owns the file, and the
     the third one to all *other* users.

     Usually PHP is not run by the owner or the group of the file, but
     by the Web server; this is either run by root or by a special system
     user (e.g. www-data), which usually is not the owner nor is its group
     the owning group.

     So the *third* permissions triple is relevant, not the first.

And please trim your quotes to the parts you are referring to.


PointedEars
-- 
Danny Goodman's books are out of date and teach practices that are
positively harmful for cross-browser scripting.
 -- Richard Cornford, cljs, <cife6q$253$1$8300dec7@news.demon.co.uk> (2004)
0
PointedEars (2489)
1/2/2011 11:54:35 PM
On 1/2/2011 6:26 PM, justaguy wrote:
> On Jan 2, 5:00 pm, Denis McMahon<denis.m.f.mcma...@googlemail.com>
> wrote:
>> On 02/01/11 20:19, justaguy wrote:
>>
>>> I dig up the following script.  Added a simple HTML file with a form
>>> field named "vote" and have it send to the following php script.
>>> However, it failed to write to the poll_result.txt file.  Why?
>>
>> The following seems to work for me.
>>
>> <?php
>> $filename = "poll_result.txt";
>> if (isset($_POST['vote'])) {
>>    $vote = intval($_POST['vote']); // get this vote
>>    $content = file($filename); // read result file
>>    $array = explode("||", $content[0]);
>>    $yes = intval($array[0]); // get stored results
>>    $no = intval($array[1]);
>>    if ($vote == 1) $yes++; // increment one
>>    if ($vote == 0) $no++;
>>    $insertvote = $yes."||".$no; // new result string
>>    }
>> else {
>>    $insertvote = "0||0\n"; // reset result string
>>    $yes = 0;
>>    $no = 0;
>>    }
>> $fp = fopen($filename,"w"); // store results
>> fputs($fp,$insertvote);
>> fclose($fp);
>> echo<<<EOT
>> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
>> "http://www.w3.org/TR/html4/strict.dtd">
>> <html>
>> <head>
>> <title>Yes-No Poll</title>
>> </head>
>> <body style="padding:1em 3em">
>> <form method="post" action="quickpoll.php" name="f1" id="f1">
>> <p>Yes:<input type="radio" name="vote" id="v1_1" value="1"><br>
>> No:<input type="radio" name="vote" id="v1_0" value="0"><br>
>> <input type="submit" value="Vote Now" name="s1" id="s1"></p>
>> </form>
>> <p>The results so far:<br>
>> Yes: $yes votes<br>
>> No: $no votes</p>
>> </body>
>> </html>
>> EOT;
>> ?>
>>
>> Rgds
>>
>> Denis McMahon
>
> Denis,
>
> I still failed to write to the poll_result.txt file and I noticed
> there's a slight difference between this file's access attributes
> compared to a similar target text file with a similar script and
> process.  The other target text file has rwxrwxrwx attributes while
> this one has rwrwrw attributes.  I thought the global writable would
> suffice but it seems I was incorrect.  My ftp client failed to do
> chmod 755 to the global x (execute) attribute to the file.
>
> Thanks though.
>
> Don

You do not need execute privileges to write the file - in fact, you 
should NOT have it - the file is not an executable.  You also definitely 
should NOT have global write (or read) privileges.

Did you check the return value from fopen()?  ALWAYS check it to ensure 
that the file opened correctly.  Also, do you know if the fread() and/or 
fwrite() worked correctly?  You never check to see.

-- 
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
0
jstucklex (14659)
1/2/2011 11:55:59 PM
On 1/2/2011 5:00 PM, Denis McMahon wrote:
> On 02/01/11 20:19, justaguy wrote:
>
>> I dig up the following script.  Added a simple HTML file with a form
>> field named "vote" and have it send to the following php script.
>> However, it failed to write to the poll_result.txt file.  Why?
>
> The following seems to work for me.
>
> <?php
> $filename = "poll_result.txt";
> if (isset($_POST['vote'])) {
>    $vote = intval($_POST['vote']); // get this vote
>    $content = file($filename); // read result file
>    $array = explode("||", $content[0]);
>    $yes = intval($array[0]); // get stored results
>    $no = intval($array[1]);
>    if ($vote == 1) $yes++; // increment one
>    if ($vote == 0) $no++;
>    $insertvote = $yes."||".$no; // new result string
>    }
> else {
>    $insertvote = "0||0\n"; // reset result string
>    $yes = 0;
>    $no = 0;
>    }
> $fp = fopen($filename,"w"); // store results
> fputs($fp,$insertvote);
> fclose($fp);
> echo<<<EOT
> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
> "http://www.w3.org/TR/html4/strict.dtd">
> <html>
> <head>
> <title>Yes-No Poll</title>
> </head>
> <body style="padding:1em 3em">
> <form method="post" action="quickpoll.php" name="f1" id="f1">
> <p>Yes:<input type="radio" name="vote" id="v1_1" value="1"><br>
> No:<input type="radio" name="vote" id="v1_0" value="0"><br>
> <input type="submit" value="Vote Now" name="s1" id="s1"></p>
> </form>
> <p>The results so far:<br>
> Yes: $yes votes<br>
> No: $no votes</p>
> </body>
> </html>
> EOT;
> ?>
>
> Rgds
>
> Denis McMahon

Like the original op's, your code is totally unsafe in a multitasking 
environment.

-- 
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
0
jstucklex (14659)
1/2/2011 11:56:41 PM
On Jan 2, 6:55=A0pm, Jerry Stuckle <jstuck...@attglobal.net> wrote:
> On 1/2/2011 6:26 PM, justaguy wrote:
>
>
>
> > On Jan 2, 5:00 pm, Denis McMahon<denis.m.f.mcma...@googlemail.com>
> > wrote:
> >> On 02/01/11 20:19, justaguy wrote:
>
> >>> I dig up the following script. =A0Added a simple HTML file with a for=
m
> >>> field named "vote" and have it send to the following php script.
> >>> However, it failed to write to the poll_result.txt file. =A0Why?
>
> >> The following seems to work for me.
>
> >> <?php
> >> $filename =3D "poll_result.txt";
> >> if (isset($_POST['vote'])) {
> >> =A0 =A0$vote =3D intval($_POST['vote']); // get this vote
> >> =A0 =A0$content =3D file($filename); // read result file
> >> =A0 =A0$array =3D explode("||", $content[0]);
> >> =A0 =A0$yes =3D intval($array[0]); // get stored results
> >> =A0 =A0$no =3D intval($array[1]);
> >> =A0 =A0if ($vote =3D=3D 1) $yes++; // increment one
> >> =A0 =A0if ($vote =3D=3D 0) $no++;
> >> =A0 =A0$insertvote =3D $yes."||".$no; // new result string
> >> =A0 =A0}
> >> else {
> >> =A0 =A0$insertvote =3D "0||0\n"; // reset result string
> >> =A0 =A0$yes =3D 0;
> >> =A0 =A0$no =3D 0;
> >> =A0 =A0}
> >> $fp =3D fopen($filename,"w"); // store results
> >> fputs($fp,$insertvote);
> >> fclose($fp);
> >> echo<<<EOT
> >> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
> >> "http://www.w3.org/TR/html4/strict.dtd">
> >> <html>
> >> <head>
> >> <title>Yes-No Poll</title>
> >> </head>
> >> <body style=3D"padding:1em 3em">
> >> <form method=3D"post" action=3D"quickpoll.php" name=3D"f1" id=3D"f1">
> >> <p>Yes:<input type=3D"radio" name=3D"vote" id=3D"v1_1" value=3D"1"><br=
>
> >> No:<input type=3D"radio" name=3D"vote" id=3D"v1_0" value=3D"0"><br>
> >> <input type=3D"submit" value=3D"Vote Now" name=3D"s1" id=3D"s1"></p>
> >> </form>
> >> <p>The results so far:<br>
> >> Yes: $yes votes<br>
> >> No: $no votes</p>
> >> </body>
> >> </html>
> >> EOT;
> >> ?>
>
> >> Rgds
>
> >> Denis McMahon
>
> > Denis,
>
> > I still failed to write to the poll_result.txt file and I noticed
> > there's a slight difference between this file's access attributes
> > compared to a similar target text file with a similar script and
> > process. =A0The other target text file has rwxrwxrwx attributes while
> > this one has rwrwrw attributes. =A0I thought the global writable would
> > suffice but it seems I was incorrect. =A0My ftp client failed to do
> > chmod 755 to the global x (execute) attribute to the file.
>
> > Thanks though.
>
> > Don
>
> You do not need execute privileges to write the file - in fact, you
> should NOT have it - the file is not an executable. =A0You also definitel=
y
> should NOT have global write (or read) privileges.
>
> Did you check the return value from fopen()? =A0ALWAYS check it to ensure
> that the file opened correctly. =A0Also, do you know if the fread() and/o=
r
> fwrite() worked correctly? =A0You never check to see.
>
> --
> =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
> Remove the "x" from my email address
> Jerry Stuckle
> JDS Computer Training Corp.
> jstuck...@attglobal.net
> =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D

Jerry,

Here's the block of code with debugging info and it still has a
problem:
//insert votes to txt file
$insertvote =3D $yes."||".$no;
$fp =3D fopen($filename,"w");
echo "fp;";
echo $fp;

fputs($fp,$insertvote);
echo "insert; ";
echo $insertvote;
fclose($fp);

And here's its output:
10 fp;insert; 1||0

Which seems to suggest the operation of $fp =3D fopen($filename,"w")
failed.

// the poll_result.txt file is in the same directory
$filename =3D "poll_result.txt";

/* why ok with another php script?
Why a similar statement for file read in another script is ok.
$myFile =3D "someLog.txt";
*/

And yes, I know the 'x' bit isn't necessary.  Odd.   Thanks.
0
donli (155)
1/3/2011 1:04:04 AM
On 1/2/2011 8:04 PM, justaguy wrote:
> On Jan 2, 6:55 pm, Jerry Stuckle<jstuck...@attglobal.net>  wrote:
>> On 1/2/2011 6:26 PM, justaguy wrote:
>>
>>
>>
>>> On Jan 2, 5:00 pm, Denis McMahon<denis.m.f.mcma...@googlemail.com>
>>> wrote:
>>>> On 02/01/11 20:19, justaguy wrote:
>>
>>>>> I dig up the following script.  Added a simple HTML file with a form
>>>>> field named "vote" and have it send to the following php script.
>>>>> However, it failed to write to the poll_result.txt file.  Why?
>>
>>>> The following seems to work for me.
>>
>>>> <?php
>>>> $filename = "poll_result.txt";
>>>> if (isset($_POST['vote'])) {
>>>>     $vote = intval($_POST['vote']); // get this vote
>>>>     $content = file($filename); // read result file
>>>>     $array = explode("||", $content[0]);
>>>>     $yes = intval($array[0]); // get stored results
>>>>     $no = intval($array[1]);
>>>>     if ($vote == 1) $yes++; // increment one
>>>>     if ($vote == 0) $no++;
>>>>     $insertvote = $yes."||".$no; // new result string
>>>>     }
>>>> else {
>>>>     $insertvote = "0||0\n"; // reset result string
>>>>     $yes = 0;
>>>>     $no = 0;
>>>>     }
>>>> $fp = fopen($filename,"w"); // store results
>>>> fputs($fp,$insertvote);
>>>> fclose($fp);
>>>> echo<<<EOT
>>>> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
>>>> "http://www.w3.org/TR/html4/strict.dtd">
>>>> <html>
>>>> <head>
>>>> <title>Yes-No Poll</title>
>>>> </head>
>>>> <body style="padding:1em 3em">
>>>> <form method="post" action="quickpoll.php" name="f1" id="f1">
>>>> <p>Yes:<input type="radio" name="vote" id="v1_1" value="1"><br>
>>>> No:<input type="radio" name="vote" id="v1_0" value="0"><br>
>>>> <input type="submit" value="Vote Now" name="s1" id="s1"></p>
>>>> </form>
>>>> <p>The results so far:<br>
>>>> Yes: $yes votes<br>
>>>> No: $no votes</p>
>>>> </body>
>>>> </html>
>>>> EOT;
>>>> ?>
>>
>>>> Rgds
>>
>>>> Denis McMahon
>>
>>> Denis,
>>
>>> I still failed to write to the poll_result.txt file and I noticed
>>> there's a slight difference between this file's access attributes
>>> compared to a similar target text file with a similar script and
>>> process.  The other target text file has rwxrwxrwx attributes while
>>> this one has rwrwrw attributes.  I thought the global writable would
>>> suffice but it seems I was incorrect.  My ftp client failed to do
>>> chmod 755 to the global x (execute) attribute to the file.
>>
>>> Thanks though.
>>
>>> Don
>>
>> You do not need execute privileges to write the file - in fact, you
>> should NOT have it - the file is not an executable.  You also definitely
>> should NOT have global write (or read) privileges.
>>
>> Did you check the return value from fopen()?  ALWAYS check it to ensure
>> that the file opened correctly.  Also, do you know if the fread() and/or
>> fwrite() worked correctly?  You never check to see.
>>
>> --
>> ==================
>> Remove the "x" from my email address
>> Jerry Stuckle
>> JDS Computer Training Corp.
>> jstuck...@attglobal.net
>> ==================
>
> Jerry,
>
> Here's the block of code with debugging info and it still has a
> problem:
> //insert votes to txt file
> $insertvote = $yes."||".$no;
> $fp = fopen($filename,"w");
> echo "fp;";
> echo $fp;
>
> fputs($fp,$insertvote);
> echo "insert; ";
> echo $insertvote;
> fclose($fp);
>
> And here's its output:
> 10 fp;insert; 1||0
>
> Which seems to suggest the operation of $fp = fopen($filename,"w")
> failed.
>
> // the poll_result.txt file is in the same directory
> $filename = "poll_result.txt";
>
> /* why ok with another php script?
> Why a similar statement for file read in another script is ok.
> $myFile = "someLog.txt";
> */
>
> And yes, I know the 'x' bit isn't necessary.  Odd.   Thanks.

I'm not talking about 'debug code' - I'm talking about actual code you 
should have in every script - error checking.

NEVER assume an external operation works. You should ALWAYS check the 
return code, especially where external operations are concerned, and if 
you get a bad one, you should take correct action.

fopen() returns false if the file was not opened.  So your current 
example should have checked for a failure in fopen and taken the 
appropriate action.  For testing purposes, displaying a message would be 
sufficient.  You would not want to do that on a production server, of 
course, but you should at least log the error.

Had you had such code in place, you would have known that the fopen() 
failed and could have followed down why.  And if this is your 
development server, you should have in your php.ini:

display_errors=on
error_reporting=E_ALL  (or E_ALL | E_STRICT)

These would have given you additional messages. But you don't want these 
on a production server; instead you should log your PHP errors to a file 
and look at that file when you have problems.



-- 
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
0
jstucklex (14659)
1/3/2011 1:34:42 AM
On Jan 2, 8:34=A0pm, Jerry Stuckle <jstuck...@attglobal.net> wrote:
> On 1/2/2011 8:04 PM, justaguy wrote:
>
>
>
> > On Jan 2, 6:55 pm, Jerry Stuckle<jstuck...@attglobal.net> =A0wrote:
> >> On 1/2/2011 6:26 PM, justaguy wrote:
>
> >>> On Jan 2, 5:00 pm, Denis McMahon<denis.m.f.mcma...@googlemail.com>
> >>> wrote:
> >>>> On 02/01/11 20:19, justaguy wrote:
>
> >>>>> I dig up the following script. =A0Added a simple HTML file with a f=
orm
> >>>>> field named "vote" and have it send to the following php script.
> >>>>> However, it failed to write to the poll_result.txt file. =A0Why?
>
> >>>> The following seems to work for me.
>
> >>>> <?php
> >>>> $filename =3D "poll_result.txt";
> >>>> if (isset($_POST['vote'])) {
> >>>> =A0 =A0 $vote =3D intval($_POST['vote']); // get this vote
> >>>> =A0 =A0 $content =3D file($filename); // read result file
> >>>> =A0 =A0 $array =3D explode("||", $content[0]);
> >>>> =A0 =A0 $yes =3D intval($array[0]); // get stored results
> >>>> =A0 =A0 $no =3D intval($array[1]);
> >>>> =A0 =A0 if ($vote =3D=3D 1) $yes++; // increment one
> >>>> =A0 =A0 if ($vote =3D=3D 0) $no++;
> >>>> =A0 =A0 $insertvote =3D $yes."||".$no; // new result string
> >>>> =A0 =A0 }
> >>>> else {
> >>>> =A0 =A0 $insertvote =3D "0||0\n"; // reset result string
> >>>> =A0 =A0 $yes =3D 0;
> >>>> =A0 =A0 $no =3D 0;
> >>>> =A0 =A0 }
> >>>> $fp =3D fopen($filename,"w"); // store results
> >>>> fputs($fp,$insertvote);
> >>>> fclose($fp);
> >>>> echo<<<EOT
> >>>> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
> >>>> "http://www.w3.org/TR/html4/strict.dtd">
> >>>> <html>
> >>>> <head>
> >>>> <title>Yes-No Poll</title>
> >>>> </head>
> >>>> <body style=3D"padding:1em 3em">
> >>>> <form method=3D"post" action=3D"quickpoll.php" name=3D"f1" id=3D"f1"=
>
> >>>> <p>Yes:<input type=3D"radio" name=3D"vote" id=3D"v1_1" value=3D"1"><=
br>
> >>>> No:<input type=3D"radio" name=3D"vote" id=3D"v1_0" value=3D"0"><br>
> >>>> <input type=3D"submit" value=3D"Vote Now" name=3D"s1" id=3D"s1"></p>
> >>>> </form>
> >>>> <p>The results so far:<br>
> >>>> Yes: $yes votes<br>
> >>>> No: $no votes</p>
> >>>> </body>
> >>>> </html>
> >>>> EOT;
> >>>> ?>
>
> >>>> Rgds
>
> >>>> Denis McMahon
>
> >>> Denis,
>
> >>> I still failed to write to the poll_result.txt file and I noticed
> >>> there's a slight difference between this file's access attributes
> >>> compared to a similar target text file with a similar script and
> >>> process. =A0The other target text file has rwxrwxrwx attributes while
> >>> this one has rwrwrw attributes. =A0I thought the global writable woul=
d
> >>> suffice but it seems I was incorrect. =A0My ftp client failed to do
> >>> chmod 755 to the global x (execute) attribute to the file.
>
> >>> Thanks though.
>
> >>> Don
>
> >> You do not need execute privileges to write the file - in fact, you
> >> should NOT have it - the file is not an executable. =A0You also defini=
tely
> >> should NOT have global write (or read) privileges.
>
> >> Did you check the return value from fopen()? =A0ALWAYS check it to ens=
ure
> >> that the file opened correctly. =A0Also, do you know if the fread() an=
d/or
> >> fwrite() worked correctly? =A0You never check to see.
>
> >> --
> >> =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
> >> Remove the "x" from my email address
> >> Jerry Stuckle
> >> JDS Computer Training Corp.
> >> jstuck...@attglobal.net
> >> =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
>
> > Jerry,
>
> > Here's the block of code with debugging info and it still has a
> > problem:
> > //insert votes to txt file
> > $insertvote =3D $yes."||".$no;
> > $fp =3D fopen($filename,"w");
> > echo "fp;";
> > echo $fp;
>
> > fputs($fp,$insertvote);
> > echo "insert; ";
> > echo $insertvote;
> > fclose($fp);
>
> > And here's its output:
> > 10 fp;insert; 1||0
>
> > Which seems to suggest the operation of $fp =3D fopen($filename,"w")
> > failed.
>
> > // the poll_result.txt file is in the same directory
> > $filename =3D "poll_result.txt";
>
> > /* why ok with another php script?
> > Why a similar statement for file read in another script is ok.
> > $myFile =3D "someLog.txt";
> > */
>
> > And yes, I know the 'x' bit isn't necessary. =A0Odd. =A0 Thanks.
>
> I'm not talking about 'debug code' - I'm talking about actual code you
> should have in every script - error checking.
>
> NEVER assume an external operation works. You should ALWAYS check the
> return code, especially where external operations are concerned, and if
> you get a bad one, you should take correct action.
>
> fopen() returns false if the file was not opened. =A0So your current
> example should have checked for a failure in fopen and taken the
> appropriate action. =A0For testing purposes, displaying a message would b=
e
> sufficient. =A0You would not want to do that on a production server, of
> course, but you should at least log the error.
>
> Had you had such code in place, you would have known that the fopen()
> failed and could have followed down why. =A0And if this is your
> development server, you should have in your php.ini:
>
> display_errors=3Don
> error_reporting=3DE_ALL =A0(or E_ALL | E_STRICT)
>
> These would have given you additional messages. But you don't want these
> on a production server; instead you should log your PHP errors to a file
> and look at that file when you have problems.
>
> --
> =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
> Remove the "x" from my email address
> Jerry Stuckle
> JDS Computer Training Corp.
> jstuck...@attglobal.net
> =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D

Jerry,

The code isn't working, so, of course it's not in production.  I'm
using an ISP's server, don't have a lot of control...
yes, you're right, I was lazy in error detection/tracking for this
piece of code, thought it should be fairly straight-forward.

Thanks.

Don
0
donli (155)
1/3/2011 2:13:34 AM
On 1/2/2011 9:13 PM, justaguy wrote:
> On Jan 2, 8:34 pm, Jerry Stuckle<jstuck...@attglobal.net>  wrote:
>> On 1/2/2011 8:04 PM, justaguy wrote:
>>
>>
>>
>>> On Jan 2, 6:55 pm, Jerry Stuckle<jstuck...@attglobal.net>    wrote:
>>>> On 1/2/2011 6:26 PM, justaguy wrote:
>>
>>>>> On Jan 2, 5:00 pm, Denis McMahon<denis.m.f.mcma...@googlemail.com>
>>>>> wrote:
>>>>>> On 02/01/11 20:19, justaguy wrote:
>>
>>>>>>> I dig up the following script.  Added a simple HTML file with a form
>>>>>>> field named "vote" and have it send to the following php script.
>>>>>>> However, it failed to write to the poll_result.txt file.  Why?
>>
>>>>>> The following seems to work for me.
>>
>>>>>> <?php
>>>>>> $filename = "poll_result.txt";
>>>>>> if (isset($_POST['vote'])) {
>>>>>>      $vote = intval($_POST['vote']); // get this vote
>>>>>>      $content = file($filename); // read result file
>>>>>>      $array = explode("||", $content[0]);
>>>>>>      $yes = intval($array[0]); // get stored results
>>>>>>      $no = intval($array[1]);
>>>>>>      if ($vote == 1) $yes++; // increment one
>>>>>>      if ($vote == 0) $no++;
>>>>>>      $insertvote = $yes."||".$no; // new result string
>>>>>>      }
>>>>>> else {
>>>>>>      $insertvote = "0||0\n"; // reset result string
>>>>>>      $yes = 0;
>>>>>>      $no = 0;
>>>>>>      }
>>>>>> $fp = fopen($filename,"w"); // store results
>>>>>> fputs($fp,$insertvote);
>>>>>> fclose($fp);
>>>>>> echo<<<EOT
>>>>>> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
>>>>>> "http://www.w3.org/TR/html4/strict.dtd">
>>>>>> <html>
>>>>>> <head>
>>>>>> <title>Yes-No Poll</title>
>>>>>> </head>
>>>>>> <body style="padding:1em 3em">
>>>>>> <form method="post" action="quickpoll.php" name="f1" id="f1">
>>>>>> <p>Yes:<input type="radio" name="vote" id="v1_1" value="1"><br>
>>>>>> No:<input type="radio" name="vote" id="v1_0" value="0"><br>
>>>>>> <input type="submit" value="Vote Now" name="s1" id="s1"></p>
>>>>>> </form>
>>>>>> <p>The results so far:<br>
>>>>>> Yes: $yes votes<br>
>>>>>> No: $no votes</p>
>>>>>> </body>
>>>>>> </html>
>>>>>> EOT;
>>>>>> ?>
>>
>>>>>> Rgds
>>
>>>>>> Denis McMahon
>>
>>>>> Denis,
>>
>>>>> I still failed to write to the poll_result.txt file and I noticed
>>>>> there's a slight difference between this file's access attributes
>>>>> compared to a similar target text file with a similar script and
>>>>> process.  The other target text file has rwxrwxrwx attributes while
>>>>> this one has rwrwrw attributes.  I thought the global writable would
>>>>> suffice but it seems I was incorrect.  My ftp client failed to do
>>>>> chmod 755 to the global x (execute) attribute to the file.
>>
>>>>> Thanks though.
>>
>>>>> Don
>>
>>>> You do not need execute privileges to write the file - in fact, you
>>>> should NOT have it - the file is not an executable.  You also definitely
>>>> should NOT have global write (or read) privileges.
>>
>>>> Did you check the return value from fopen()?  ALWAYS check it to ensure
>>>> that the file opened correctly.  Also, do you know if the fread() and/or
>>>> fwrite() worked correctly?  You never check to see.
>>
>>>> --
>>>> ==================
>>>> Remove the "x" from my email address
>>>> Jerry Stuckle
>>>> JDS Computer Training Corp.
>>>> jstuck...@attglobal.net
>>>> ==================
>>
>>> Jerry,
>>
>>> Here's the block of code with debugging info and it still has a
>>> problem:
>>> //insert votes to txt file
>>> $insertvote = $yes."||".$no;
>>> $fp = fopen($filename,"w");
>>> echo "fp;";
>>> echo $fp;
>>
>>> fputs($fp,$insertvote);
>>> echo "insert; ";
>>> echo $insertvote;
>>> fclose($fp);
>>
>>> And here's its output:
>>> 10 fp;insert; 1||0
>>
>>> Which seems to suggest the operation of $fp = fopen($filename,"w")
>>> failed.
>>
>>> // the poll_result.txt file is in the same directory
>>> $filename = "poll_result.txt";
>>
>>> /* why ok with another php script?
>>> Why a similar statement for file read in another script is ok.
>>> $myFile = "someLog.txt";
>>> */
>>
>>> And yes, I know the 'x' bit isn't necessary.  Odd.   Thanks.
>>
>> I'm not talking about 'debug code' - I'm talking about actual code you
>> should have in every script - error checking.
>>
>> NEVER assume an external operation works. You should ALWAYS check the
>> return code, especially where external operations are concerned, and if
>> you get a bad one, you should take correct action.
>>
>> fopen() returns false if the file was not opened.  So your current
>> example should have checked for a failure in fopen and taken the
>> appropriate action.  For testing purposes, displaying a message would be
>> sufficient.  You would not want to do that on a production server, of
>> course, but you should at least log the error.
>>
>> Had you had such code in place, you would have known that the fopen()
>> failed and could have followed down why.  And if this is your
>> development server, you should have in your php.ini:
>>
>> display_errors=on
>> error_reporting=E_ALL  (or E_ALL | E_STRICT)
>>
>> These would have given you additional messages. But you don't want these
>> on a production server; instead you should log your PHP errors to a file
>> and look at that file when you have problems.
>>
>> --
>> ==================
>> Remove the "x" from my email address
>> Jerry Stuckle
>> JDS Computer Training Corp.
>> jstuck...@attglobal.net
>> ==================
>
> Jerry,
>
> The code isn't working, so, of course it's not in production.  I'm
> using an ISP's server, don't have a lot of control...
> yes, you're right, I was lazy in error detection/tracking for this
> piece of code, thought it should be fairly straight-forward.
>
> Thanks.
>
> Don

Another point - get your own development machine going. It will make 
your life a lot easier.  Even if it's not the same OS and won't catch 
every error (i.e. you wouldn't see this error on Windows), it will help 
you many other ways.

And depending on the ISP's setup, ini_set() and/or your .htaccess file 
can change a number of PHP settings for a single script, every script on 
your site, or many steps in between.

-- 
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
0
jstucklex (14659)
1/3/2011 3:22:48 AM
On 02/01/11 23:56, Jerry Stuckle wrote:

> Like the original op's, your code is totally unsafe in a multitasking
> environment.

I didn't say it was. I just took his code and made it work on my dev
server. It's up to him to make sure his code is robust for the
environment he's running it in.

As my dev server isn't a multitasking environment from the point of view
of multiple users accessing the poll at once, I'm not too worried about
whether this particular code on it is robust in that environment.

Rgds

Denis McMahon
0
1/3/2011 3:38:50 AM
On 1/2/2011 10:38 PM, Denis McMahon wrote:
> On 02/01/11 23:56, Jerry Stuckle wrote:
>
>> Like the original op's, your code is totally unsafe in a multitasking
>> environment.
>
> I didn't say it was. I just took his code and made it work on my dev
> server. It's up to him to make sure his code is robust for the
> environment he's running it in.
>
> As my dev server isn't a multitasking environment from the point of view
> of multiple users accessing the poll at once, I'm not too worried about
> whether this particular code on it is robust in that environment.
>
> Rgds
>
> Denis McMahon

No, but if you're going to suggest code, you need to suggest good code 
or list major limitations like not being safe in a multitasking environment.

-- 
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
0
jstucklex (14659)
1/3/2011 3:46:14 AM
On Jan 2, 10:22=A0pm, Jerry Stuckle <jstuck...@attglobal.net> wrote:
> On 1/2/2011 9:13 PM, justaguy wrote:
>
>
>
> > On Jan 2, 8:34 pm, Jerry Stuckle<jstuck...@attglobal.net> =A0wrote:
> >> On 1/2/2011 8:04 PM, justaguy wrote:
>
> >>> On Jan 2, 6:55 pm, Jerry Stuckle<jstuck...@attglobal.net> =A0 =A0wrot=
e:
> >>>> On 1/2/2011 6:26 PM, justaguy wrote:
>
> >>>>> On Jan 2, 5:00 pm, Denis McMahon<denis.m.f.mcma...@googlemail.com>
> >>>>> wrote:
> >>>>>> On 02/01/11 20:19, justaguy wrote:
>
> >>>>>>> I dig up the following script. =A0Added a simple HTML file with a=
 form
> >>>>>>> field named "vote" and have it send to the following php script.
> >>>>>>> However, it failed to write to the poll_result.txt file. =A0Why?
>
> >>>>>> The following seems to work for me.
>
> >>>>>> <?php
> >>>>>> $filename =3D "poll_result.txt";
> >>>>>> if (isset($_POST['vote'])) {
> >>>>>> =A0 =A0 =A0$vote =3D intval($_POST['vote']); // get this vote
> >>>>>> =A0 =A0 =A0$content =3D file($filename); // read result file
> >>>>>> =A0 =A0 =A0$array =3D explode("||", $content[0]);
> >>>>>> =A0 =A0 =A0$yes =3D intval($array[0]); // get stored results
> >>>>>> =A0 =A0 =A0$no =3D intval($array[1]);
> >>>>>> =A0 =A0 =A0if ($vote =3D=3D 1) $yes++; // increment one
> >>>>>> =A0 =A0 =A0if ($vote =3D=3D 0) $no++;
> >>>>>> =A0 =A0 =A0$insertvote =3D $yes."||".$no; // new result string
> >>>>>> =A0 =A0 =A0}
> >>>>>> else {
> >>>>>> =A0 =A0 =A0$insertvote =3D "0||0\n"; // reset result string
> >>>>>> =A0 =A0 =A0$yes =3D 0;
> >>>>>> =A0 =A0 =A0$no =3D 0;
> >>>>>> =A0 =A0 =A0}
> >>>>>> $fp =3D fopen($filename,"w"); // store results
> >>>>>> fputs($fp,$insertvote);
> >>>>>> fclose($fp);
> >>>>>> echo<<<EOT
> >>>>>> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
> >>>>>> "http://www.w3.org/TR/html4/strict.dtd">
> >>>>>> <html>
> >>>>>> <head>
> >>>>>> <title>Yes-No Poll</title>
> >>>>>> </head>
> >>>>>> <body style=3D"padding:1em 3em">
> >>>>>> <form method=3D"post" action=3D"quickpoll.php" name=3D"f1" id=3D"f=
1">
> >>>>>> <p>Yes:<input type=3D"radio" name=3D"vote" id=3D"v1_1" value=3D"1"=
><br>
> >>>>>> No:<input type=3D"radio" name=3D"vote" id=3D"v1_0" value=3D"0"><br=
>
> >>>>>> <input type=3D"submit" value=3D"Vote Now" name=3D"s1" id=3D"s1"></=
p>
> >>>>>> </form>
> >>>>>> <p>The results so far:<br>
> >>>>>> Yes: $yes votes<br>
> >>>>>> No: $no votes</p>
> >>>>>> </body>
> >>>>>> </html>
> >>>>>> EOT;
> >>>>>> ?>
>
> >>>>>> Rgds
>
> >>>>>> Denis McMahon
>
> >>>>> Denis,
>
> >>>>> I still failed to write to the poll_result.txt file and I noticed
> >>>>> there's a slight difference between this file's access attributes
> >>>>> compared to a similar target text file with a similar script and
> >>>>> process. =A0The other target text file has rwxrwxrwx attributes whi=
le
> >>>>> this one has rwrwrw attributes. =A0I thought the global writable wo=
uld
> >>>>> suffice but it seems I was incorrect. =A0My ftp client failed to do
> >>>>> chmod 755 to the global x (execute) attribute to the file.
>
> >>>>> Thanks though.
>
> >>>>> Don
>
> >>>> You do not need execute privileges to write the file - in fact, you
> >>>> should NOT have it - the file is not an executable. =A0You also defi=
nitely
> >>>> should NOT have global write (or read) privileges.
>
> >>>> Did you check the return value from fopen()? =A0ALWAYS check it to e=
nsure
> >>>> that the file opened correctly. =A0Also, do you know if the fread() =
and/or
> >>>> fwrite() worked correctly? =A0You never check to see.
>
> >>>> --
> >>>> =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
> >>>> Remove the "x" from my email address
> >>>> Jerry Stuckle
> >>>> JDS Computer Training Corp.
> >>>> jstuck...@attglobal.net
> >>>> =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
>
> >>> Jerry,
>
> >>> Here's the block of code with debugging info and it still has a
> >>> problem:
> >>> //insert votes to txt file
> >>> $insertvote =3D $yes."||".$no;
> >>> $fp =3D fopen($filename,"w");
> >>> echo "fp;";
> >>> echo $fp;
>
> >>> fputs($fp,$insertvote);
> >>> echo "insert; ";
> >>> echo $insertvote;
> >>> fclose($fp);
>
> >>> And here's its output:
> >>> 10 fp;insert; 1||0
>
> >>> Which seems to suggest the operation of $fp =3D fopen($filename,"w")
> >>> failed.
>
> >>> // the poll_result.txt file is in the same directory
> >>> $filename =3D "poll_result.txt";
>
> >>> /* why ok with another php script?
> >>> Why a similar statement for file read in another script is ok.
> >>> $myFile =3D "someLog.txt";
> >>> */
>
> >>> And yes, I know the 'x' bit isn't necessary. =A0Odd. =A0 Thanks.
>
> >> I'm not talking about 'debug code' - I'm talking about actual code you
> >> should have in every script - error checking.
>
> >> NEVER assume an external operation works. You should ALWAYS check the
> >> return code, especially where external operations are concerned, and i=
f
> >> you get a bad one, you should take correct action.
>
> >> fopen() returns false if the file was not opened. =A0So your current
> >> example should have checked for a failure in fopen and taken the
> >> appropriate action. =A0For testing purposes, displaying a message woul=
d be
> >> sufficient. =A0You would not want to do that on a production server, o=
f
> >> course, but you should at least log the error.
>
> >> Had you had such code in place, you would have known that the fopen()
> >> failed and could have followed down why. =A0And if this is your
> >> development server, you should have in your php.ini:
>
> >> display_errors=3Don
> >> error_reporting=3DE_ALL =A0(or E_ALL | E_STRICT)
>
> >> These would have given you additional messages. But you don't want the=
se
> >> on a production server; instead you should log your PHP errors to a fi=
le
> >> and look at that file when you have problems.
>
> >> --
> >> =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
> >> Remove the "x" from my email address
> >> Jerry Stuckle
> >> JDS Computer Training Corp.
> >> jstuck...@attglobal.net
> >> =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
>
> > Jerry,
>
> > The code isn't working, so, of course it's not in production. =A0I'm
> > using an ISP's server, don't have a lot of control...
> > yes, you're right, I was lazy in error detection/tracking for this
> > piece of code, thought it should be fairly straight-forward.
>
> > Thanks.
>
> > Don
>
> Another point - get your own development machine going. It will make
> your life a lot easier. =A0Even if it's not the same OS and won't catch
> every error (i.e. you wouldn't see this error on Windows), it will help
> you many other ways.
>
> And depending on the ISP's setup, ini_set() and/or your .htaccess file
> can change a number of PHP settings for a single script, every script on
> your site, or many steps in between.
>
> --
> =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
> Remove the "x" from my email address
> Jerry Stuckle
> JDS Computer Training Corp.
> jstuck...@attglobal.net
> =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D

Too bad, an open source webserver, Caucho Resin on my Windows 7 box
fails to support php.  The same code produced a blank page with
nothing.
0
donli (155)
1/3/2011 4:29:16 AM
Gazing into my crystal ball I observed justaguy <donli@yahoo.com>
writing in
news:ccc49c8d-262f-4b86-a1d6-4ef388fe677e@p7g2000prb.googlegroups.com: 

> On Jan 2, 10:22�pm, Jerry Stuckle <jstuck...@attglobal.net> wrote:
>> On 1/2/2011 9:13 PM, justaguy wrote:
>>
>>
>>
>> > On Jan 2, 8:34 pm, Jerry Stuckle<jstuck...@attglobal.net> �wrote:
>> >> On 1/2/2011 8:04 PM, justaguy wrote:
>>
>> >>> On Jan 2, 6:55 pm, Jerry Stuckle<jstuck...@attglobal.net> � �wrot
> e:
>> >>>> On 1/2/2011 6:26 PM, justaguy wrote:
>>
>> >>>>> On Jan 2, 5:00 pm, Denis
>> >>>>> McMahon<denis.m.f.mcma...@googlemail.com> wrote:
>> >>>>>> On 02/01/11 20:19, justaguy wrote:
>>
>> >>>>>>> I dig up the following script. �Added a simple HTML file with
>> >>>>>>> a 
>  form
>> >>>>>>> field named "vote" and have it send to the following php
>> >>>>>>> script. However, it failed to write to the poll_result.txt
>> >>>>>>> file. �Why? 
>>
>> >>>>>> The following seems to work for me.
>>
>> >>>>>> <?php
>> >>>>>> $filename = "poll_result.txt";
>> >>>>>> if (isset($_POST['vote'])) {
>> >>>>>> � � �$vote = intval($_POST['vote']); // get this vote
>> >>>>>> � � �$content = file($filename); // read result file
>> >>>>>> � � �$array = explode("||", $content[0]);
>> >>>>>> � � �$yes = intval($array[0]); // get stored results
>> >>>>>> � � �$no = intval($array[1]);
>> >>>>>> � � �if ($vote == 1) $yes++; // increment one
>> >>>>>> � � �if ($vote == 0) $no++;
>> >>>>>> � � �$insertvote = $yes."||".$no; // new result string
>> >>>>>> � � �}
>> >>>>>> else {
>> >>>>>> � � �$insertvote = "0||0\n"; // reset result string
>> >>>>>> � � �$yes = 0;
>> >>>>>> � � �$no = 0;
>> >>>>>> � � �}
>> >>>>>> $fp = fopen($filename,"w"); // store results
>> >>>>>> fputs($fp,$insertvote);
>> >>>>>> fclose($fp);
>> >>>>>> echo<<<EOT
>> >>>>>> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
>> >>>>>> "http://www.w3.org/TR/html4/strict.dtd">
>> >>>>>> <html>
>> >>>>>> <head>
>> >>>>>> <title>Yes-No Poll</title>
>> >>>>>> </head>
>> >>>>>> <body style="padding:1em 3em">
>> >>>>>> <form method="post" action="quickpoll.php" name="f1" id="f
> 1">
>> >>>>>> <p>Yes:<input type="radio" name="vote" id="v1_1" value="1"
>><br>
>> >>>>>> No:<input type="radio" name="vote" id="v1_0" value="0"><br
>>
>> >>>>>> <input type="submit" value="Vote Now" name="s1" id="s1"></
> p>
>> >>>>>> </form>
>> >>>>>> <p>The results so far:<br>
>> >>>>>> Yes: $yes votes<br>
>> >>>>>> No: $no votes</p>
>> >>>>>> </body>
>> >>>>>> </html>
>> >>>>>> EOT;
>> >>>>>> ?>
>>
>> >>>>>> Rgds
>>
>> >>>>>> Denis McMahon
>>
>> >>>>> Denis,
>>
>> >>>>> I still failed to write to the poll_result.txt file and I
>> >>>>> noticed there's a slight difference between this file's access
>> >>>>> attributes compared to a similar target text file with a
>> >>>>> similar script and process. �The other target text file has
>> >>>>> rwxrwxrwx attributes whi 
> le
>> >>>>> this one has rwrwrw attributes. �I thought the global writable
>> >>>>> wo 
> uld
>> >>>>> suffice but it seems I was incorrect. �My ftp client failed to
>> >>>>> do chmod 755 to the global x (execute) attribute to the file.
>>
>> >>>>> Thanks though.
>>
>> >>>>> Don
>>
>> >>>> You do not need execute privileges to write the file - in fact,
>> >>>> you should NOT have it - the file is not an executable. �You
>> >>>> also defi 
> nitely
>> >>>> should NOT have global write (or read) privileges.
>>
>> >>>> Did you check the return value from fopen()? �ALWAYS check it to
>> >>>> e 
> nsure
>> >>>> that the file opened correctly. �Also, do you know if the
>> >>>> fread() 
> and/or
>> >>>> fwrite() worked correctly? �You never check to see.
>>
>> >>>> --
>> >>>> =================
>> >>>> Remove the "x" from my email address
>> >>>> Jerry Stuckle
>> >>>> JDS Computer Training Corp.
>> >>>> jstuck...@attglobal.net
>> >>>> =================
>>
>> >>> Jerry,
>>
>> >>> Here's the block of code with debugging info and it still has a
>> >>> problem:
>> >>> //insert votes to txt file
>> >>> $insertvote = $yes."||".$no;
>> >>> $fp = fopen($filename,"w");
>> >>> echo "fp;";
>> >>> echo $fp;
>>
>> >>> fputs($fp,$insertvote);
>> >>> echo "insert; ";
>> >>> echo $insertvote;
>> >>> fclose($fp);
>>
>> >>> And here's its output:
>> >>> 10 fp;insert; 1||0
>>
>> >>> Which seems to suggest the operation of $fp =
>> >>> fopen($filename,"w") failed.
>>
>> >>> // the poll_result.txt file is in the same directory
>> >>> $filename = "poll_result.txt";
>>
>> >>> /* why ok with another php script?
>> >>> Why a similar statement for file read in another script is ok.
>> >>> $myFile = "someLog.txt";
>> >>> */
>>
>> >>> And yes, I know the 'x' bit isn't necessary. �Odd. � Thanks.
>>
>> >> I'm not talking about 'debug code' - I'm talking about actual code
>> >> you should have in every script - error checking.
>>
>> >> NEVER assume an external operation works. You should ALWAYS check
>> >> the return code, especially where external operations are
>> >> concerned, and i 
> f
>> >> you get a bad one, you should take correct action.
>>
>> >> fopen() returns false if the file was not opened. �So your current
>> >> example should have checked for a failure in fopen and taken the
>> >> appropriate action. �For testing purposes, displaying a message
>> >> woul 
> d be
>> >> sufficient. �You would not want to do that on a production server,
>> >> o 
> f
>> >> course, but you should at least log the error.
>>
>> >> Had you had such code in place, you would have known that the
>> >> fopen() failed and could have followed down why. �And if this is
>> >> your development server, you should have in your php.ini:
>>
>> >> display_errors=on
>> >> error_reporting=E_ALL �(or E_ALL | E_STRICT)
>>
>> >> These would have given you additional messages. But you don't want
>> >> the 
> se
>> >> on a production server; instead you should log your PHP errors to
>> >> a fi 
> le
>> >> and look at that file when you have problems.
>>
>> >> --
>> >> =================
>> >> Remove the "x" from my email address
>> >> Jerry Stuckle
>> >> JDS Computer Training Corp.
>> >> jstuck...@attglobal.net
>> >> =================
>>
>> > Jerry,
>>
>> > The code isn't working, so, of course it's not in production. �I'm
>> > using an ISP's server, don't have a lot of control...
>> > yes, you're right, I was lazy in error detection/tracking for this
>> > piece of code, thought it should be fairly straight-forward.
>>
>> > Thanks.
>>
>> > Don
>>
>> Another point - get your own development machine going. It will make
>> your life a lot easier. �Even if it's not the same OS and won't catch
>> every error (i.e. you wouldn't see this error on Windows), it will
>> help you many other ways.
>>
>> And depending on the ISP's setup, ini_set() and/or your .htaccess
>> file can change a number of PHP settings for a single script, every
>> script on your site, or many steps in between.
>>
>> --
>> =================
>> Remove the "x" from my email address
>> Jerry Stuckle
>> JDS Computer Training Corp.
>> jstuck...@attglobal.net
>> =================
> 
> Too bad, an open source webserver, Caucho Resin on my Windows 7 box
> fails to support php.  The same code produced a blank page with
> nothing.
> 

What edition of Win7?  If it's not Home, you could use IIS.

-- 
Adrienne Boswell at Home
Arbpen Web Site Design Services
http://www.cavalcade-of-coding.info
Please respond to the group so others can share
0
arbpen454 (114)
1/3/2011 6:36:08 AM
On 1/2/2011 11:29 PM, justaguy wrote:
>>
>>> Jerry,
>>
>>> The code isn't working, so, of course it's not in production.  I'm
>>> using an ISP's server, don't have a lot of control...
>>> yes, you're right, I was lazy in error detection/tracking for this
>>> piece of code, thought it should be fairly straight-forward.
>>
>>> Thanks.
>>
>>> Don
>>
>> Another point - get your own development machine going. It will make
>> your life a lot easier.  Even if it's not the same OS and won't catch
>> every error (i.e. you wouldn't see this error on Windows), it will help
>> you many other ways.
>>
>> And depending on the ISP's setup, ini_set() and/or your .htaccess file
>> can change a number of PHP settings for a single script, every script on
>> your site, or many steps in between.
>>
>
> Too bad, an open source webserver, Caucho Resin on my Windows 7 box
> fails to support php.  The same code produced a blank page with
> nothing.

Why don't you just install Apache - it's open source, free and supports 
PHP just fine.   It also happens to be the most used web server in the 
world.

And BTW - I didn't say the code was "in production".  I said it was "on 
a production machine".  Two entirely different things.

-- 
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
0
jstucklex (14659)
1/3/2011 11:53:45 AM
On 03/01/11 03:46, Jerry Stuckle wrote:

> No, but if you're going to suggest code, you need to suggest good code
> or list major limitations like not being safe in a multitasking
> environment.

I see no point in voicing issues you've already mentioned Jerry.

Rgds

Denis McMahon
0
1/3/2011 1:36:07 PM
On 03/01/11 04:29, justaguy wrote:

> Too bad, an open source webserver, Caucho Resin on my Windows 7 box
> fails to support php.  The same code produced a blank page with
> nothing.

If you want a PHP / MySQL / Apache setup on a windows box for code
development, check out http://www.wampserver.com

Rgds

Denis McMahon
0
1/3/2011 1:38:39 PM
On Jan 3, 6:53=A0am, Jerry Stuckle <jstuck...@attglobal.net> wrote:
> On 1/2/2011 11:29 PM, justaguy wrote:
>
>
>
>
>
> >>> Jerry,
>
> >>> The code isn't working, so, of course it's not in production. =A0I'm
> >>> using an ISP's server, don't have a lot of control...
> >>> yes, you're right, I was lazy in error detection/tracking for this
> >>> piece of code, thought it should be fairly straight-forward.
>
> >>> Thanks.
>
> >>> Don
>
> >> Another point - get your own development machine going. It will make
> >> your life a lot easier. =A0Even if it's not the same OS and won't catc=
h
> >> every error (i.e. you wouldn't see this error on Windows), it will hel=
p
> >> you many other ways.
>
> >> And depending on the ISP's setup, ini_set() and/or your .htaccess file
> >> can change a number of PHP settings for a single script, every script =
on
> >> your site, or many steps in between.
>
> > Too bad, an open source webserver, Caucho Resin on my Windows 7 box
> > fails to support php. =A0The same code produced a blank page with
> > nothing.
>
> Why don't you just install Apache - it's open source, free and supports
> PHP just fine. =A0 It also happens to be the most used web server in the
> world.
>
> And BTW - I didn't say the code was "in production". =A0I said it was "on
> a production machine". =A0Two entirely different things.
>
> --
> =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
> Remove the "x" from my email address
> Jerry Stuckle
> JDS Computer Training Corp.
> jstuck...@attglobal.net
> =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D

Hi all, problem resolved with the same (my 'original' code).  My ISP
first adversely changed file attributes of another text file,
Terrible!  Which failed my software download routine, anyway, after
calling them up and instructing them to set it to rw rw rw, all's well
now.

But let me ask Jerry and all, how do we ensure the "target file" to
support concurrent users, if it's opened by user A, we need to lock it
or something... how do we do it with php?

Many thanks.

Don
0
donli (155)
1/3/2011 11:11:16 PM
On Jan 3, 8:38=A0am, Denis McMahon <denis.m.f.mcma...@googlemail.com>
wrote:
> On 03/01/11 04:29, justaguy wrote:
>
> > Too bad, an open source webserver, Caucho Resin on my Windows 7 box
> > fails to support php. =A0The same code produced a blank page with
> > nothing.
>
> If you want a PHP / MySQL / Apache setup on a windows box for code
> development, check outhttp://www.wampserver.com
>
> Rgds
>
> Denis McMahon

I can't, Denis, if I do it must mirror what my ISP has.  Thanks
though.  Don
0
donli (155)
1/3/2011 11:12:03 PM
On 1/3/2011 6:11 PM, justaguy wrote:
> On Jan 3, 6:53 am, Jerry Stuckle<jstuck...@attglobal.net>  wrote:
>> On 1/2/2011 11:29 PM, justaguy wrote:
>>
>>
>>
>>
>>
>>>>> Jerry,
>>
>>>>> The code isn't working, so, of course it's not in production.  I'm
>>>>> using an ISP's server, don't have a lot of control...
>>>>> yes, you're right, I was lazy in error detection/tracking for this
>>>>> piece of code, thought it should be fairly straight-forward.
>>
>>>>> Thanks.
>>
>>>>> Don
>>
>>>> Another point - get your own development machine going. It will make
>>>> your life a lot easier.  Even if it's not the same OS and won't catch
>>>> every error (i.e. you wouldn't see this error on Windows), it will help
>>>> you many other ways.
>>
>>>> And depending on the ISP's setup, ini_set() and/or your .htaccess file
>>>> can change a number of PHP settings for a single script, every script on
>>>> your site, or many steps in between.
>>
>>> Too bad, an open source webserver, Caucho Resin on my Windows 7 box
>>> fails to support php.  The same code produced a blank page with
>>> nothing.
>>
>> Why don't you just install Apache - it's open source, free and supports
>> PHP just fine.   It also happens to be the most used web server in the
>> world.
>>
>> And BTW - I didn't say the code was "in production".  I said it was "on
>> a production machine".  Two entirely different things.
>>
>
> Hi all, problem resolved with the same (my 'original' code).  My ISP
> first adversely changed file attributes of another text file,
> Terrible!  Which failed my software download routine, anyway, after
> calling them up and instructing them to set it to rw rw rw, all's well
> now.
>
> But let me ask Jerry and all, how do we ensure the "target file" to
> support concurrent users, if it's opened by user A, we need to lock it
> or something... how do we do it with php?
>
> Many thanks.
>
> Don

It should NOT be world writable, which is one security exposure. 
Another is that anyone can download the file at any time, because it's 
in your web site's root directory.  Both are bad.  At the very least you 
should protect access through .htaccess, and better is to have it 
outside your web site's root.

You can single thread access on some OS's - see flock() in the PHP 
documentation.  But better is to use a database, which will solve all 
these problems.


-- 
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
0
jstucklex (14659)
1/4/2011 12:37:44 AM
On 1/3/2011 6:12 PM, justaguy wrote:
> On Jan 3, 8:38 am, Denis McMahon<denis.m.f.mcma...@googlemail.com>
> wrote:
>> On 03/01/11 04:29, justaguy wrote:
>>
>>> Too bad, an open source webserver, Caucho Resin on my Windows 7 box
>>> fails to support php.  The same code produced a blank page with
>>> nothing.
>>
>> If you want a PHP / MySQL / Apache setup on a windows box for code
>> development, check outhttp://www.wampserver.com
>>
>> Rgds
>>
>> Denis McMahon
>
> I can't, Denis, if I do it must mirror what my ISP has.  Thanks
> though.  Don

Not a problem, if you code your scripts correctly.

-- 
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
0
jstucklex (14659)
1/4/2011 12:38:36 AM
justaguy wrote:
> On Jan 3, 6:53 am, Jerry Stuckle <jstuck...@attglobal.net> wrote:
>> On 1/2/2011 11:29 PM, justaguy wrote:
>>
>>
>>
>>
>>
>>>>> Jerry,
>>>>> The code isn't working, so, of course it's not in production.  I'm
>>>>> using an ISP's server, don't have a lot of control...
>>>>> yes, you're right, I was lazy in error detection/tracking for this
>>>>> piece of code, thought it should be fairly straight-forward.
>>>>> Thanks.
>>>>> Don
>>>> Another point - get your own development machine going. It will make
>>>> your life a lot easier.  Even if it's not the same OS and won't catch
>>>> every error (i.e. you wouldn't see this error on Windows), it will help
>>>> you many other ways.
>>>> And depending on the ISP's setup, ini_set() and/or your .htaccess file
>>>> can change a number of PHP settings for a single script, every script on
>>>> your site, or many steps in between.
>>> Too bad, an open source webserver, Caucho Resin on my Windows 7 box
>>> fails to support php.  The same code produced a blank page with
>>> nothing.
>> Why don't you just install Apache - it's open source, free and supports
>> PHP just fine.   It also happens to be the most used web server in the
>> world.
>>
>> And BTW - I didn't say the code was "in production".  I said it was "on
>> a production machine".  Two entirely different things.
>>
>> --
>> ==================
>> Remove the "x" from my email address
>> Jerry Stuckle
>> JDS Computer Training Corp.
>> jstuck...@attglobal.net
>> ==================
> 
> Hi all, problem resolved with the same (my 'original' code).  My ISP
> first adversely changed file attributes of another text file,
> Terrible!  Which failed my software download routine, anyway, after
> calling them up and instructing them to set it to rw rw rw, all's well
> now.
> 
> But let me ask Jerry and all, how do we ensure the "target file" to
> support concurrent users, if it's opened by user A, we need to lock it
> or something... how do we do it with php?
> 
> Many thanks.
> 
> Don
create a lock file
0
tnp (2409)
1/4/2011 4:16:53 AM
On Jan 3, 7:37=A0pm, Jerry Stuckle <jstuck...@attglobal.net> wrote:
> On 1/3/2011 6:11 PM, justaguy wrote:
>
>
>
> > On Jan 3, 6:53 am, Jerry Stuckle<jstuck...@attglobal.net> =A0wrote:
> >> On 1/2/2011 11:29 PM, justaguy wrote:
>
> >>>>> Jerry,
>
> >>>>> The code isn't working, so, of course it's not in production. =A0I'=
m
> >>>>> using an ISP's server, don't have a lot of control...
> >>>>> yes, you're right, I was lazy in error detection/tracking for this
> >>>>> piece of code, thought it should be fairly straight-forward.
>
> >>>>> Thanks.
>
> >>>>> Don
>
> >>>> Another point - get your own development machine going. It will make
> >>>> your life a lot easier. =A0Even if it's not the same OS and won't ca=
tch
> >>>> every error (i.e. you wouldn't see this error on Windows), it will h=
elp
> >>>> you many other ways.
>
> >>>> And depending on the ISP's setup, ini_set() and/or your .htaccess fi=
le
> >>>> can change a number of PHP settings for a single script, every scrip=
t on
> >>>> your site, or many steps in between.
>
> >>> Too bad, an open source webserver, Caucho Resin on my Windows 7 box
> >>> fails to support php. =A0The same code produced a blank page with
> >>> nothing.
>
> >> Why don't you just install Apache - it's open source, free and support=
s
> >> PHP just fine. =A0 It also happens to be the most used web server in t=
he
> >> world.
>
> >> And BTW - I didn't say the code was "in production". =A0I said it was =
"on
> >> a production machine". =A0Two entirely different things.
>
> > Hi all, problem resolved with the same (my 'original' code). =A0My ISP
> > first adversely changed file attributes of another text file,
> > Terrible! =A0Which failed my software download routine, anyway, after
> > calling them up and instructing them to set it to rw rw rw, all's well
> > now.
>
> > But let me ask Jerry and all, how do we ensure the "target file" to
> > support concurrent users, if it's opened by user A, we need to lock it
> > or something... how do we do it with php?
>
> > Many thanks.
>
> > Don
>
> It should NOT be world writable, which is one security exposure.
> Another is that anyone can download the file at any time, because it's
> in your web site's root directory. =A0Both are bad. =A0At the very least =
you
> should protect access through .htaccess, and better is to have it
> outside your web site's root.
>
> You can single thread access on some OS's - see flock() in the PHP
> documentation. =A0But better is to use a database, which will solve all
> these problems.
>
> --
> =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
> Remove the "x" from my email address
> Jerry Stuckle
> JDS Computer Training Corp.
> jstuck...@attglobal.net
> =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D


"
It should NOT be world writable,
"
world writable means to let Internet users to write to it, so, the
global w attribute is removed, how could an anonymous user
write to it?


"and better is to have it outside your web site's root. "
Yes, for now it's for dev purpose, so, I didn't care.

"
You can single thread access on some OS's - see flock() in the PHP
documentation.
"
sample code?  Since I'm not a php coder and don't intend to become
one, at least not now... could you save me some time?

"
But better is to use a database, which will solve all these problems.
"
No, not practical, this is a temp task (a poll).

Thanks.

0
donli (155)
1/5/2011 3:19:55 AM
On 1/4/2011 10:19 PM, justaguy wrote:
> On Jan 3, 7:37 pm, Jerry Stuckle<jstuck...@attglobal.net>  wrote:
>> On 1/3/2011 6:11 PM, justaguy wrote:
>>
>>
>>
>>> On Jan 3, 6:53 am, Jerry Stuckle<jstuck...@attglobal.net>    wrote:
>>>> On 1/2/2011 11:29 PM, justaguy wrote:
>>
>>>>>>> Jerry,
>>
>>>>>>> The code isn't working, so, of course it's not in production.  I'm
>>>>>>> using an ISP's server, don't have a lot of control...
>>>>>>> yes, you're right, I was lazy in error detection/tracking for this
>>>>>>> piece of code, thought it should be fairly straight-forward.
>>
>>>>>>> Thanks.
>>
>>>>>>> Don
>>
>>>>>> Another point - get your own development machine going. It will make
>>>>>> your life a lot easier.  Even if it's not the same OS and won't catch
>>>>>> every error (i.e. you wouldn't see this error on Windows), it will help
>>>>>> you many other ways.
>>
>>>>>> And depending on the ISP's setup, ini_set() and/or your .htaccess file
>>>>>> can change a number of PHP settings for a single script, every script on
>>>>>> your site, or many steps in between.
>>
>>>>> Too bad, an open source webserver, Caucho Resin on my Windows 7 box
>>>>> fails to support php.  The same code produced a blank page with
>>>>> nothing.
>>
>>>> Why don't you just install Apache - it's open source, free and supports
>>>> PHP just fine.   It also happens to be the most used web server in the
>>>> world.
>>
>>>> And BTW - I didn't say the code was "in production".  I said it was "on
>>>> a production machine".  Two entirely different things.
>>
>>> Hi all, problem resolved with the same (my 'original' code).  My ISP
>>> first adversely changed file attributes of another text file,
>>> Terrible!  Which failed my software download routine, anyway, after
>>> calling them up and instructing them to set it to rw rw rw, all's well
>>> now.
>>
>>> But let me ask Jerry and all, how do we ensure the "target file" to
>>> support concurrent users, if it's opened by user A, we need to lock it
>>> or something... how do we do it with php?
>>
>>> Many thanks.
>>
>>> Don
>>
>> It should NOT be world writable, which is one security exposure.
>> Another is that anyone can download the file at any time, because it's
>> in your web site's root directory.  Both are bad.  At the very least you
>> should protect access through .htaccess, and better is to have it
>> outside your web site's root.
>>
>> You can single thread access on some OS's - see flock() in the PHP
>> documentation.  But better is to use a database, which will solve all
>> these problems.
>>
>
>
> "
> It should NOT be world writable,
> "
> world writable means to let Internet users to write to it, so, the
> global w attribute is removed, how could an anonymous user
> write to it?
>

No, world writable means anyone else on that server can write to it - 
including other users.  You may have 150 web sites on a shared server, 
with each web site having its own users.  Any of them can overwrite your 
file.

>
> "and better is to have it outside your web site's root."
> Yes, for now it's for dev purpose, so, I didn't care.
>

Doesn't matter.  If you start with good habits, you have fewer problems 
later.

> "
> You can single thread access on some OS's - see flock() in the PHP
> documentation.
> "
> sample code?  Since I'm not a php coder and don't intend to become
> one, at least not now... could you save me some time?
>

At my normal consulting rates, I can.  This newsgroup is for users 
helping users, not free consulting.

> "
> But better is to use a database, which will solve all these problems.
> "
> No, not practical, this is a temp task (a poll).
>
> Thanks.
>

Still better to use a database - and completely practical.


-- 
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================
0
jstucklex (14659)
1/5/2011 3:54:21 AM
On 05/01/11 03:19, justaguy wrote:

> "
> It should NOT be world writable,
> "
> world writable means to let Internet users to write to it, so, the
> global w attribute is removed, how could an anonymous user
> write to it?

No. The file system access attributes control access for the server file
system. Internet users are not logged on to the server as users. The web
server process is the user that is accessing the file.

eg If your web server process runs as user www_data then www_data will
be the owner of any file it creates, and will only need owner rw access
to read and write the file.

> "
> You can single thread access on some OS's - see flock() in the PHP
> documentation.
> "
> sample code?  Since I'm not a php coder and don't intend to become
> one, at least not now... could you save me some time?

There's usually plenty of code examples in the user added notes to the
php manual. Go read.

> "
> But better is to use a database, which will solve all these problems.
> "
> No, not practical, this is a temp task (a poll).

The database functionality may already be there, simply waiting for you
to take advantage of it.

Rgds

Denis McMahon
0
1/5/2011 8:47:07 AM
On 05/01/11 03:19, justaguy wrote:

> "
> You can single thread access on some OS's - see flock() in the PHP
> documentation.
> "
> sample code?  Since I'm not a php coder and don't intend to become
> one, at least not now... could you save me some time?

This isn't perfect, but should allow you to output votes to a file that
you can tally later. If the file can't be seized for append, it backs
off up to 10 times for a random interval of 0.1 +- 0.05 secs and tries
again. It may be more robust than your approach. Doubtless others here
will pick it apart, but they prefer to do that rather than offer solutions.

<?php

// Use one of these file names as appropriate to your setup
$file = "/home/justaguy/path/poll_result.log";
$file = "data/poll_result.log";
$file = "poll_result.log";

$v = "no";
if ($vote == 1) $v = "yes";
$info = $_SERVER['REMOTE_ADDR'] . " " . date(DATE_RFC2822);

$vstr = $v . " " . $info . "\n";

$tries = 10;
while (!$fh = @fopen($file,"a") && $tries--) usleep(rand(50000,150000));
if ($fh) {
  fwrite($fh,$vstr);
  fclose($fh);
}
else {
  error_log($info . ' open poll_result.log for append failed.');
}

?>

If after 10 attempts it still fails, it puts a message in wherever the
error log is defined as.

Code to open, read and tally the votes is left as an exercise for the OP.

Note, however, that this won't solve your original problem, which seems
to be that the process that is executing the php doesn't have write
access to the directory that the file is being created in.

Rgds

Denis McMahon
0
1/5/2011 1:00:08 PM
Reply: