Page 1 of 2

PHP Tennis

Posted: Fri Jul 19, 2002 2:25 pm
by hex
I would like to introduce to the PHPDN forums... PHP Tennis! As seen on the SitePoint Forums, the idea of the game is "serve" with a single script then "volley" with a revision of the "served" code, with some modifications.

The server must pick a piece of code from the Zend Code Gallery to begin. The next person then makes a modification of this code according to the limits set at the beginning of the game (lines-per-edit, etc). And so on and so forth until either someone posts code with a fatal error or the time limit is up.

Editing posts constitutes disqualification.

The winner is the person who submitted the last piece of working code.

The full (rather long) list of rules can be found here

____________________________________________________________


So here goes for the first match...

Sudden death (free for all)
Max lines per edit: 3
Time limit: None

Code: http://zend.com/codex.php?id=29&single=1

Code: Select all

<? 
$counter = "counter.txt";  

function displayCounter($counter) &#123; 
        $fp = fopen("$counter", "r"); 
        $num = fread( $fp, 100); 
        $fpsoma = ($num + 1); 
        fclose($fp); 
        $fa = fopen("$counter", "w"); 
        fwrite( $fa, $fpsoma, 100); 
        fclose($fa); 
        echo "<center><h1>Hit number $fpsoma</h1></center>"; 
&#125; 

displayCounter($counter); 
?>
Competitors step up!

Posted: Fri Jul 19, 2002 3:28 pm
by protokol
Awww, come on .. only 3 lines?? How about 5?

Posted: Fri Jul 19, 2002 3:44 pm
by hex
Ummmm... no. Those are the rules for this game... Just add 3 lines, then someone else can add the other two. Or something...

Posted: Fri Jul 19, 2002 8:52 pm
by jason
Okay, with a brief overview of the rules, I hope this satisfies them:

Code: Select all

<? 
$counter = "counter.txt";  

function displayCounter($counter, $prefix='', $suffix='')
&#123; 
	if ( file_exists($counter) ) &#123;
        $fp 	= fopen($counter, 'r'); 
        $num 	= fread($fp, 100); 
        $fpsoma = ($num + 1); 
        fclose($fp); 
        $fa = fopen($counter, 'w'); 
        fwrite($fa, $fpsoma, 100); 
        fclose($fa); 
        echo $prefix.$fpsoma.$suffix; 
	&#125;
&#125; 

displayCounter($counter); 
?>
Next =)

Posted: Fri Jul 19, 2002 11:38 pm
by EricS

Code: Select all

<? 
$counter = "counter.txt";  

function displayCounter($counter, $prefix='', $suffix='') 
&#123; 
   if ( file_exists($counter) ) &#123; 
        $fp    = fopen($counter, 'r'); 
        $num    = fread($fp, 100); 
        $fpsoma = ($num + 1); 
        fclose($fp); 
        $fa = fopen($counter, 'w'); 
        fwrite($fa, $fpsoma, 100); 
        fclose($fa); 
        echo $prefix.$fpsoma.$suffix; 
   &#125;
   else &#123;
         echo ("File not found.");
   &#125;
&#125; 

displayCounter($counter); 
?>

Posted: Sat Jul 20, 2002 12:10 am
by EricS
I forgot to comment the above code per the rules, anyway, I just add a little code to display an error message upon not finding the file specified.

Posted: Sat Jul 20, 2002 5:29 am
by volka

Code: Select all

function displayCounter($counter, $prefix='', $suffix='') 
&#123; 
   if ( file_exists($counter) ) &#123; 
        $fp    = fopen($counter, 'r'); 
        $num    = fread($fp, 100); 
        $fpsoma = ($num + 1); 
        fclose($fp); 
        $fa = fopen($counter, 'w'); 
        fwrite($fa, $fpsoma, 100); 
        fclose($fa); 
        echo $prefix.$fpsoma.$suffix;
        return TRUE; // someone might want to check this
   &#125; 
   else &#123; 
         echo ("File not found."); 
         return FALSE; // s.o
   &#125; 
&#125; 

displayCounter($counter); 
?>

Posted: Sat Jul 20, 2002 10:33 am
by protokol

Code: Select all

// open a file containing a list of already counted IP's and check that $ip is in the list
function checkUniqueVisitor($ip) &#123;
&#125;
function displayCounter($counter, $prefix='', $suffix='') 
&#123;
   if ( file_exists($counter) ) &#123; 
        $fp    = fopen($counter, 'r'); 
        $num    = fread($fp, 100); 
        $fpsoma = ($num + 1); 
        fclose($fp); 
        $fa = fopen($counter, 'w'); 
        fwrite($fa, $fpsoma, 100); 
        fclose($fa); 
        echo $prefix.$fpsoma.$suffix;
        return TRUE;
   &#125; 
   else &#123; 
         echo ("File not found."); 
         return FALSE;
   &#125; 
&#125; 

displayCounter("counter.txt"); // pass the counter file
?>
[/quote]

Posted: Sat Jul 20, 2002 11:43 am
by hob_goblin

Code: Select all

// open a file containing a list of already counted IP's and check that $ip is in the list 
function checkUniqueVisitor($ip) &#123; 
$ip = getenv('REMOTE_ADDR');
$_fp = fopen("ips.txt", 'a+');
fclose("$_fp");
&#125; 
function displayCounter($counter, $prefix='', $suffix='') 
&#123; 
   if ( file_exists($counter) ) &#123; 
        $fp    = fopen($counter, 'r'); 
        $num    = fread($fp, 100); 
        $fpsoma = ($num + 1); 
        fclose($fp); 
        $fa = fopen($counter, 'w'); 
        fwrite($fa, $fpsoma, 100); 
        fclose($fa); 
        echo $prefix.$fpsoma.$suffix; 
        return TRUE; 
   &#125; 
   else &#123; 
         echo ("File not found."); 
         return FALSE; 
   &#125; 
&#125; 

displayCounter("counter.txt"); // pass the counter file 
?>
lol, i was going to start using mysql, but i decided it'd be better off to go flat file, to stay consistent

Posted: Sat Jul 20, 2002 5:46 pm
by hex

Code: Select all

&lt;?php

class Counter {

    // open a file containing a list of already counted IP's and check that $ip is in the list 
    function checkUniqueVisitor($ip)
    { 
        $ip = getenv('REMOTE_ADDR'); 
        $_fp = fopen("ips.txt", 'a+'); 
        fclose("$_fp"); 
    }

    function displayCounter($counter, $prefix='', $suffix='') 
    { 
        if (file_exists($counter)) { 
            $fp    = fopen($counter, 'r'); 
            $num    = fread($fp, 100); 
            $fpsoma = ($num + 1); 
            fclose($fp); 
            $fa = fopen($counter, 'w'); 
            fwrite($fa, $fpsoma, 100); 
            fclose($fa); 
            echo $prefix.$fpsoma.$suffix; 
            return TRUE; 
        } else { 
            echo ("File not found."); 
            return FALSE; 
        } 
    } 

}

Counter::displayCounter("counter.txt"); //pass the counter file 

?&gt;
Put it all in a class.

Posted: Sat Jul 20, 2002 5:47 pm
by hex

Code: Select all

<?php 

class Counter &#123; 

// open a file containing a list of already counted IP's and check that $ip is in the list 
function checkUniqueVisitor($ip) 
&#123; 
$ip = getenv('REMOTE_ADDR'); 
$_fp = fopen("ips.txt", 'a+'); 
fclose("$_fp"); 
&#125; 

function displayCounter($counter, $prefix='', $suffix='') 
&#123; 
if (file_exists($counter)) &#123; 
$fp = fopen($counter, 'r'); 
$num = fread($fp, 100); 
$fpsoma = ($num + 1); 
fclose($fp); 
$fa = fopen($counter, 'w'); 
fwrite($fa, $fpsoma, 100); 
fclose($fa); 
echo $prefix.$fpsoma.$suffix; 
return TRUE; 
&#125; else &#123; 
echo ("File not found."); 
return FALSE; 
&#125; 
&#125; 

&#125; 

Counter::displayCounter("counter.txt"); //pass the counter file 

?>
Put it all in a class.

Posted again, used wrong tags :P

Posted: Sat Jul 20, 2002 7:05 pm
by volka

Code: Select all

<?php 
class Counter &#123; 
function checkUniqueVisitor($ip) 
&#123; 
   $ip = getenv('REMOTE_ADDR'); 
   $_fp = fopen("ips.txt", 'a+'); 
   fclose("$_fp"); 
&#125; 

function displayCounter($counter, $prefix='', $suffix='') 
&#123; 
   if (file_exists($counter)) &#123; 
      $fp = fopen($counter, 'r'); 
      $num = fread($fp, 100); 
      $fpsoma = ($num + 1); 
      fclose($fp); 
      $fa = fopen($counter, 'w'); 
      fwrite($fa, $fpsoma, 100); 
      fclose($fa); 
      echo $prefix.$fpsoma.$suffix; 
      return TRUE; 
   &#125; else &#123; 
      echo ("File not found."); 
      return FALSE; 
   &#125; 
&#125; 

&#125; 
$oCounter = new Counter; // what is a class without an instance of it ;)
Counter::displayCounter("counter.txt"); //pass the counter file 

?>

Posted: Sat Jul 20, 2002 8:55 pm
by protokol

Code: Select all

<?php 
class Counter &#123;
   function Counter() &#123; // what is a class without a constructor :-)
   &#125;   

   function checkUniqueVisitor($ip) &#123; 
      $ip = getenv('REMOTE_ADDR'); 
      $_fp = fopen("ips.txt", 'a+'); 
      fclose("$_fp"); 
   &#125; 

   function displayCounter($counter, $prefix='', $suffix='') &#123; 
      if (file_exists($counter)) &#123; 
         $fp = fopen($counter, 'r'); 
         $num = fread($fp, 100); 
         $fpsoma = ($num + 1); 
         fclose($fp); 
         $fa = fopen($counter, 'w'); 
         fwrite($fa, $fpsoma, 100); 
         fclose($fa); 
         echo $prefix.$fpsoma.$suffix; 
         return TRUE; 
      &#125; else &#123; 
         echo ("File not found."); 
         return FALSE; 
      &#125; 
   &#125;
&#125; 

$oCounter = new Counter;
$oCounter->displayCounter("counter.txt"); // instantiated object calls the function

?>

Posted: Sat Jul 20, 2002 11:23 pm
by hob_goblin

Code: Select all

<?php 
class Counter &#123; 
   function Counter() &#123; // what is a class without a constructor :-) 
   &#125;    

   function checkUniqueVisitor($ip) &#123; 
      $ip = getenv('REMOTE_ADDR'); 
      $arr = file("ips.txt");
      if(in_array($ip,$arr)&#123;
      return FALSE; &#125;
      $_fp = fopen("ips.txt", 'a+'); 
      fclose("$_fp"); 
   &#125; 

   function displayCounter($counter, $prefix='', $suffix='') &#123; 
      if (file_exists($counter)) &#123; 
         $fp = fopen($counter, 'r'); 
         $num = fread($fp, 100); 
         $fpsoma = ($num + 1); 
         fclose($fp); 
         $fa = fopen($counter, 'w'); 
         fwrite($fa, $fpsoma, 100); 
         fclose($fa); 
         echo $prefix.$fpsoma.$suffix; 
         return TRUE; 
      &#125; else &#123; 
         echo ("File not found."); 
         return FALSE; 
      &#125; 
   &#125; 
&#125; 

$oCounter = new Counter; 
$oCounter->displayCounter("counter.txt"); // instantiated object calls the function 

?>
continued with the IP checking

Posted: Sun Jul 21, 2002 5:41 am
by volka

Code: Select all

<?php 
class Counter &#123; 
   var m_id;  // instances should store something
   function Counter() &#123;
      m_id = uniqid(); // empty constructors aren't that nice
   &#125;    

   function checkUniqueVisitor($ip) &#123; 
      $ip = getenv('REMOTE_ADDR'); 
      $arr = file("ips.txt"); 
      if(in_array($ip,$arr)&#123; 
         return FALSE; &#125; 
      $_fp = fopen("ips.txt", 'a+'); 
      fclose("$_fp"); 
   &#125; 

   function displayCounter($counter, $prefix='', $suffix='') &#123; 
      if (file_exists($counter)) &#123; 
         $fp = fopen($counter, 'r'); 
         $num = fread($fp, 100); 
         $fpsoma = ($num + 1); 
         fclose($fp); 
         $fa = fopen($counter, 'w'); 
         fwrite($fa, $fpsoma, 100); 
         fclose($fa); 
         echo $prefix.$fpsoma.$suffix; 
         return TRUE; 
      &#125; else &#123; 
         echo ("File not found."); 
         return FALSE; 
      &#125; 
   &#125; 
&#125; 

$oCounter = new Counter; 
$oCounter->displayCounter("counter.txt");
?>