如何过滤在html上输入表单的电子邮件?

时间:2017-03-02 18:34:33

标签: php html mysql sql mysqli

这是我用于存储电子邮件的html和php。我该怎么做才能确保一个人实际输入电子邮件而不是随机字符。也许是代码,看看它是否有。"扩展名"或者" @"符号。另外,我如何防止重复的电子邮件条目。另外我想提醒网站访问者在提交时取得成功,我该如何实现呢?

任何帮助将不胜感激!谢谢!

HTML:

<div class="row">
    <div class="col-md-12">
        <div class="call-to-action text-center">
            <form class="form-inline margin-clear">
                <div class="form-group has-feedback">
                    <label class="sr-only" for="subscribe3">Email address</label>
                    <input type="email" class="form-control form-control-lg" id="subscribe3" placeholder="Enter email" name="subscribe3" required="">
                    <i class="fa fa-envelope form-control-feedback"></i>
                </div>
                <button type="submit" class="btn btn-lg btn-gray-transparent btn-animated margin-clear">Subscribe <i class="fa fa-send"></i></button>
            </form>
        </div>
    </div>
</div>

PHP:

<?php
//checking if data has been entered
if( isset( $_POST['subscribe3'] ) && !empty( $_POST['subscribe3'] ) )
{
    $data = $_POST['subscribe3'];
} else {
    header( 'location: index.html' );
    exit();
}

//setting up mysql details
$sql_server = 'localhost';
$sql_user = 'root';
$sql_pwd = '*PASSWORD*';
$sql_db = 'newsletter';

// USE A PREPARED STATEMENT LIKE SO FOR SAFETY
$mysqli = new mysqli( $sql_server, $sql_user, $sql_pwd, $sql_db ) or die( $mysqli->error );

 /* Make sure its an email */
if(filter_var($email, FILTER_VALIDATE_EMAIL)){
        $query = "INSERT IGNORE INTO email  (emails)
VALUES (?)";
$stmt = $conn->prepare($query);
if($stmt){
  $stmt->bind_param("s=",$data);
  $stmt->execute();
  $stmt->close();
  $successmessage = "successfully inserted email";
}

    //closing mysqli connection
    $mysqli->close();
  if(isset($_POST['submit'])){
     echo $successmessage;
  }

?>

1 个答案:

答案 0 :(得分:0)

你可以做多件事:

  1. pregmatch ...只是一个快速正则表达式的例子,使用像rubular.com这样的东西来测试它

    if(pregmatch(&#39; / ^ EXPRESSION $ /&#39;){

    echo&#34;有效&#34 ;;

    {

  2. 或使用内置的电子邮件过滤器for php

  3. http://php.net/manual/en/filter.filters.validate.php

    <?php
        //checking if data has been entered
        if( isset( $_POST['subscribe3'] ) && !empty( $_POST['subscribe3'] ) )
        {
            $data = $_POST['subscribe3'];
        } else {
            header( 'location: index.html' );
            exit();
        }
    
        //setting up mysql details
        $sql_server = 'localhost';
        $sql_user = 'root';
        $sql_pwd = '*password*';
        $sql_db = 'newsletter';
    
        // USE A PREPARED STATEMENT LIKE SO FOR SAFETY
        $mysqli = new mysqli( $sql_server, $sql_user, $sql_pwd, $sql_db ) or die( $mysqli->error );
    
         /* Make sure its an email */
        if(filter_var($email, FILTER_VALIDATE_EMAIL)){
                $query = "INSERT IGNORE INTO email  (emails)
        VALUES (?)";
        $stmt = $conn->prepare($query);
        if($stmt){
          $stmt->bind_param("s=",$data);
          $stmt->execute();
          $stmt->close();
          $successmessage = "successfully inserted email";
        }
    }
        //closing mysqli connection
        $mysqli->close();
      if(isset($_POST['submit'])){
         echo $successmessage;
      }
    
    ?>
    

    就防止重复而言,INSERT IGNORE应该停止,只要你有一个好的PK集。否则,您可以运行SELECT快速查询以检查是否存在任何行,然后围绕准备好的插入执行if测试,例如if($ numRows == 0)然后插入。我发送的内容也会显示成功的提交消息