Hi,

Friday, March 11, 2005, 6:30:52 PM, you wrote:

KM> -----Original Message-----
KM> From: Richard Lynch [mailto:[EMAIL PROTECTED] 
KM> Sent: Thursday, March 10, 2005 10:14 PM
KM> To: Ross Hulford
KM> Cc: php-general@lists.php.net
KM> Subject: Re: [PHP] incrementing a number from a text file

>>> I want to read a number from an external (txt) file and increment it.then
>>> save the number back on the text file.
>>> I know this is possible but want a simple amd economical way to do this.

>> That's what you *THINK* you want to do :-)

>> But what happens when *TWO* users hit that same script at exactly the same
>> time.

KM> <snip>

>> This is why so many early "hit counter" scripts back in the day were
>> always getting messed up and reset to 0.

KM> Donīt increase if counter is not read?

>> What you really want, almost for sure, is an SQL database with "sequences"

KM> I try NOT to use databases if not nessecary

>> You *can* use http://php.net/flock, but even that is a Bad Idea, because
>> flock under Un*x is self-imposed -- If some *other* program/script/user
>> decides not change that file and doesn't use flock, well, they're not
>> STOPPED from doing that.

KM> Poor programming?

>> flock is therefore all too subject to human fallibility when you re-work,
>> re-write, or add more code to your system.

KM> Every code is... if You donīt use Your head, the outcome will be...?

KM> If U no nothing about SQL, I use a database to a counter, I
KM> get a piece of code from a friend "alter table counter
KM> auto_increment = 1"; and Iīm curious to see, what it does? 

KM> Or better: I put my query in a link like <a
KM> href="db.php?q=select+*+from+customers">show customers</a> and
KM> Iīve got NO clue about the settings in the mysql db, so this gets
KM> "funny"
KM> <a href="db.php?q=delete+from+customers">show customers</a>
KM> <a href="db.php?q=drop+table+customers">show customers</a>

KM> *whistle*

KM> This _was_ an issue in a company I worked for, along with queries like:

KM> Select blablabla FROM t1,t2,t3
KM> WHERE customers LIKE '%$s%'
KM> OR name LIKE '%$s%'
KM> OR domain LIKE '%$s%'
KM> OR email LIKE '%$s%'
KM> OR log LIKE '%$s%'
KM> AND t1.id = t2.t1_id
KM> AND t1.id = t3.t1_id

KM> Horror!

KM> Kind regards
KM> Kim Madsen

KM> --
KM> PHP General Mailing List (http://www.php.net/)
KM> To unsubscribe, visit: http://www.php.net/unsub.php


Sorry to get in late here, try these scripts (probably unix only):

//count.php

<?php
//wait if another process has results.new
$x = 5; //5 seconds
while($x > 0 && file_exists('results.new')){
  sleep(1);
  clearstatcache();
  $x--;
}
if($x == 0) echo "Hung process detected <br>";
if(file_exists('results')){
  $count = intval(trim(file_get_contents('results')));
  $count ++;
}else{
  $count = 1;
}
$fp = fopen("results.new", "w");
if($fp) {
  fwrite($fp,$count."\n");
  exec("ln -f results results.old"); //incase it is being read
  exec("mv results.new results");
  echo "done count=$count\n";
  fclose($fp);
}


//testing script count.html

<html>
<head>
        <title>Counter Test</title>
</head>
<frameset cols="150,150,150,*" rows="*">
  <frame src="count.php" name="c1" frameborder="1">
  <frame src="count.php" name="c2" frameborder="1">
  <frame src="count.php" name="c3" frameborder="1">
  <frame src="count.php" name="c4" frameborder="1">
  <noframes>
    <body bgcolor="#FFFFFF">
      <p>need frames</p>
    </body>
  </noframes>
</frameset>
</html>


In theory no 2 frames should have the same number....
-- 
regards,
Tom

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to