2 Replies - 1883 Views - Last Post: 31 August 2008 - 10:09 AM

#1 utdfederation   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 31-August 08

Contact Form Help request

Posted 31 August 2008 - 09:44 AM

hello all,

request some help here please with this contact form i found off the net.

was wondering if someone could please help me add a captcha or some form of security that the user has to input when filling out the form.would be very greatfull for someones help on this.

contact.php
<?php
include('corefuncs.php');
if (function_exists('nukeMagicQuotes')) {
  nukeMagicQuotes();
  }

// process the email
if (array_key_exists('send', $_POST)) {
  $to = '[email protected]'; // use your own email address
  $heading = '**You Have Mail**';
  
  // list expected fields
  $expected = array('name', 'email', 'subject', 'message', 'securitycode');
  // set required fields
  $required = array('name', 'email', 'subject', 'message', 'securitycode');
  // create empty array for any missing fields
  $missing = array();
  
  // assume that there is nothing suspect
  $suspect = false;
  // create a pattern to locate suspect phrases
  $pattern = '/Content-Type:|Bcc:|Cc:/i';
  
  // function to check for suspect phrases
  function isSuspect($val, $pattern, &$suspect) {
	// if the variable is an array, loop through each element
	// and pass it recursively back to the same function
	if (is_array($val)) {
	  foreach ($val as $item) {
		isSuspect($item, $pattern, $suspect);
		}
	  }
	else {
	  // if one of the suspect phrases is found, set Boolean to true
	  if (preg_match($pattern, $val)) {
		$suspect = true;
		}
	  }
	}
  
  // check the $_POST array and any sub-arrays for suspect content
  isSuspect($_POST, $pattern, $suspect);
  
  if ($suspect) {
	$mailSent = false;
	unset($missing);
	}
  else {
	// process the $_POST variables
	foreach ($_POST as $key => $value) {
	  // assign to temporary variable and strip whitespace if not an array
	  $temp = is_array($value) ? $value : trim($value);
	  // if empty and required, add to $missing array
	  if (empty($temp) && in_array($key, $required)) {
		array_push($missing, $key);
		}
	  // otherwise, assign to a variable of the same name as $key
	  elseif (in_array($key, $expected)) {
		${$key} = $temp;
		}
	  }
	}
  
  // validate the email address
  if (!empty($email)) {
	// regex to ensure no illegal characters in email address 
	$checkEmail = '/^[^@][email protected][^\s\r\n\'";,@%]+$/';
	// reject the email address if it doesn't match
	if (!preg_match($checkEmail, $email)) {
	  array_push($missing, 'email');
	  }
	}
  
  // go ahead only if not suspect and all required fields OK
  if (!$suspect && empty($missing)) {
	// build the message
	$comments = "Name: $name\n\n";
	$comments .= "Email: $email\n\n";
	$comments .= "Message: $message";

	// limit line length to 70 characters
	$message = wordwrap($message, 70);
	
	// create additional headers
	$additionalHeaders = 'From: <[email protected]>';
	if (!empty($email)) {
	  $additionalHeaders .= "\r\nReply-To: $email";
	  }
	
	// send it  
	$mailSent = mail($to, $heading, $message, $additionalHeaders);
	if ($mailSent) {
	  // $missing is no longer needed if the email is sent, so unset it
	  unset($missing);
	  }
	}
  }
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Contact</title>
<link href="assets/formstyle.css" rel="stylesheet" type="text/css" media="screen" />
<style type="text/css">
<!--
.style1 {
	color: #00FF00;
	font-weight: bold;
	font-style: italic;
}
.style4 {color: #FF0000; font-style: italic; }
.style5 {color: #036}
.style6 {color: #FF0000}
-->
</style>
</head>

<body>
  
<div id="form-area">
	  <p>
			  <?php
		if ($_POST && isset($missing)) {
		?>
			<span class="warning"><em>Please complete the missing item(s) indicated.</em></span></p>
		<p>
			  <?php
		  }
		elseif ($_POST && !$mailSent) {
		?>
			<span class="warning"><em>Sorry, there was a problem sending your message. Please try later.</em></span></p>
		<p>
			  <?php
		  }
		elseif ($_POST && $mailSent) {
		?>
				<span class="style1">Thank You....Your Message Was Successfully Sent. </span></p>
		<p>
			  <?php } ?>
		</p>
		<form id="feedback" method="post" action="">
			  <p>
					<label for="name"><span class="style4">*</span><em> Your Name:</em>
					<?php
				if (isset($missing) && in_array('name', $missing)) { ?>
						<span class="warning"><em>Please Enter Your Name !! </em></span>
						<?php } ?>
					</label>
					<input name="name" id="name" type="text" class="formbox" 
				<?php if (isset($missing)) {
				  echo 'value="'.htmlentities($_POST['name']).'"';} ?>
				/>
			  </p>
			  <p>
					<label for="email"><span class="style4">*</span><em> Your E-mail :</em>
					<?php
				if (isset($missing) && in_array('email', $missing)) { ?>
						<span class="warning"><em>Please Enter Your E-mail !! </em></span>
						<?php } ?>
					</label>
					<input name="email" id="email" type="text" class="formbox" 
				<?php if (isset($missing)) {
				  echo 'value="'.htmlentities($_POST['email']).'"';} ?>
				/>
			  </p>
			  <p>
					<label for="subject"><span class="style4">*</span><em> Your Subject:</em>
					<?php
				if (isset($missing) && in_array('subject', $missing)) { ?>
						<span class="warning"><em>Please Enter Your Subject !! </em></span>
						<?php } ?>
					</label>
					<input name="subject" id="subject" type="text" class="formbox" 
				<?php if (isset($missing)) {
				  echo 'value="'.htmlentities($_POST['subject']).'"';} ?>
				/>
			  </p>
			  <p>
					<label for="message"><span class="style4">*</span><em> Your Message :</em>
					<?php
				if (isset($missing) && in_array('message', $missing)) { ?>
						<span class="warning"><em>Please Enter Your Message !! </em></span>
						<?php } ?>
					</label>
					<textarea name="message" id="message" cols="60" rows="8"><?php 
				if (isset($missing)) {
				  echo htmlentities($_POST['message']);
				  } ?>
					</textarea>
			  </p>
			  <p>security image goes here..</p>
			  </p>
			  <p>
					<label for="securitycode"><span class="style4">*</span><em> Enter Security Code :</em>
					<?php
				if (isset($missing) && in_array('securitycode', $missing)) { ?>
						<span class="warning"><em>Please Enter Security Code !! </em></span>
						<?php } ?>
					</label>
					<input name="securitycode" id="securitycode" type="text" class="formbox" 
				<?php if (isset($missing)) {
				  echo 'value="'.htmlentities($_POST['securitycode']).'"';} ?>
				/><br />
			  </p>
			  <p>
	  <input name="send" id="send" type="submit" value="Send Message" class="button" />
</p>
		</form>
</div>
</div>

</body>
</html>


Is This A Good Question/Topic? 0
  • +

Replies To: Contact Form Help request

#2 Martyr2   User is offline

  • Programming Theoretician
  • member icon

Reputation: 5612
  • View blog
  • Posts: 14,686
  • Joined: 18-April 07

Re: Contact Form Help request

Posted 31 August 2008 - 09:50 AM

Sorry but we do not do custom programming requests here. We help debug code/logic that you yourself has written or created.

Now I will move this post to the "Request a Service" forum and there perhaps someone will be willing to build a captcha system for the forum. In the meanwhile, do a search on the net for captcha code in PHP. One such link is below...

Building a CAPTCHA generator using PHP and GD - Rob's Blog

Good luck. :)
Was This Post Helpful? 0
  • +
  • -

#3 utdfederation   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 31-August 08

Re: Contact Form Help request

Posted 31 August 2008 - 10:09 AM

thanks for moving this i had no idea which section to place this in...

i was actually looking for someone to do this for me a i have no clue what i'm doing here.

it's just the captcha to do now so if anyone would be willing to help i would be very greatful.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1