PHP Tennis

Ye' old general discussion board. Basically, for everything that isn't covered elsewhere. Come here to shoot the breeze, shoot your mouth off, or whatever suits your fancy.
This forum is not for asking programming related questions.

Moderator: General Moderators

User avatar
hex
Forum Commoner
Posts: 92
Joined: Sat Apr 20, 2002 3:20 am
Location: UK

PHP Tennis

Post 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!
User avatar
protokol
Forum Contributor
Posts: 353
Joined: Fri Jun 21, 2002 7:00 pm
Location: Cleveland, OH
Contact:

Post by protokol »

Awww, come on .. only 3 lines?? How about 5?
User avatar
hex
Forum Commoner
Posts: 92
Joined: Sat Apr 20, 2002 3:20 am
Location: UK

Post 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...
jason
Site Admin
Posts: 1767
Joined: Thu Apr 18, 2002 3:14 pm
Location: Montreal, CA
Contact:

Post 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 =)
EricS
Forum Contributor
Posts: 183
Joined: Thu Jul 11, 2002 12:02 am
Location: Atlanta, Ga

Post 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); 
?>
EricS
Forum Contributor
Posts: 183
Joined: Thu Jul 11, 2002 12:02 am
Location: Atlanta, Ga

Post 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.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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); 
?>
User avatar
protokol
Forum Contributor
Posts: 353
Joined: Fri Jun 21, 2002 7:00 pm
Location: Cleveland, OH
Contact:

Post 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]
User avatar
hob_goblin
Forum Regular
Posts: 978
Joined: Sun Apr 28, 2002 9:53 pm
Contact:

Post 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
User avatar
hex
Forum Commoner
Posts: 92
Joined: Sat Apr 20, 2002 3:20 am
Location: UK

Post 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.
User avatar
hex
Forum Commoner
Posts: 92
Joined: Sat Apr 20, 2002 3:20 am
Location: UK

Post 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
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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 

?>
User avatar
protokol
Forum Contributor
Posts: 353
Joined: Fri Jun 21, 2002 7:00 pm
Location: Cleveland, OH
Contact:

Post 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

?>
User avatar
hob_goblin
Forum Regular
Posts: 978
Joined: Sun Apr 28, 2002 9:53 pm
Contact:

Post 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
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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");
?>
Locked