Hey ggrant3. I think that you were taking steps in the right direction with this. You mentioned in your post that you were new to PHP, so I decided to put some sample code together for you in the hopes that it will "click" when you see it. There's plenty of different ways you could go about solving your problem, but this is what I came up with:
Code: Select all
<?php
/**
* Adds a new email address to the customer storage file ($storageFile).
* The email address will be stored as lowercase with this function.
*
* @param string $emailAddress - (e.g. 'bob@anexample.com')
* @param string $storageFile - (e.g. '/tmp/myfile.txt')
* @return bool - true upon success
*/
function addCustomerEmailToFile($emailAddress, $storageFile) {
/* We create an array to store the existing customer email address for comparison. */
$existingCustomerEmailAddresses = array();
$fileContents = "";
/* If the file already exists on the disk, then we should read it. */
if(file_exists($storageFile)) {
if(!$fileContents = file_get_contents($storageFile)) {
die("An error occured while trying to read the customer storage file $storageFile.");
}
}
/* Then, we open the file $storageFile for append mode. */
if(!$fileHandle = fopen($storageFile, "a")) {
die('Could not open customer storage file.');
}
/* We explode on newline characters since we store one email address per line. */
if(!empty($fileContents)) $existingCustomerEmailAddresses = explode("\r\n", $fileContents);
/* We're only going to add the email address if it doesn't already exist in the file. */
if(!in_array(strtolower($emailAddress), $existingCustomerEmailAddresses)) {
if(fwrite($fileHandle, strtolower($emailAddress)."\r\n") === false) {
die('An error occured while writing to the customer storage file.');
}
}
/* Finally close the file handle. */
fclose($fileHandle);
return true;
}
if(isset($_POST["btnAdd"])) {
if(addCustomerEmailToFile($_POST["emailAddress"], "customer_emails.txt")) {
echo "Processed customer email address for " . $_POST["emailAddress"];
}
}
?>
<form name="frmAddEmail" action="<?php print($_SERVER["PHP_SELF"]); ?>" method="post">
<input type="textbox" name="emailAddress" />
<input type="submit" name="btnAdd" value="Add Email Address" />
</form>
Essentially, the above reads the file contents into an array should it already exist on the disk. Then, prior to writing the new address to the file, an in_array check happens to see if that email address was already present in the array we loaded up earlier. I'm using strtolower at the time of writing and comparison to remove any uppercase/lowercase comparison issues.
I'm not sure what sort of project you are working on, but is there a reason that the email addresses need to be stored in a flat file? It seems like a database could be a better option depending on your needs.
I would encourage you to use the above code as a rough picture of one way that your task could be accomplished. If you were going to write this for a production environment, I would highly recommend you to take the necessary steps to make the above code a little more secure.
Anywho, I hope this helps you out
