Implementing CAPTCHA into the index.php

Helpdesk for my helpdesk software

Moderator: mkoch227

Post Reply
johnny-legend
Posts: 23
Joined: Tue Jan 03, 2006 7:53 pm

Implementing CAPTCHA into the index.php

Post by johnny-legend »

Before the new version of Hesk gets out I wanted to use the CAPTCHA anyway.

I already got the CAPTCHA image and input field itself integrated but not the denial if the code was entered wrong.

Maybe somebody can take a look at that.

The index.php:

Code: Select all

<?php
# Helpdesk software Hesk
# Version: 0.93b from July 5, 2005
# File name: index.php
# File last modified: July 5, 2005
# Written 23rd April 2005 by Klemen Stirn (info@phpjunkyard.com)
# http://www.PHPJunkYard.com

##############################################################################
# COPYRIGHT NOTICE                                                           #
# Copyright 2005 PHPJunkYard All Rights Reserved.                            #
#                                                                            #
# The Hesk may be used and modified free of charge by anyone AS LONG AS      #
# THIS COPYRIGHT NOTICE AND ALL THE COMMENTS REMAIN INTACT. By using this    #
# code you agree to indemnify Klemen Stirn from any liability that might     #
# arise from it's use.                                                       #
#                                                                            #
# Selling the code for this program or any portion of it without prior       #
# written consent is expressly forbidden.                                    #
#                                                                            #
# Obtain permission before redistributing this software over the Internet or #
# in any other medium. In all cases copyright and header must remain intact. #
# This Copyright is in full effect in any country that has International     #
# Trade Agreements with the United States of America or with                 #
# the European Union.                                                        #
##############################################################################

#############################
#     DO NOT EDIT BELOW     #
#############################

define('IN_SCRIPT',1);

/* Get all the required files and functions */
require_once('hesk_settings.inc.php');
require_once('language/'.$hesk_settings['language'].'.inc.php');
require_once('inc/common.inc.php');

/* Print header */
require_once('inc/header.inc.php');

/* What should we do? */
$action=hesk_input($_REQUEST['a']) or $action='start';
if ($action == 'start') {print_start();}
elseif ($action == 'add') {print_add_ticket();}
else {hesk_error($hesklang['invalid_action']);}

/* Print footer */
require_once('inc/footer.inc.php');
exit();

/*** START FUNCTIONS ***/

function print_add_ticket() {
global $hesk_settings, $hesklang;
?>

<p class="smaller"><a href="<?php echo $hesk_settings['site_url']; ?>"
class="smaller"><?php echo $hesk_settings['site_title']; ?></a> >
<a href="index.php?a=start" class="smaller"><?php echo $hesk_settings['hesk_title']; ?></a>
> <?php echo $hesklang['submit_ticket']; ?><br>&nbsp;</p>
</td>
</tr>
<tr>
<td>

<h3><?php echo $hesklang['submit_ticket']; ?></h3>

<p><?php echo $hesklang['use_form_below']; ?>
<font class="important">*</font></p>

<form method="POST" action="submit_ticket.php" name="form1" enctype="multipart/form-data">

<table border="0" cellspacing="0" cellpadding="5" size="750">
<tr>
<td>

<!-- Contact info -->
<table border="0">
<tr>
<td align="right" width="150"><?php echo $hesklang['name']; ?>: <font class="important">*</font></td>
<td align="left" width="600"><input type="text" name="name" size="25" maxlength="30"></td>
</tr>
<tr>
<td align="right" width="150"><?php echo $hesklang['email']; ?>: <font class="important">*</font></td>
<td align="left" width="600"><input type="text" name="email" size="30" maxlength="50"></td>
</tr>
</table>

<hr>

<!-- Department and priority -->
<table border="0">
<tr>
<td align="right" width="150"><?php echo $hesklang['category']; ?>: <font class="important">*</font></td>
<td align="left" width="600"><select name="category">
<?php
require_once('inc/database.inc.php');

hesk_dbConnect() or hesk_error("Can't connect to database! Please notify webmaster at $hesk_settings[webmaster_mail]!");
$sql = "SELECT * FROM `hesk_categories`";
$result = hesk_dbQuery($sql) or hesk_error("Couldn't execute SQL: $sql<br>Please contact webmaster at $hesk_settings[webmaster_mail] and give him this error message");
while ($row=hesk_dbFetchAssoc($result))
{
	echo "
    <option value=\"$row[id]\">$row[name]</option>
    ";
}

?>
</select></td>
</tr>
<tr>
<td align="right" width="150"><?php echo $hesklang['priority']; ?>: <font class="important">*</font></td>
<td align="left" width="600"><select name="priority">
<option value="3" selected><?php echo $hesklang['low']; ?></option>
<option value="2"><?php echo $hesklang['medium']; ?></option>
<option value="1"><?php echo $hesklang['high']; ?></option>
</select></td>
</tr>
</table>

<hr>

<!-- ticket info -->


<table border="0">
<tr>
<td align="right" width="150"><?php echo $hesklang['subject']; ?>: <font class="important">*</font></td>
<td align="left" width="600"><input type="text" name="subject" size="40" maxlength="40"></td>
</tr>
<tr>
<td align="right" valign="top" width="150"><?php echo $hesklang['message']; ?>: <font class="important">*</font></td>
<td align="left" width="600"><textarea name="message" rows="12" cols="60"></textarea></td>
</tr>
</table>

<hr>
<p align="center">
<img src="captcha/captcha.php" border="0" title="CAPTCHA"><br>
<input type="text" name="CAPTCHA" size="4"><!-- Submit --></p>
<div align="center">
<center>
<table border="0">
<tr>
<td>
<p>&nbsp;<br>
<b><?php echo $hesklang['before_submit']; ?></b>
<li><?php echo $hesklang['all_info_in']; ?>.</li>
<li><?php echo $hesklang['all_error_free']; ?>.</li>
</p>

<p><b><?php echo $hesklang['we_have']; ?>:</b>
<li><?php echo $_SERVER['REMOTE_ADDR'].' '.$hesklang['recorded_ip']; ?></li>
<li><?php echo $hesklang['recorded_time']; ?></li>
</p>

<p align="center"><input type="submit" value="<?php echo $hesklang['sub_ticket']; ?>" class="button"></p>
</td>
</tr>
</table>
</center>
</div>

</td>
</tr>
</table>
</center>
</div>

<?php
} // End print_add_ticket()


function print_start() {
global $hesk_settings, $hesklang;
?>

<p class="smaller"><a href="<?php echo $hesk_settings['site_url']; ?>"
class="smaller"><?php echo $hesk_settings['site_title']; ?></a> >
<?php echo $hesk_settings['hesk_title']; ?><br>&nbsp;</p>
</td>
</tr>
<tr>
<td>

<h3 align="center"><?php echo $hesk_settings['hesk_title']; ?></h3>

<p><b><?php echo $hesklang['open_ticket']; ?></b></p>

<p><a href="index.php?a=add"><?php echo $hesklang['sub_support']; ?></a> </p>

<hr>

<p><b><?php echo $hesklang['view_existing']; ?></b></p>

<p><form action="ticket.php" method="GET">
<?php echo $hesklang['trackID']; ?>: <input type="text" name="track">
<input type="hidden" name="Refresh" value="<?php echo rand(10000,99999); ?>">
<input type="submit" value="<?php echo $hesklang['view_ticket']; ?>" class="button">
</form></p>

<hr>

<p>&nbsp;</p>

<?php
} // End print_start()

?>
The captcha.php:

Code: Select all

<?php 
   session_start(); 
   unset($_SESSION['captcha_spam']); 
   $text = rand(1000,10000); //Zufallszahl 

   function encrypt($string, $key) { 
   $result = ''; 
   for($i=0; $i<strlen($string); $i++) { 
      $char = substr($string, $i, 1); 
      $keychar = substr($key, ($i % strlen($key))-1, 1); 
      $char = chr(ord($char)+ord($keychar)); 
      $result.=$char; 
   } 
   return base64_encode($result); 
   } 

   $_SESSION['captcha_spam'] = encrypt($text, "D2H9m4180IrEsbBN8"); //Key 
   $_SESSION['captcha_spam'] = str_replace("=", "", $_SESSION['captcha_spam']); 

         
   header('Content-type: image/png'); 
   $img = ImageCreateFromPNG('captcha.PNG'); //Backgroundimage 
   $color = ImageColorAllocate($img, 0, 0, 0); //Farbe 
   $ttf = $_SERVER['Document-Root']."GENUIB.TTF"; //Schriftart 
   $ttfsize = 25; //Schriftgrösse 
   $angle = rand(0,5); 
   $t_x = rand(5,50); 
   $t_y = 35; 
   imagettftext($img, $ttfsize, $angle, $t_x, $t_y, $color, $ttf, $text); 
   imagepng($img); 
   imagedestroy($img); 
?> 
The page I got this script from also has this code for the script to put somewhere, but I can't deal with this to put into the right place:

Code: Select all

<?php 
session_start(); 
function encrypt($string, $key) { 
$result = ''; 
for($i=0; $i<strlen($string); $i++) { 
   $char = substr($string, $i, 1); 
   $keychar = substr($key, ($i % strlen($key))-1, 1); 
   $char = chr(ord($char)+ord($keychar)); 
   $result.=$char; 
} 
return base64_encode($result); 
} 
$sicherheits_eingabe = encrypt($_POST["sicherheitscode"], "8h384ls94"); 
$sicherheits_eingabe = str_replace("=", "", $sicherheits_eingabe); 
if($sicherheits_eingabe == $_SESSION['captcha_spam']){ 
unset($_SESSION['captcha_spam']); 

// 
// 
//Original Script
// 
// 

} 
?> 
Klemen
Site Admin
Posts: 10139
Joined: Fri Feb 11, 2005 4:04 pm

Post by Klemen »

Klemen, creator of HESK and PHPJunkyardWas this helpful? You can buy me a drink here Image

Image You should follow me on Twitter here

Help desk software | Cloud help desk | Guestbook | Link manager | Click counter | more PHP Scripts ...

Also browse for php hosting companies, read php books, find php resources and use webmaster tools
Post Reply