0

Possible Duplicate:
E-mail validation in php? please help

I have Ajax sign up form. The code checks if the email exists in mysql, if it does, it says "sorry your email exists" - otherwise it will insert the email into mySQL.

How do I check to see if they have inserted a valid email? Like a rule char@ext

<?php
$con = mysql_connect("localhost","user","pass");
mysql_select_db("mytable", $con);
if(isset($_GET['email'])){
    $e= $_GET['email'];
    $cc= mysql_real_escape_string($e);
    $r = mysql_query("SELECT * FROM `maillist` WHERE `email` = '".$cc."'");
        if (mysql_affected_rows()==0) {
            $r = mysql_query('INSERT INTO maillist (id, email) VALUES(NULL,"'.$cc.'")');
            echo $cc;
        } else {
            echo 'Your email already exists!';
        }   
}
mysql_close($con);
?>
1
  • 6
    There a lots of questions concerning email validation via PHP already. For example this one. Commented Nov 1, 2011 at 12:20

2 Answers 2

2

You can use PHP filters : http://php.net/manual/en/filter.filters.validate.php

Specifically : FILTER_VALIDATE_EMAIL

if (filter_var('[email protected]', FILTER_VALIDATE_EMAIL) === false) {
  //fake email
}
Sign up to request clarification or add additional context in comments.

1 Comment

Sweet. PHP is amazing. I would have to make a seperate function in ASP CLASSIC in VB and then make find replaces and checkers.. I think jQuery validation would be better making sure only emails go the PHP script but this is always good for fall backs for security. Especially for GET request. Thanks. REP ++
0

You should consult the docs on validate filters:

if (filter_var($someEmail, FILTER_VALIDATE_EMAIL)) {
    // ...
} 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.