By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,462 Members | 3,388 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,462 IT Pros & Developers. It's quick & easy.

How to create function to avoid repetitive code?

P: n/a
I have 3 files I need to write to - and need to make sure I have a lock on
each, and take action if I can't get a lock. The code below works, but I
know it could be more efficient if I used a function instead of the
repetitive code. Still kind of green in php.... Any help in creating a
function to eliminate the repeated file lock tests is appreciated. Thanks
in advance!

//***** write to visdata *****
$got_it = false;
$timeout = 5;
$new_visitor =
$visdate."|".
$visip."|".
$visos."|".
$visbrow."|".
$visisp;
$fp = fopen($visdata,"a");
/////******* CODE BELOW IS REPEATED**********
while (($got_it === false) && ($timeout != 0))
{
if (flock($fp, LOCK_EX))
{
fwrite($fp, $new_visitor."\n");
flock($fp, LOCK_UN);
$got_it = true;
}
else
{
usleep(100000);
--$timeout;
}
}
//echo "<br>got_it = ".$got_it;
//if cd not get lock after 5 tries (.5 sec.) skip and record failure
if (!$got_it)
{
$fp=fopen('/home/clearpoi/public_html/viscount_files/viscount_fail', 'a');
fwrite($fp,date("M d Y h:i a",time() + $timeadjust)."\n");
exit;
}
fclose($fp);
//***** write to viscounter *****
$got_it = false;
$timeout = 5;
$fp = fopen($viscounter,"a");
//*********REPEATED CODE**********
while (($got_it === false) && ($timeout != 0))
{
if (flock($fp, LOCK_EX))
{
fwrite($fp, time()."\n");
flock($fp, LOCK_UN);
$got_it = true;
}
else
{
usleep(100000);
--$timeout;
}
}
//$got_it = 0;
//echo "<br>got_it = ".$got_it;
//if cd not get lock after 5 tries (.5 sec.) skip and record failure
if (!$got_it)
{
$fp=fopen('/home/clearpoi/public_html/viscount_files/viscount_fail', 'a');
fwrite($fp,date("M d Y h:i a",time() + $timeadjust)."\n");
exit;
}
fclose($fp);
//get count for visdisplay
$avs = file($viscounter);
$v24h = 0;
$v30d = 0;
$v365d = 0;
foreach ($avs as $val)
{
if($val>(time()-(3600*24)))
{
++$v24h;
}
if($val>(time()-(3600*24*30)))
{
++$v30d;
}
if($val>(time()-(3600*24*365)))
{
++$v365d;
}
}
echo "<br>v24h = ".$v24h;
echo "<br>v30d = ".$v30d;
echo "<br>v365d = ".$v365d;
$vtotals = $v24h."|".$v30d."|".$v365d;
//***** write to visdisplay *****
$got_it = false;
$timeout = 5;
$fp=fopen($visdisplay, 'w');
//*********REPEATED CODE********
while (($got_it === false) && ($timeout != 0))
{
if (flock($fp, LOCK_EX))
{
fwrite($fp, $vtotals);
flock($fp, LOCK_UN);
$got_it = true;
}
else
{
usleep(100000);
--$timeout;
}
}
//echo "<br>got_it = ".$got_it;
//if cd not get lock after 5 tries (.5 sec.) skip and record failure
if (!$got_it)
{
$fp=fopen('/home/clearpoi/public_html/viscount_files/viscount_fail', 'a');
fwrite($fp,date("M d Y h:i a",time() + $timeadjust)."\n");
exit;
}
fclose($fp);

Jul 17 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Look on the web for "php functions" e.g.
http://builder.com.com/5100-6371-1045063.html

--
http://www.dbForumz.com/ This article was posted by author's request
Articles individually checked for conformance to usenet standards
Topic URL: http://www.dbForumz.com/PHP-create-f...ict129634.html
Visit Topic URL to contact author (reg. req'd). Report abuse: http://www.dbForumz.com/eform.php?p=432527
Jul 17 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.