# Solved: PHP: fopen/fwrite: check if email address exist



## iammikerodriguez (Apr 24, 2010)

i'm working on a script for my newsletter subscription and i'm having a bit of trouble with the email list.

everything seems to work fine, but when i input my email a second time, it comes out double inside of the TXT file that all my email address are place.


```
<?php

$email = $_POST['email'];
$file = fopen("email.txt", "a");
fwrite($file, "\n" . $email);
fclose($file);


?>
```
i was wondering how can i prevent the script from writing the same email twice inside the txt file.

would it be something like:


```
if($email=$file)
{
echo "email already exist"
}
else {
fwrite($file, "\n" . $email);
fclose($file);
echo "thank you for signing up"
}
```
any help would be greatly appreciated!
thanks in advance


----------



## cpscdave (Feb 25, 2004)

any chance of using a DB? It'll make stuff a lot easier

That being said try something like:


```
$theFile = "somefile";
$fp = fopen($theFile, "r");
$content = fread($fp, filesize($theFile));
fclose($fp);
$emails = explode("\n",$content);
if(isset($emails[$_POST['email']]))
//email exists 
else
//add it
```


----------



## iammikerodriguez (Apr 24, 2010)

not using a DB. but if i were to use a DB, what simple script can i use to record data, and check it it exist within the tables of the database?


----------



## iammikerodriguez (Apr 24, 2010)

okay i did some revision to this code, would this seem to work? i keep getting an error, but i dont know what exactly i missed here. I'm a total noob to php scripting "/

*Parse error*: syntax error, unexpected T_ELSE in */home2/restles2/public_html/uproarmiami/test-list/test-list.php* on line *9

*


```
$emailpost = $_POST['email'];
$theFile = "testemail.txt";
$fp = fopen($theFile, "r");
$content = fread($fp, filesize($theFile));
fclose($fp);
$emails = explode("\n",$content);
if(isset($emails[$_POST['email']])){

echo "email already subscribed"

}
else {

fwrite($theFile, "\n" . $emailpost);
echo "thank you"

}
```
thanks for the help in advance!


----------



## cpscdave (Feb 25, 2004)

```
echo "email already subscribed"; //you missed ; here
```


```
echo "thank you"; //and here
```
testing rest of the code now


----------



## cpscdave (Feb 25, 2004)

```
<?php
$emailpost = $_POST['email'];
$theFile = "temp.log"; //change to whatever your file name is 
$fp = fopen($theFile, "r"); //open for reading 
$content = fread($fp, filesize($theFile));
fclose($fp);

if(strstr($content,$emailpost))
{

echo "email already subscribed";

}
else {

$fp = fopen($theFile, 'a'); //reopen so you can append
fwrite($fp, "_\n" . $emailpost); //the _ prevents .co emails from failing on .com ones 
fclose($fp); //fclose flushes the changes to the file 
echo "thank you";

}  
?>
```
try this. 
This should work better. But a DB would be the best way to do this


----------



## iammikerodriguez (Apr 24, 2010)

ehh sorted those errors out. thanks. but now a couple more errors! "/

*Warning*: fread() [function.fread]: Length parameter must be greater than 0 in */home2/restles2/public_html/uproarmiami/test-list/test-list.php* on line *5*

*Warning*: fwrite(): supplied argument is not a valid stream resource in */home2/restles2/public_html/uproarmiami/test-list/test-list.php* on line *15*
thank you

current code


```
$emailpost = $_POST['email'];
$theFile = "testemail.txt";
$fp = fopen($theFile, "r");
$content = fread($fp, filesize($theFile));
fclose($fp);
$emails = explode("\n",$content);
if(isset($emails[$_POST['email']])){

echo "email already subscribed";

}
else {

fwrite($theFile, "\n" . $emailpost);
echo "thank you";

}
```


----------



## cpscdave (Feb 25, 2004)

see my second post with code that works  

First error is happening cause the file doesn't exist. 
2 solutions 
wrap the whole read section around a if(fileexists($thefile)){... 

or just create a blank file

2nd error is cause you need to re-opne the file to write to it can't just provide the filename see the example I posted


----------



## iammikerodriguez (Apr 24, 2010)

THANK YOU!!!
worked perfectly fine! i got an error the first email i inputed but after the second one it seemed to work fine. thank you for the comments within the code, it help me understand.


----------



## cpscdave (Feb 25, 2004)

ya the error was most likely a result of the file not existing. 
I should've put a check in for that. *shrug*


----------



## iammikerodriguez (Apr 24, 2010)

it's okay lol you've done enough. do you have a link of yours that i could post on my site or blog or something?

helper always deserve some recognition :]


----------



## cpscdave (Feb 25, 2004)

just link to TSG


----------



## iammikerodriguez (Apr 24, 2010)

got it, thanks 

you've been a big help


----------

