Also note that you can reduce the fopen/fwrite/fclose function calls down to a single call to [man]file_put_contents/man (don't forget the FILE_APPEND flag).
email validation and storage
Thank you all for your help, although I am hopeless stuck on as to why this doesn't work..
$email = filter_var($_POST['email'], FILTER_VALIDATE_EMAIL);
$filename = "../../../DataFiles/RegisteredEmail/EmailIndex.txt";
//valid email
if(filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)
int file_put_contents($filename, $email, FILE_APPEND);
else
{
echo "invalid submission";
}
I get this error:
Parse error: syntax error, unexpected T_STRING in C:\xampp\htdocs\Aaron White\view\processEmailRegistration.php on line 17
line 17 is: int file_put_contents($filename, $email, FILE_APPEND);
Any ideas?
again thanks!
-tama198-
Why do you have "int" before the call to file_put_contents() ?
That was me just trying something else and forgot to take it out... although it has no change on the error.
<?php
$email = filter_var($_POST['email'], FILTER_VALIDATE_EMAIL);
$filename = "../../../DataFiles/RegisteredEmail/EmailIndex.txt";
//valid email
if(filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)
file_put_contents($filename, $email, FILE_APPEND);
else
{
echo "invalid submission";
}
?>
This is exactly what I have now and i'm beyond stumped why that error keeps coming up.
-tama198-
The error is on this line:
if(filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)
You're missing a ')' parenthesis to close the if() statement.
EDIT: Also note that you aren't appending any separator character (such as a line break) at the end of the e-mail address, so you're going to see a file in the form:
foo@example.combar@example.comfoobar@example.com
wow thank you bradgrafelman,
I definitely need a break if i'm missing simple syntax like that. And thank you for the note about the separation. I will be sure to add that.
I must have a logical error somewhere still because, the file_put_contents function never gets called, it just jumps to invalid submission. but that is another situation entirely..
Thanks again for your help bradgrafelman,
-tama198-
An issue I see is with this line:
$email = filter_var($_POST['email'], FILTER_VALIDATE_EMAIL);
a) You're not checking if $_POST['email'] exists before putting it through the filter_var() function
b) You're not checking what the actual result of the filter_var() function is. What if it's not a valid email address and it returns false? You then use it later in the file_put_contents() function.
Also, you seem to be running the filter_var() function again on the $_POST['email'] variable, but you've already verified that it's a correct email address.
Thanks Bonesnap,
I may have misunderstood the purpose of this function:
file_put_contents($filename, $email, FILE_APPEND);
but it is my understanding that the "FILE_APPEND" flag will check to see if it is already listed in the file, if not, add it, if it is, overwrite it. However my knowledge of php is very introductory and if that code is wrong, what would you suggest for fix it.
Additionally, the email addresses are adding to the file I have set up, although like bradgrafelman mentioned earlier, they are running together and not seperated on each line. The code I have for that is this:
$file = fopen('../../../EmailIndex.txt', 'ab');
fwrite($file, "$email\n");
fclose($file);
and
file_put_contents("../../../EmailIndex.txt", "$email\n", FILE_APPEND);
These two segments of code are virtually the same, but neither writes the data to the file on seperate lines.
Thanks again for you input
-tama198-
tama198;10998577 wrote:Thanks Bonesnap,
I may have misunderstood the purpose of this function:
file_put_contents($filename, $email, FILE_APPEND);
but it is my understanding that the "FILE_APPEND" flag will check to see if it is already listed in the file, if not, add it, if it is, overwrite it.
Not only does that explanation sound incorrect, but that has nothing to do with what Bonesnap was referring to.
First, the FILE_APPEND flag simply tells PHP to append whatever data you're trying to write to the end of the file referenced (or create the file if it doesn't exist); the default behavior is similar to using the 'w' flag for fopen() in that an existing file will be overwritten.
What Bonesnap was referring to, however, was whether or not the variable $_POST['email'] exists. You should always verify that external data exists (e.g. via [man]isset/man or [man]empty/man) before you attempt to access it.
tama198;10998577 wrote:
$file = fopen('../../../EmailIndex.txt', 'ab'); fwrite($file, "$email\n"); fclose($file);
and
file_put_contents("../../../EmailIndex.txt", "$email\n", FILE_APPEND);
These two segments of code are virtually the same, but neither writes the data to the file on seperate lines.
Au contraire, both of those code snippets append a LF (line feed) character after the data being written. (If you're not seeing multiple lines when you view the text file, it's likely because you're using a text editor that is too stupid to realize what a line break could be. In other words, you're probably using a Microsoft product like Notepad. )
This is everything I Have right now. What it does as of now is, get the email address from a form, checks to see if the value is empty, and writes it to a text file. What I can't figure out now is why the code will continue to write to the file, when there is an email already present. Is the strpos function syntactically correct? or is it just the incorrect function all together?
The file I'm storing the addresses in is a notepad++ text file, and the data is formatted on every line.
<?php
$filepath = "../../../EmailIndex.txt";
if(empty($_POST['useremail']))
{
echo "Please enter an email address";
}
else
{
$email = filter_var($_POST['useremail'], FILTER_VALIDATE_EMAIL);
if(filter_var($_POST['useremail'], FILTER_VALIDATE_EMAIL))
{
$file = fopen("$filepath", "ab");
$pos = strpos("$file", "$email");
if($pos)
{
echo "Email already exists";
}
else
{
fwrite($file, "$email\n");
fclose($file);
echo "File was successfully added";
}
}
}
?>
Again thanks for your patience in helping me
-tama198-
This:
$pos = strpos("$file", "$email");
makes no sense, because you're trying to use a [man]resource[/man] (e.g. one returned by [man]fopen/man) inside of a [man]string[/man]. If you tried to echo $file, you'd probably just see the string 'Resource id #2' or something similar.
What you need to do is actually search through the file's contents and check to see if any of the existing lines match the given e-mail address. Since you're using line breaks as delimiters, one easy way to do this would be to use [man]file/man (with the FILE_IGNORE_NEW_LINES flag) to load the entire file's contents into an array and then use [man]in_array/man to check if a given e-mail address is already contained in that array.
But here's a better recommendation: stop trying to (very primitively and incompletely) imitate a DBMS with your text-file database and instead use a real DBMS (such as MySQL).
You still haven't addressed this line:
$email = filter_var($_POST['useremail'], FILTER_VALIDATE_EMAIL);
The filter_var() function either returns the filtered data or false on failure, so if $_POST['useremail'] is an improper email address, which is entirely possible, then $email will be false.
But then the very next line you check $_POST['useremail'] again to see if it's valid. Why not just check it once?