gpt4 book ai didi

php - 如何过滤输入到 html 表单中的电子邮件?

转载 作者:行者123 更新时间:2023-11-29 19:18:35 24 4
gpt4 key购买 nike

这是我用来存储电子邮件的 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. pregmatch...只是正则表达式快速的一个示例,使用 rubular.com 之类的东西来测试它

    if(pregmatch('/^表达式$/'){

    回显“有效”;

    {

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

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

?>

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

关于php - 如何过滤输入到 html 表单中的电子邮件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42563254/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com