In this tutorial of php code for beginners we will see how to create your own capcha for your feedback, registration or any other form.
Step 1:- Create captcha.php file and add the below code in it.
Note: Create a fonts folder in your root directory and add "arial.ttf" file. You can find arial.ttf in you system's font folder.
<?php
session_start();
$string = '';
for ($i = 0; $i < 5; $i++) {
$string .= chr(rand(97, 122));
}
$_SESSION['captcha'] = $string;
$font_path = 'fonts/';
$captcha_image = imagecreatetruecolor(150, 60);
$text_color = imagecolorallocate($captcha_image, 200, 100, 90); // red
$bg_color = imagecolorallocate($captcha_image, 255, 255, 255);
imagefilledrectangle($captcha_image,0,0,399,99,$bg_color);
imagettftext ($captcha_image, 30, 0, 10, 40, $text_color, $font_path."arial.ttf", $_SESSION['captcha']);
header("Content-type: image/png");
imagepng($captcha_image);
?>
session_start();
$string = '';
for ($i = 0; $i < 5; $i++) {
$string .= chr(rand(97, 122));
}
$_SESSION['captcha'] = $string;
$font_path = 'fonts/';
$captcha_image = imagecreatetruecolor(150, 60);
$text_color = imagecolorallocate($captcha_image, 200, 100, 90); // red
$bg_color = imagecolorallocate($captcha_image, 255, 255, 255);
imagefilledrectangle($captcha_image,0,0,399,99,$bg_color);
imagettftext ($captcha_image, 30, 0, 10, 40, $text_color, $font_path."arial.ttf", $_SESSION['captcha']);
header("Content-type: image/png");
imagepng($captcha_image);
?>
Step 2:- Now create feedback.php and past the following code in it.
<?php
session_start();
if(isset($_POST['submit'])) {
if(!empty($_POST['name']) && !empty($_POST['email']) && !empty($_POST['message']) && !empty($_POST['code'])) {
if($_POST['code'] == $_SESSION['captcha']) {
// send email to admin
$accept = "Thank you for your feedback.";
} else {
$error = "Please check that you have entered proper security code.";
}
} else {
$error = "Please enter all details.";
}
}
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Feedback Form</title>
<style>
.error {
color:red;
}
.accept {
color:green;
}
</style>
</head>
<body>
<?php if(!empty($error)) echo '<div class="error">'.$error.'</div>'; ?>
<?php if(!empty($accept)) echo '<div class="accept">'.$accept.'</div>'; ?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<table>
<tr>
<td colspan="2" align='center'><b>Feedback Form</b></td>
</tr>
<tr>
<td>Enter Your Name:</td>
<td><input type="text" name="name" /></td>
</tr>
<tr>
<td>Enter Your Email ID:</td>
<td><input type="text" name="email" /></td>
</tr>
<tr>
<td>Feedback:</td>
<td><textarea name="message"></textarea></td>
</tr>
<tr>
<td></td>
<td><img src="captcha.php"/></td>
</tr>
<tr>
<td>Enter Security Code:</td>
<td><input type="text" name="code" /></td>
</tr>
<tr>
<td><input type="submit" name="submit" value="Submit" class="button" /></td>
<td></td>
</tr>
</table>
</form>
</body>
</html>
session_start();
if(isset($_POST['submit'])) {
if(!empty($_POST['name']) && !empty($_POST['email']) && !empty($_POST['message']) && !empty($_POST['code'])) {
if($_POST['code'] == $_SESSION['captcha']) {
// send email to admin
$accept = "Thank you for your feedback.";
} else {
$error = "Please check that you have entered proper security code.";
}
} else {
$error = "Please enter all details.";
}
}
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Feedback Form</title>
<style>
.error {
color:red;
}
.accept {
color:green;
}
</style>
</head>
<body>
<?php if(!empty($error)) echo '<div class="error">'.$error.'</div>'; ?>
<?php if(!empty($accept)) echo '<div class="accept">'.$accept.'</div>'; ?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<table>
<tr>
<td colspan="2" align='center'><b>Feedback Form</b></td>
</tr>
<tr>
<td>Enter Your Name:</td>
<td><input type="text" name="name" /></td>
</tr>
<tr>
<td>Enter Your Email ID:</td>
<td><input type="text" name="email" /></td>
</tr>
<tr>
<td>Feedback:</td>
<td><textarea name="message"></textarea></td>
</tr>
<tr>
<td></td>
<td><img src="captcha.php"/></td>
</tr>
<tr>
<td>Enter Security Code:</td>
<td><input type="text" name="code" /></td>
</tr>
<tr>
<td><input type="submit" name="submit" value="Submit" class="button" /></td>
<td></td>
</tr>
</table>
</form>
</body>
</html>
Now you feedback form with your own captcha is ready to use.
Hope this php tutorial is useful for you. Keep following www.phpcodeforbeginner.blogspot.in for more help.
No comments:
Post a Comment