This is my first post here so don't hate if I do something in the wrong way.
I want to make a captcha for my site with logical questions. I think it is better than reCaptcha. Anyway, I made a fancy code with lots of "Googleing" but it must ahve some mistakes and I also would like to know, how to validate the answer from the database. My database looks like this:
id question answer
1 example? exampleanswer
My current code is this:
<?php
$database_db="general";
$user_db="root";
$password_db="somepass";
$host_db="localhost";
$link=mysql_connect($host_db,$user_db,$password_db) or die ("couldnot connect: ".mysql_error());
mysql_select_db($database_db, $link) or exit('Error Selecting database: '.mysql_error()); ;
$query = "SELECT * FROM `captcha` ORDERED BY RAND() LIMIT 1";
$question=mysql_query($query);
$answer=$_POST["answer"];
$errormessage = "";
$sql="SELECT * FROM captcha where question='$question' and answer='$answer'";
$result = mysql_query($sql, $link) or exit('$sql failed: '.mysql_error());
$num_rows = mysql_num_rows($result);
if($num_rows==0){
header("Location: error.php");
} else {
header("Location: success.php");
exit;
}
?>
<html>
<body>
<form method="post">
<?php echo $question; ?>
<input type="text" name="answer" id="answer" />
<input type="submit" name="submit" value="submit" />
</form>
</body>
</html>
Success.php is a simple file where the user goes if he did everything well, and error.php is a file which contains the following code:
<?php echo "Captcha is not valid! Please try again"; ?>
Also please note what would you change to optimize this code. If it is ready, I also have an other question: How to implement it in any other, external website.
Thank you for your help, MLL