想阻止用户在表单中输入无效的电子邮件地址

I would like to be able to run a php validate script to stop users form entering gibberish as their email address. I know we can have the form input type as email but that can be easily bypassed in developer tools and the database integrity damaged.

My insert page looks like this:

$email = $conn->real_escape_string($_POST['emailpost']);
        $password = $conn->real_escape_string($_POST['passpost']);
        $firstname = $conn->real_escape_string($_POST['firstnamepost']);
        $lastname = $conn->real_escape_string($_POST['lastnamepost']);
        $phonenumber = $conn->real_escape_string($_POST['phonenumberpost']);
        $education = $conn->real_escape_string($_POST['institutionpost']);
        $facebook = $conn->real_escape_string($_POST['facebookpost']);
        $twitter = $conn->real_escape_string($_POST['twitterpost']);
        $instagram = $conn->real_escape_string($_POST['instagrampost']);

        $filename = $_FILES['uploadprofileimg']['name'];
        $filename = $ran.$filename;
        $filetmp = $_FILES['uploadprofileimg']['tmp_name'];
        $filetype = $_FILES['uploadprofileimg']['type'];
        move_uploaded_file($filetmp, "../userimages/".$filename);

    $insertuser = "INSERT INTO elmtree_users (user, email, pw, firstName, lastName, profileimg, learninginstitute, phone, facebook, twitter, instagram) VALUES
    ('$username', '$email', '$password', '$firstname', '$lastname', '$filename', '$education', '$phonenumber', '$facebook', '$twitter', '$instagram')";



        $resultinsert = $conn -> query($insertuser);

        if(!$resultinsert){

        echo $conn->error;
        }else{

        echo "<h2> Account successfully registered!</h2> 
                <h4>Please <a href='login.php'> <font class='text-success'><strong>login.</strong></font></a></h4><br><br><br><br>";

Like everyone is pointing out

making your own logging system is tricky. it required you to do additional steps to make the content secured. Not only to hackers but you as administrator of the database shouldn't have access to see your customers password in PlainText Most users will use the same password on your site as they used for there email password they registered with on your site..

It is more advisable to create login tools like laravel, Or simply research how to build a secure login system, because what we are seeing here in your code, is BAD, Not syntactically, but from a security stand point.

Me knowing you store passwords like that, I wouldn't register onto your website.

Any how not only that, But you really should have a look into mysqli binding Or even, and something I like better is PDO_Mysql Your code will not only be more clear to read, but will bind values directly to a a field within mysql ( no need to use real_escape_string no more )

Now to actually answer your question.

You probably should make some kind of javascript live validator on the field of your form directly.

then on PHP side, You can do a simple condition with REGXP and preg_match()

Have a look at https://regex101.com/r/SOgUIV/1 this is a regex that will validate EMAILs. With this link, You should then experiment a bit with it, it has not only documentation on the side but also possibles quantifier and such.

if(preg_match("/^((?!\.)[\w-_.]*[^.])(@\w+)(\.\w+(\.\w+)?[^.\W])$/i",trim($_POST['Email']))){
//What ever is in here will get process when $_POST['emailpost'] is valid.
}

Edited ----

As some user pointed out in comments. You would probably be better of using

if(filter_var($_POST['emailpost'],FILTER_VALIDATE_EMAIL){
  //What ever is in here will get process when $_POST['emailpost'] is valid
}

Also if you want to make sure user has access to the email address account, You could also add two column within your users table, isConfirmed,ConfirmationCode

When the user register, You create a unique code and put it into ConfirmationCode then send the user an email with something along those line "Please click the following link to activate account www.yourWebSite.com/confirmationPage.php?Code=$TheActualCodeYouCreatedForThatUser" Then once user get to that page, Change the field isConfirmed to '1' or true.

Once there on your website, you will be able to assume that only emails with isConfirmed is a real user.

To validate email you need to check a lot of stuff like

  1. if the email already exists
  2. if its a real email i.e check for presence of @
  3. check for funny characters which are not supposed to be in an email.

then always encrypt your password

if ($_POST['submit']) {
    $errors = array();

    $email = $conn->real_escape_string($_POST['emailpost']);
    $password = $conn->real_escape_string($_POST['passpost']);
    $firstname = $conn->real_escape_string($_POST['firstnamepost']);
    $lastname = $conn->real_escape_string($_POST['lastnamepost']);
    $phonenumber = $conn->real_escape_string($_POST['phonenumberpost']);
    $education = $conn->real_escape_string($_POST['institutionpost']);
    $facebook = $conn->real_escape_string($_POST['facebookpost']);
    $twitter = $conn->real_escape_string($_POST['twitterpost']);
    $instagram = $conn->real_escape_string($_POST['instagrampost']);

    $filename = $_FILES['uploadprofileimg']['name'];
    $filename = $ran.$filename;
    $filetmp = $_FILES['uploadprofileimg']['tmp_name'];
    $filetype = $_FILES['uploadprofileimg']['type'];
    move_uploaded_file($filetmp, "../userimages/".$filename);

    if (strlen($email) && strlen($password) && strlen($firstname) && strlen($lastname) && strlen($phonenumber) && strlen($education) && strlen($facebook) && strlen($twitter) && strlen($instagram)) {
        //check for a valid email
        if(preg_match("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$^",$email))
            $errors['email'] = 'invalid email address';

        //check for presence of @ in email
        if (!stristr($em,"@") OR !stristr($em,".") $errors['email'] = 'please enter an email';

        //echeck if email already exists in database
        $checkemail = $conn->get_row("SELECT * FROM elmtree_users WHERE email=".$email);
        if( $conn->num_rows( $checkemail ) > 0 ) $errors['email'] = "User already exists with the email: " . $email;

        //validate password
        $minpasslen = 8;
        if (strlen($password) < $minpasslen)
            $errors['email'] = 'password is too short';
        $finalpassword = MD5($password);
        if (empty($errors)) {
            $insertuser = "INSERT INTO elmtree_users (user, email, pw, firstName, lastName, profileimg, learninginstitute, phone, facebook, twitter, instagram) VALUES
            ('$username', '$email', '$finalpassword', '$firstname', '$lastname', '$filename', '$education', '$phonenumber', '$facebook', '$twitter', '$instagram')";

            $resultinsert = $conn -> query($insertuser);
            if(!$resultinsert){
                echo $conn->error;
            } else {
                echo "<h2> Account successfully registered!</h2> 
                        <h4>Please <a href='login.php'> <font class='text-success'><strong>login.</strong></font></a></h4><br><br><br><br>";

        } else {
            echo implode('<br>', $errors);
        }
    }
}