Last active
October 22, 2021 07:49
-
-
Save loschke/e38888e55e5a15c6bdab3b94ee364ce3 to your computer and use it in GitHub Desktop.
Code Snippets zu Blogbeitrag https://sevenx.de/php-formular-spamschutz-und-validierung-spam-emails-verhindern-auch-ohne-captcha/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<form id="phpform" method="post" action="kontakt.php" autocomplete="off"> | |
<p><label for="name">Name</label> | |
<input type="text" name="name" value=""></p> | |
<p><label for="email">Email</label> | |
<input type="text" name="email" value=""></p> | |
<p><label for="message">Nachricht</label><br /> | |
<textarea name="message" rows="8"></textarea></p> | |
<p><input type="checkbox" name="human"> Ich versende keinen Spam</p> | |
<p><input type="submit" name="submit" value="Absenden"></p> | |
<div class="terms"> | |
Folgende Felder bitte unberührt lassen! | |
<input type="checkbox" name="terms"> | |
</div> | |
</form |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<input type="text" name="repeat_email" /> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script src="http://codeorigin.jquery.com/jquery-1.10.2.min.js"></script> | |
<script type="text/javascript"> | |
$(document).ready(function(){ | |
$('.terms').append('<input type="text" name="repeat_email" />'); | |
}); | |
</script> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<noscript>Bitte aktivieren Sie Javascript zum Absenden des Formulars oder | |
nutzen Sie eine der alternative Kontaktmöglichkeiten unter www.domain.de/kontakt.htm</noscript> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$errors = array(); | |
//Prüfen ob Formular abgesendet | |
if(isset($_POST['submit'])) { | |
//Spamcheck mit jedem neuem Absenden zurücksetzen | |
$spamcheck = false; | |
//Spamcheck | |
if(!isset($_POST["repeat_email"]) || !empty($_POST["repeat_email"]) || isset($_POST["terms"])) { | |
$errors[] = "Zusatzfelder wurden ausgefüllt, wir vermuten Spam und brechen hier ab."; | |
} else { | |
$spamcheck = true; | |
} | |
// Eingaben Validieren | |
if($spamcheck == true) { | |
if(empty($_POST['name'])) { //Wenn Name leer | |
$errors[] = "Bitte geben Sie Ihren Namen an"; | |
} | |
if(empty($_POST['email'])){ //Wenn Email leer | |
$errors[] = "Bitte Emailadresse angeben"; | |
} elseif (filter_var($_POST['email'], FILTER_VALIDATE_EMAIL) == false) { //Emailformat überprüfen ab PHP 5.2 | |
$errors[] = "Bitte geben Sie ein gültige Emailadresse an"; | |
} | |
if(empty($_POST['message'])){ //Wenn Nachricht leer | |
$errors[] = "Bitte geben Sie Ihre Nachricht ein"; | |
} | |
if(!isset($_POST["gender"])){ //Wenn Spamcheck nicht markiert | |
$errors[] = "Bitte bestätigen Sie den Spamcheck"; | |
} | |
} | |
if(isset($_POST['submit']) && empty($errors) && $spamcheck == true) { | |
// Spamtest bestanden, alle erforderlichen Felder richtig ausgefüllt | |
// Eintrag in Datenbank oder Email Versand | |
echo "Alles richtig gemacht"; | |
} | |
} | |
?> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php if(isset($_POST['submit']) && empty($errors) == false) {?> | |
<div style="background:#FCC"> | |
<strong>Bitte überprüfen Sie Ihre Angaben!</strong><br /> | |
<?php echo '<ul><li>'.implode('</li><li>',$errors).'</li></ul>'; ?> | |
</div> | |
<?php } ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment