Make your own free website on Tripod.com

Visit the Official PHP Website

Search this Site

PHP Programming

By James N Hitz

The Code

The code that accepts and processes the form (/php-bin/mail_lst.php) is as follows:

<html><head><title>PHP Mailing List</title></head>
<body>
<h2>PHP Mailing List</h2>
<?php
  $datafile = '/www/home/php-bin/list.file';
  $formTop = "<form action=\"http://localhost/php-bin/mail_list.php\" method=POST>";
  $formBot = "<br><input type='reset' value='clear'> <input type='submit' value='Ok'>";

  if(!preg_match("/^[\w_\.]+\@[\w_\.]+\.(\w){2,3}$/",$mail)){
    $outString = "Email Address: <input type='text' name='mail'><br>";
  } else {
    $formOk = "<input type=\"hidden\" name=\"mail\" value=\"$mail\">";
  }

if($action == 1){ //if we are subscribing...
	if(!preg_match("/^\w+$/", trim($fName))){
	   $outString .= "First Name: <input type='text' name='fName'><br>";
        }else{
           $formOk .= "<input type=\"hidden\" name=\"fName\" value=\"$fName\">";
        }
	
  if(!preg_match("/^\w+$/", trim($lName))){
     $outString .= "Last Name: <input type='text' name='lName'><br>";
  } else {
     $formOk .= "<input type=\"hidden\" name=\"lName\" value=\"$lName\">";
  }
  
  if(strlen($outString) > 0){
    print "<h2>Subscription Error</h2>".
      "<p>Please supply valid information in the relevant field(s)".
      " below.  This is information is required:</p>". 
	$formTop.$outString.$formOk.
	"<input type='hidden' value=1 name='action'>" . $formBot;
    exit();
  }

  if(!$dfHandle = fopen($datafile,'a+')){
     print "Internal Error.  Cannot open data file";
     exit();
  }
  //read entire file into array (this has its dis-advantages)
  $allFile = file($datafile);
  reset($allFile); //rewind array pointer
			
  //search if email address already exists in list
  while(list($linenum,$line) = each($allFile)){
     $items = preg_split("/\|/",$line);
     if( preg_match("/^$mail$/", $items[2]) ){
         print "<p>Cannot add <b>$items[2]</b>. ".
          "It already exists in the mailing list.";
     exit();
     }
  }
  unset($allFile); //release variable - save RAM
		    
  //email does not exist. Add it and thank user
  $writeString = "$fName|$lName|$mail\n";
  fwrite($dfHandle,$writeString);
  fclose($dfHandle);
  print "<p>Thank you <b>$lName</b>. ".
   "Your address <b>$mail</b> has been added".
   " to the PHP mailing list";
	
} elseif($action ==2) { //We  are unsubsribing
   if(strlen($outString >0)){
     print "<h3>UnSubscription Error</h3>".
      "<p>The specified email address is invalid. ".
      "Please re-enter:" .$formTop.$outString.
      "<input type='hidden' value=2 name='action'>" . $formBot;
      exit();
   }
				  
   $tempfile = "./temp.file";
   $dfHandle = fopen($datafile,'r') or die("Error. Cannot open data file.");
   $tmp = fopen($tempfile, 'w') or die("Cannot create or open temp file");
		
   //read entire file into array
   $allFile = file($datafile);
   while(list($linenum,$line) = each($allFile)){
       $items = preg_split("/\|/",$line);
       if( preg_match("/^$mail$/", $items[2]) ){
           $found = 1;
       } else {
           fwrite($tmp,"$line");
       }
   }

   fclose($tmp);
   fclose($dfHandle);
   unlink($datafile) or die("Cannot erase datafile");
   copy($tempfile,$datafile);
   unlink($tempfile) or die("Cannot erase tempfile");
   if($found){
      print "Your email address <b>$mail</b> has been successfully".
      " removed from the PHP mailing List";
   }else {
      print "The specified email address <b>$mail</b> does not exist ".
       "in the database.  Please ensure that you typed it correctly";
   }
		
} elseif($action == 3){
   $dfHandle = fopen($datafile,'r') or die("Internal Error.  Cannot open data file");
   echo "<h3>Current Subscriptions to the PHP Mailing List</h3>";
   echo "<table width=\"100%\" bgcolor=\"#FFFFCC\" border ".
   "cellspacing='0' cellpadding='3'>\n".
   "<tr bgcolor='#FFCC99'><td>First Name</td><td>Last Name</td>".
   "<td>Email Address</td></tr>";
   $allFile = file($datafile);		
   while(list($linenum, $line)= each($allFile)){
       $items = preg_split("/\|/",$line);
       print "<tr><td>$items[0]</td><td>$items[1]</td><td>$items[2]</td></tr>\n";
    }
    echo "</table>";
    }
?>
</body></html>

This code is VERY lengthy. The longest so far. We'll explain it slowly and patiently until we are through with it. You may need to read and re-read the explanations and hopefully things will be Aw-Okey! Go!

<< The Interface - mail_list.html | More Functions >>

JamHitz Productions