Welcome to Dream.In.Code
Become an Expert!

Join 150,175 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 2,210 people online right now. Registration is fast and FREE... Join Now!




Contact Form Help request

 
Reply to this topicStart new topic

Contact Form Help request, Contact Form

utdfederation
31 Aug, 2008 - 08:44 AM
Post #1

New D.I.C Head
*

Joined: 31 Aug, 2008
Posts: 2

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
CODE
<?php
include('corefuncs.php');
if (function_exists('nukeMagicQuotes')) {
  nukeMagicQuotes();
  }

// process the email
if (array_key_exists('send', $_POST)) {
  $to = 'YOU@YOUREMAIL.com'; // 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 = '/^[^@]+@[^\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: <YOU@YOUREMAIL.com>';
    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>


User is offlineProfile CardPM
+Quote Post

Martyr2
RE: Contact Form Help Request
31 Aug, 2008 - 08:50 AM
Post #2

Programming Theoretician
Group Icon

Joined: 18 Apr, 2007
Posts: 5,660



Thanked: 313 times
Expert In: C/C++, Java, VB, VB.NET, C#, PHP, Web Development, HTML & CSS, Javascript

My Contributions
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. smile.gif
User is offlineProfile CardPM
+Quote Post

utdfederation
RE: Contact Form Help Request
31 Aug, 2008 - 09:09 AM
Post #3

New D.I.C Head
*

Joined: 31 Aug, 2008
Posts: 2

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.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 03:32AM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month