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

标签 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>";

最佳答案

就像每个人都指出的那样

制作自己的日志系统很棘手。它要求您执行额外的步骤来保护内容。不仅对于黑客,而且作为数据库管理员的您不应该有权在 PlainText 中查看您的客户密码 大多数用户将在您的网站上使用与他们在注册时使用的电子邮件密码相同的密码在您的网站上..

更明智的做法是创建像 laravel 这样的登录工具,或者只是研究如何构建一个安全的登录系统,因为我们在您的代码中看到的是 BAD,不是语法上的,而是从安全的角度来看。

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

不仅如此,你真的应该看看 mysqli binding 甚至,我更喜欢的是 PDO_Mysql 您的代码不仅更易读,而且会将值直接绑定(bind)到 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
}

此外,如果您想确保用户可以访问电子邮件地址帐户,您还可以在 users 表中添加两列,isConfirmed,ConfirmationCode

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

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

关于php - 想要阻止用户在表单中输入无效的电子邮件地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55349091/

相关文章:

php - 更改数据库中ENUM的值

php - preg_match 在大文本上比 strpos 更快?

php - 使用 php 从快照获取 Azure 文件

php - 如何只为域(没有子域)设置 cookie?

javascript - ng-change 没有调用方法并且没有显示错误

html - 将 div 与第一个并排对齐,占据第二个 div 不需要的整个宽度

php - PHP:检查mysql中是否存在行

javascript - For 语句打破循环

mysql - 使用随机数据填充表时如何避免插入具有重复主键值的元组

MySQL 查询不起作用