본문 바로가기
웹 개발 이야기/php

[PHP] 스팸글 등록 방지를 위한 아주 간단한 방법

by Gommin 2023. 3. 13.

// Simple honeypot for an HTML form using PHP

<?php

//check if form was sent
if($_POST){

	$to = 'some@email.com';
	$subject = 'Testing HoneyPot';
	$header = "From: $name <$name>";

	$name = $_POST['name'];
	$email = $_POST['email'];
	$message = $_POST['message'];

	//honey pot field
	$honeypot = $_POST['firstname'];

	//check if the honeypot field is filled out. If not, send a mail.
	if( ! empty( $honeypot ) ){
		return; //you may add code here to echo an error etc.
	}else{
		mail( $to, $subject, $message, $header );
	}
}

?>
<html>
	<head>
		<title>HoneyPot for HTML Form Example</title>
		<style>
		.hide-robot{
			display:none;
		}
		</style>
	</head>

	<body>

		<form method="post" action="#my-form" id="my-form">
			<!-- Create fields for the honeypot -->
			<input name="firstname" type="text" id="firstname" class="hide-robot">
			<!-- honeypot fields end -->
			
			<input name="name" type="text" id="name" placeholder="Name" required><br>
			<input name="email" type="email" id="email" placeholder="Email" required><br>
			<textarea name="message" id="message" placeholder="Enter your message here" required></textarea><br>
			<input type="submit">
		</form>

	</body>
</html>

 

 

출처 : https://gist.github.com/andrewlimaza/958826feac907114a57462bfc8d535ff

 

 

Simple honeypot for an HTML form using PHP

Simple honeypot for an HTML form using PHP. GitHub Gist: instantly share code, notes, and snippets.

gist.github.com

 

댓글