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

时间:2019-03-26 02:40:16

标签: php html mysql

我希望能够运行php验证脚本来阻止用户输入乱码作为其电子邮件地址。我知道我们可以将表单输入类型设置为电子邮件,但是可以轻松地在开发人员工具中忽略它,并且破坏数据库完整性。

我的插入页面如下:

$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>";

2 个答案:

答案 0 :(得分:0)

就像每个人都在指出

制作自己的日志系统非常棘手。它要求您执行其他步骤以确保内容的安全。不仅是黑客,您作为数据库管理员也无权访问PlainText中的客户密码。大多数用户将在您的网站上使用与在网站上注册的电子邮件密码相同的密码。 ..

建议创建类似laravel的登录工具,或者只是研究如何构建安全的登录系统,因为我们在您的代码中看到的是错误的,不是语法上的,而是从安全的角度来看。

>

我知道您存储了这样的密码,所以我不会在您的网站上注册。

不仅如此,但您确实应该看看mysqli binding 甚至,我更喜欢的是PDO_Mysql 您的代码不仅可读性更强,而且将值直接绑定到mysql中的一个字段(无需再使用real_escape_string了)

现在可以真正回答您的问题了。

您可能应该直接在表单字段上进行某种javascript实时验证。

然后在PHP方面,您可以使用REGXPpreg_match()

做一个简单的条件

看看https://regex101.com/r/SOgUIV/1,这是一个可验证EMAIL的正则表达式。 通过此链接,您应该对其进行一些试验,它不仅在侧面提供了文档,而且还提供了可能的量词等。

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.
}

已编辑----

正如一些用户在评论中指出的那样。 使用

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

如果您要确保用户有权访问电子邮件地址帐户,也可以在usersisConfirmedConfirmationCode

中添加两列

当用户注册时,您创建一个unique代码并将其放入ConfirmationCode,然后向用户发送一封电子邮件,其中包含“请单击以下链接以激活帐户www.yourWebSite。 com / confirmationPage.php?Code = $ TheActualCodeYouCreatedForThatUser” 然后,一旦用户转到该页面,请将字段isConfirmed更改为'1'或true。

一旦在您的网站上出现,您就可以假定只有带有isConfirmed的电子邮件才是真实用户。

答案 1 :(得分:-1)

要验证电子邮件,您需要检查很多类似的东西

  1. 如果电子邮件已经存在
  2. 如果它是真实的电子邮件,即检查@
  3. 检查不应该包含在电子邮件中的有趣字符。

然后始终加密您的密码

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);
        }
    }
}
相关问题