Visit the Official PHP Website |
PHP ProgrammingBy James N HitzThe Code Explained: Un-SubscriptionThe unsubscription section is the one with the bulk of file management functions. It starts off by checking whether the email address was wrong (in which case $outString will be longer than zero). If it is, the user is given a chance to correct: 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(); } To unsubscribe the user, we will need to open the original file ($datafile) and a temporary file ($tempfile). We then read the entire datafile into the array $allFile. We step through the array - one line at a time and test whether the email address supplied matches the email address saved. If it is found, we change the value of $found to 1 else, we record the read line into the temp file. $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"); $allFile = file($datafile); //read entire file into array while(list($linenum,$line) = each($allFile)){ $items = preg_split("/\|/",$line); if( preg_match("/^$mail$/", $items[2]) ){ $found = 1; } else { fwrite($tmp,"$line"); } } At this time, the temp file contains the entire data file EXCEPT for the one line containing the email address of the unsubsriber. On top of that the $found variable is set to one. This assumes that the email address was found. If the email address was NOT found, then at this moment, the temp file and the data file will be the same, and $found will bot have been set. In either case, if we close both the files, delete the data file and copy the temp file into the datafile, we will have a clean copy with what we want. We then delete the temp file: fclose($tmp); fclose($dfHandle); unlink($datafile) or die("Cannot erase datafile"); copy($tempfile,$datafile); unlink($tempfile) or die("Cannot erase tempfile"); This part makes use of exit() and is thus a little longer. We could have used rename instead. Saves you a line of code: fclose($tmp) $fclose($dfHandle) unlink($datafile) or die("Cannot erase datafile"); rename($tmp,$datafile); If all was well, the variable $found was set and the record was erased. Otherwise, the record could not be found in our database. Read TYPO error or even a possible blinky SNOOP. if($found){ print "Your email address $mail has been successfully". " removed from the PHP mailing List"; } else { print "The specified email address $mail does not exist ". "in the database. Please ensure that you typed it correctly"; } See? This code is really fun and... that is all it takes to unsubscribe. Let's look into the third and last statement block. My afterthought. This is to be used by webmasters to list all the subscribed members. << Code Explanation: Subscribe | Code Explanation: Listing >> |
JamHitz Productions