captcha i min kontaktform
Hej er der nogen der gider og hjælpe mig med og vise mig hvordan jeg tilføjer captcha til min kontakt kontakt, har prøvet selv men den ryger stadig igennem uden man har skrevet koden.Min kode ser sådan her ud
<?php
session_start();
require 'lib/Database.php';
require 'inc/Top.inc';
$self = $_SERVER['PHP_SELF'];
if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
$dbo = new \Application\Model\Database();
// prepare form data
$_POST = array_map('addslashes', $_POST);
$_POST = array_map('trim', $_POST);
extract($_POST);
// TODO: Validate data (make sure form is completely & correctly filled)
$form_valid = (
isset($firstname) &&
isset($lastname) &&
isset($email) &&
isset($phone) &&
isset($mail_message)
);
$errors = [];
if ($form_valid && count($errors) == 0) {
$ip = getenv('REMOTE_ADDR');
$seed = time();
$enc1 = md5($_FILES['file1']['tmp_name']) . '.dat';
$enc2 = md5($_FILES['file2']['tmp_name']) . '.dat';
$enc3 = md5($_FILES['file3']['tmp_name']) . '.dat';
$enc4 = md5($_FILES['file4']['tmp_name']) . '.dat';
$enc5 = md5($_FILES['file5']['tmp_name']) . '.dat';
$q = $dbo->Query("insert into Messages (
FirstName,
LastName,
Email,
Phone,
Message,
EncSeed,
EncFile1,
EncFile2,
EncFile3,
EncFile4,
EncFile5,
IPAddress,
SentDate)
values (
'$firstname',
'$lastname',
'$email',
'$phone',
'$mail_message',
'$seed',
'$enc1',
'$enc2',
'$enc3',
'$enc4',
'$enc5',
'$ip',
NOW()
);");
if ($q !== false) {
$mail_message = htmlentities($mail_message);
$mail_message = nl2br($mail_message);
$html = '<h2>Besked fra Mitsite</h2><hr>';
$html .= "<p><b>Navn:</b> $firstname $lastname<br><b>Telefon:</b> $phone<br><b>E-mail:</b> $email</p>";
$html .= '<hr>' . $mail_message;
if (isset($_FILES) && count($_FILES) > 0) {
$html .= '<hr><h3>Billeder</h3>';
}
if (isset($_FILES['file1']) && $_FILES['file1']['name'] != null) {
move_uploaded_file($_FILES['file1']['tmp_name'], "admin/message_files/$enc1");
$html .= "<img src='https://mitsite.dk/admin/message_files/$enc1'><br>";
}
if (isset($_FILES['file2']) && $_FILES['file2']['name'] != null) {
move_uploaded_file($_FILES['file2']['tmp_name'], "admin/message_files/$enc2");
$html .= "<img src='https://mitsite.dk/admin/message_files/$enc2'><br>";
}
if (isset($_FILES['file3']) && $_FILES['file3']['name'] != null) {
move_uploaded_file($_FILES['file3']['tmp_name'], "admin/message_files/$enc3");
$html .= "<img src='https://mitsite.dk/admin/message_files/$enc3'><br>";
}
if (isset($_FILES['file4']) && $_FILES['file4']['name'] != null) {
move_uploaded_file($_FILES['file4']['tmp_name'], "admin/message_files/$enc4");
$html .= "<img src='https://mitsite.dk/admin/message_files/$enc4'><br>";
}
if (isset($_FILES['file5']) && $_FILES['file5']['name'] != null) {
move_uploaded_file($_FILES['file5']['tmp_name'], "admin/message_files/$enc5");
$html .= "<img src='https://mitsite.dk/admin/message_files/$enc5'><br>";
}
echo '<h2>Kontakt</h2><hr><p>Vi har modtaget din besked, og vil besvare den hurtigst muligt.</p>';
// Send html mail with text and uploaded images
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1" . "\r\n";
$headers .= "From: contact@mitsite.dk" . "\r\n" .
"Reply-To: contact@mitsiteom.dk" . "\r\n" .
"X-Mailer: PHP/" . phpversion();
mail('minmail@hotmail.com', 'Besked fra mitsite', $html, $headers);
}
require 'inc/Bottom.inc';
die;
}
}
// general form check
$err_msg = (isset($form_valid) && !$form_valid) ? '<p class="error">Alle felter skal udfyldes.</p><hr>' : '';
// individual check done only if general check yielded nothing
$err_list_html = '';
if (count($errors) > 0) {
$err_list_html = "\n<ul>\n";
foreach ($errors as $e) {
$err_list_html .= "<li class=\"error\">$e</li>\n";
}
$err_list_html .= "</ul>\n\n";
$err_msg = $err_list_html;
}
echo <<< HTML
<h2>Kontakt</h2>
<hr>
$err_msg
<form action="./kontakt" method="post" enctype="multipart/form-data">
<div class="row">
<div class="col-md-6">
<label for="firstname">Fornavn</label><br>
<input type="text" value="$firstname" name="firstname" id="firstname" class="checkout form-control" required>
</div>
<div class="col-md-6">
<label for="lastname">Efternavn</label><br>
<input type="text" value="$lastname" name="lastname" id="lastname" class="checkout form-control" required>
</div>
</div>
<div class="row">
<div class="col-md-6">
<label for="email">E-mail</label><br>
<input type="email" value="$email" name="email" id="email" class="checkout form-control" required>
</div>
<div class="col-md-6">
<label for="phone">Telefon</label><br>
<input type="tel" value="$phone" name="phone" id="phone" class="checkout form-control" required>
</div>
</div>
<div class="row">
<div class="col-md-12">
<label for="message">Meddelelse</label><br>
<textarea id="message" name="mail_message" class="checkout form-control" required style="height:200px;">$mail_message</textarea>
</div>
</div>
<div class="row text-center"><button type="submit" class="btn btn-primary">Send</button></div>
<div class="row h3">
<div class="col-md-12">Billeder, der skal vedhæftes beskeden (valgfrit)</div>
</div>
<div class="row">
<div class="col-md-12">
<input type="file" name="file1"><br>
<input type="file" name="file2"><br>
<input type="file" name="file3"><br>
<input type="file" name="file4"><br>
<input type="file" name="file5">
</div>
</div>
</form>
HTML;
require 'inc/Bottom.inc';
så har jeg det her stykke kode fra det captcha script jeg gerne vil bruge
if(isset($_POST["captcha"]))
if($_SESSION["captcha"]==$_POST["captcha"])
{
//CAPTHCA is valid; proceed the message: save to database, send by e-mail …
echo '<div class="alert alert-success">CAPTHCA is valid; proceed the message</div>';
}
else
{
echo '<div class="alert alert-danger">CAPTHCA is not valid; ignore submission</div>';
}
?>
og det her som skal bruges i formen øverst
<div class="col-sm-5 pull-left"><label for="pwd">Anti Spam code, Please Enter 3 Black Symbols</label>
<img src="captcha.php" alt="captcha image"></div>
<div class="col-sm-7 pull-right"><input type="text" name="captcha" size="3″ maxlength="3″ class="form-control"></div>
</div>
ved ikke om det er nødvendigt men selve captcha script koden
<?php
session_start();
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
function _generateRandom($length=6)
{
$_rand_src = array(
array(48,57) //digits
, array(97,122) //lowercase chars
// , array(65,90) //uppercase chars
);
srand ((double) microtime() * 1000000);
$random_string = "";
for($i=0;$i<$length;$i++){
$i1=rand(0,sizeof($_rand_src)-1);
$random_string .= chr(rand($_rand_src[$i1][0],$_rand_src[$i1][1]));
}
return $random_string;
}
$im = @imagecreatefromjpeg("captcha.jpg");
$rand = _generateRandom(3);
$_SESSION['captcha'] = $rand;
ImageString($im, 5, 2, 2, $rand[0]." ".$rand[1]." ".$rand[2]." ", ImageColorAllocate ($im, 0, 0, 0));
$rand = _generateRandom(3);
ImageString($im, 5, 2, 2, " ".$rand[0]." ".$rand[1]." ".$rand[2], ImageColorAllocate ($im, 255, 0, 0));
Header ('Content-type: image/jpeg');
imagejpeg($im,NULL,100);
ImageDestroy($im);
?>
Den der skal lykkedes det er at den ikke skal kunne sende hvis Captcha ikke er korrekt indtastet. På forhånd tak