Page 1 of 1

PHP script to check if email exists in a text.txt file

Posted: Tue Nov 21, 2006 9:14 pm
by maxtran2005
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Hi guys,

How do I check an input email from user to a text.txt file containing the list of email seperated by comma

emaillist.txtexample of email:

Code: Select all

1234@yaho.com,
asdf@asgb.com,
a24t@asf.com,
423yg@yasdf.com

Code: Select all

<?php

if ($_SERVER['REQUEST_METHOD'] == "POST") {
    	$email = strip_tags($email);
			
		$str = strtoupper(file_get_contents("emaillist.txt"));
			if (strpos($str, strtoupper($email)))
    			{
                                                                // stuff here
    				mail($email, $subject, $message);
    			}
			else
    			{
                                                                // stuff here
    				mail($email, $subject, $message);
    			} 
}

?>
The problem is that is goes to the else statement all the times so I think I didn't do it right. Any idea?

Thanks


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Tue Nov 21, 2006 9:34 pm
by feyd

Posted: Tue Nov 21, 2006 9:36 pm
by neophyte
Do you have register globals on? Otherwise I think $email should be referenced from the form like:

Code: Select all

$_POST['email'];

Posted: Tue Nov 21, 2006 9:38 pm
by maxtran2005
Here's the whole script

Code: Select all

<?php

if ($_SERVER['REQUEST_METHOD'] == "POST") {
    	$email = strip_tags($email);
			
		$str = strtoupper(file_get_contents("emaillist.txt"));
			if (strpos($str, strtoupper($email)))
    			{
    				$feedback = "You already subscribed.";
    				$subject = "Thank you";
    				$message = "$email and $feedback";
    				mail($email, $subject, $message);
    			}
			else
    			{
    				$feedback = "You haven't subscribed yet.  We will add you into our mailing list.";
    				$subject = "Sorry";
    				$message = "$email and $feedback";
    				mail($email, $subject, $message);
    			} 
}

?>

<html>
<head>
  <title></title>
</head>
<body>


<?php 
if ($_SERVER['REQUEST_METHOD'] == "POST") {
    
    $email = $_POST['email']; 
    echo("<p><b>The following message was sent to $email\n");

    }
    else {
?>


  <form action="<?php echo $_SERVER['PHP_SELF']; ?>" METHOD="POST">
    <p>
    Email: 
    <input type="text" name="email" size="25">
    </td>
    </tr>
      <input type="submit" value="Send Feedback">
      </table>
        </p>
  </form>


<?php } ?>

</body>
</html>

Posted: Tue Nov 21, 2006 9:48 pm
by neophyte

Code: Select all

$email = strip_tags($email);
Unless register globals is on $email is empty.

Code: Select all

$email =  strip_tags($_POST['email']);

Posted: Tue Nov 21, 2006 9:48 pm
by feyd
Forgot to mention, in_array() could also be used for the last couple parts.

Posted: Wed Nov 22, 2006 7:55 am
by maxtran2005
sorry to ask again, I can't make it work with what you said by adding the code below
$email = strip_tags($_POST['email']);

It still goes to the else statement. Other posts are helpful but I still don't know how to use explode() in_array() like u mentioned above.

Help please.

Posted: Wed Nov 22, 2006 2:56 pm
by feyd
What have you tried with the functions I've linked?