treehouse : what would you like to learn today?
Web Design Web Development iOS Development

62-advanced-form-styling-functionality Issue

  • Greetings Everyone,

    After viewing Chris's tutorial on 62-advanced-form-styling-functionality I decided to chose it and used it in a clients site. I changed the form slightly with different options. Changed some of the code in the header to reflect these changes. I developed this site locally using Xampp so I couldn't test the forms. Now they are on a live site and the forms don't actually work. I don't know why. Would someone be willing to look over this code to see if something is off for me?

    <?php
    /*
    Template Name: Feedback
    */
    ?>
    <?php

    session_start();

    function getRealIp() {
    if (!empty($_SERVER['HTTP_CLIENT_IP'])) { //check ip from share internet
    $ip=$_SERVER['HTTP_CLIENT_IP'];
    } elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) { //to check ip is pass from proxy
    $ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
    } else {
    $ip=$_SERVER['REMOTE_ADDR'];
    }
    return $ip;
    }

    function writeLog($where) {

    $ip = getRealIp(); // Get the IP from superglobal
    $host = gethostbyaddr($ip); // Try to locate the host of the attack
    $date = date(\"d M Y\");

    // create a logging message with php heredoc syntax
    $logging = <<<LOG
    \n
    << Start of Message >>
    There was a hacking attempt on your form. \n
    Date of Attack: {$date}
    IP-Adress: {$ip} \n
    Host of Attacker: {$host}
    Point of Attack: {$where}
    << End of Message >>
    LOG;
    // Awkward but LOG must be flush left

    // open log file
    if($handle = fopen('hacklog.log', 'a')) {

    fputs($handle, $logging); // write the Data to file
    fclose($handle); // close the file

    } else { // if first method is not working, for example because of wrong file permissions, email the data

    $to = 'feedback@northstar-ems.us';
    $subject = 'HACK ATTEMPT';
    $header = 'From: feedback@northstar-ems.us';
    if (mail($to, $subject, $logging, $header)) {
    echo \"Sent notice to admin.\";
    }

    }
    }

    function verifyFormToken($form) {

    // check if a session is started and a token is transmitted, if not return an error
    if(!isset($_SESSION[$form.'_token'])) {
    return false;
    }

    // check if the form is sent with token in it
    if(!isset($_POST['token'])) {
    return false;
    }

    // compare the tokens against each other if they are still the same
    if ($_SESSION[$form.'_token'] !== $_POST['token']) {
    return false;
    }

    return true;
    }

    function generateFormToken($form) {

    // generate a token from an unique value, took from microtime, you can also use salt-values, other crypting methods...
    $token = md5(uniqid(microtime(), true));

    // Write the generated token to the session variable to check it against the hidden field when the form is sent
    $_SESSION[$form.'_token'] = $token;

    return $token;
    }

    // VERIFY LEGITIMACY OF TOKEN
    if (verifyFormToken('form1')) {

    // CHECK TO SEE IF THIS IS A MAIL POST
    if (isset($_POST['req-phone'])) {

    // Building a whitelist array with keys which will send through the form, no others would be accepted later on
    $whitelist = array('token','req-name','req-email','req-phone','req-position', 'curText', 'save-stuff');

    // Building an array with the $_POST-superglobal
    foreach ($_POST as $key=>$item) {

    // Check if the value $key (fieldname from $_POST) can be found in the whitelisting array, if not, die with a short message to the hacker
    if (!in_array($key, $whitelist)) {

    writeLog('Unknown form fields');
    die(\"Hack-Attempt detected. Please use only the fields in the form\");

    }
    }






    // Lets check the URL whether it's a real URL or not. if not, stop the script

    if(!filter_var($_POST['URL-main'],FILTER_VALIDATE_URL)) {
    writeLog('URL Validation');
    die('Hack-Attempt detected. Please insert a valid URL');
    }





    // SAVE INFO AS COOKIE, if user wants name and email saved

    $saveCheck = $_POST['save-stuff'];
    if ($saveCheck == 'on') {
    setcookie(\"WRCF-Name\", $_POST['req-name'], time()+60*60*24*365);
    setcookie(\"WRCF-Phone\", $_POST['req-phone'], time()+60*60*24*365);
    setcookie(\"WRCF-Email\", $_POST['req-email'], time()+60*60*24*365);

    }



    // PREPARE THE BODY OF THE MESSAGE

    $message = '<html><body>';
    $message .= '<table rules=\"all\" style=\"border-color: #666;\" cellpadding=\"10\">';
    $message .= \"<tr style='background: #eee;'><td><strong>Name:</strong> </td><td>\" . strip_tags($_POST['req-name']) . \"</td></tr>\";
    $message .= \"<tr><td><strong>Email:</strong> </td><td>\" . strip_tags($_POST['req-email']) . \"</td></tr>\";
    $message .= \"<tr><td><strong>Position:</strong> </td><td>\" . strip_tags($_POST['req-position']) . \"</td></tr>\";
    $message .= \"<tr><td><strong>Phone:</strong> </td><td>\" . strip_tags($_POST['req-phone']) . \"</td></tr>\";
    $addURLS = $_POST['addURLS'];
    if (($addURLS) != '') {
    $message .= \"<tr><td><strong>URL To Change (additional):</strong> </td><td>\" . strip_tags($addURLS) . \"</td></tr>\";
    }
    $curText = htmlentities($_POST['curText']);
    if (($curText) != '') {
    $message .= \"<tr><td><strong>CURRENT Content:</strong> </td><td>\" . $curText . \"</td></tr>\";
    }
    $message .= \"<tr><td><strong>NEW Content:</strong> </td><td>\" . htmlentities($_POST['newText']) . \"</td></tr>\";
    $message .= \"</table>\";
    $message .= \"</body></html>\";




    // MAKE SURE THE \"FROM\" EMAIL ADDRESS DOESN'T HAVE ANY NASTY STUFF IN IT

    $pattern = \"/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i\";
    if (preg_match($pattern, trim(strip_tags($_POST['req-email'])))) {
    $cleanedFrom = trim(strip_tags($_POST['req-email']));
    } else {
    return \"The email address you entered was invalid. Please try again!\";
    }




    // CHANGE THE BELOW VARIABLES TO YOUR NEEDS

    $to = 'feedback@northstar-ems.us';

    $subject = 'Feedback';

    $headers = \"From: \" . $cleanedFrom . \"\r\n\";
    $headers .= \"Reply-To: \". strip_tags($_POST['req-email']) . \"\r\n\";
    $headers .= \"MIME-Version: 1.0\r\n\";
    $headers .= \"Content-Type: text/html; charset=ISO-8859-1\r\n\";

    if (mail($to, $subject, $message, $headers)) {
    echo 'Your message has been sent.';
    } else {
    echo 'There was a problem sending the email.';
    }

    // DON'T BOTHER CONTINUING TO THE HTML...
    die();

    }
    } else {

    if (!isset($_SESSION[$form.'_token'])) {

    } else {
    echo \"Hack-Attempt detected. Got ya!.\";
    writeLog('Formtoken');
    }

    }

    if($_POST['submit'] && is_numeric($_POST['input'])) {
    //process data...
    //then die or redirect otherwise the form below will be shown again
    }
    ?>

    <!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\" xml:lang=\"fr\" lang=\"fr\">
    <head>
    <title><?php bloginfo('name'); ?><?php if ( is_single() ) { ?>&raquo; Blog Archive<?php } ?><?php wp_title(); ?></title>
    <meta http-equiv=\"content-type\" content=\"application/xhtml+xml; charset=UTF-8\" />
    <link rel=\"stylesheet\" href=\"<?php bloginfo('stylesheet_url'); ?>\" type=\"text/css\" />
    <?php wp_head(); ?>
    <!--[if IE]>
    <link rel=\"stylesheet\" href=\"http://www.northstar-ems.us/wp-content/themes/northblue/ie7.css\" type=\"text/css\" media=\"screen\" />
    <![endif]-->
    <!--[if lt IE 7.]>
    <link rel=\"stylesheet\" href=\"http://www.northstar-ems.us/wp-content/themes/northblue/ie6.css\" type=\"text/css\" media=\"screen\" />
    <script type=\"text/javascript\" src=\"http://www.northstar-ems.us/wp-content/themes/northblue/scripts/pngfix.js\"></script>
    <![endif]-->
    <link rel=\"stylesheet\" href=\"http://www.northstar-ems.us/wp-content/themes/northblue/css/jqtransform.css\" type=\"text/css\" media=\"all\" />
    <script src=\"http://www.google.com/jsapi\" type=\"text/javascript\"></script>
    <script type=\"text/javascript\">
    google.load(\"jquery\", \"1.3.2\");
    </script>
    <script type=\"text/javascript\" src=\"http://www.northstar-ems.us/wp-content/themes/northblue/js/jquery.jqtransform.js\"></script>
    <script type=\"text/javascript\" src=\"http://www.northstar-ems.us/wp-content/themes/northblue/js/jquery.validate.js\"></script>
    <script type=\"text/javascript\" src=\"http://www.northstar-ems.us/wp-content/themes/northblue/js/jquery.form.js\"></script>
    <script type=\"text/javascript\" src=\"http://www.northstar-ems.us/wp-content/themes/northblue/js/websitechange.js\"></script>
    <link rel=\"alternate\" type=\"application/rss+xml\" title=\"<?php bloginfo('name'); ?> RSS Feed\" href=\"<?php bloginfo('rss2_url'); ?>\" />
    <link rel=\"pingback\" href=\"<?php bloginfo('pingback_url'); ?>\" />
    </head>
    <?php
    // generate a new token for the $_SESSION superglobal and put them in a hidden field
    $newToken = generateFormToken('form1');
    ?>
    <body>
    <div id=\"container\">
    <div id=\"header\">
    <div id=\"logser\">
    <div id=\"logo\"><a id=\"logolink\" href=\"<?php bloginfo('siteurl'); ?>\"></a></div>
    <div id=\"search\">
    <div id=\"sbox\">
    <form id=\"searchform\" method=\"get\" action=\"<?php bloginfo('home'); ?>\">
    <input type=\"text\" name=\"s\" id=\"searchb\" size=\"15\" />
    <input type=\"submit\" id=\"searchs\" value=\"<?php _e('Search'); ?>\" />
    </form>
    </div>
    </div>
    </div>
    <div id=\"nav\">
    <ul>
    <?php if(is_page('home')) { ?>
    <li class=\"nav-home-off\"><a href=\"<?php bloginfo('siteurl'); ?>\"></a></li>
    <?php } else { ?>
    <li class=\"nav-home-off\"><a href=\"<?php bloginfo('siteurl'); ?>\"></a></li>
    <?php } ?>
    <?php if(is_page('services')) { ?>
    <li class=\"nav-serv-on\"><a href=\"<?php bloginfo('siteurl'); ?>?page_id=6\"></a></li>
    <?php } else { ?>
    <li class=\"nav-serv-off\"><a href=\"<?php bloginfo('siteurl'); ?>?page_id=6\"></a></li>
    <?php } ?>
    <?php if(is_page('information')) { ?>
    <li class=\"nav-info-on\"><a href=\"<?php bloginfo('siteurl'); ?>?page_id=24\"></a></li>
    <?php } else { ?>
    <li class=\"nav-info-on\"><a href=\"<?php bloginfo('siteurl'); ?>?page_id=24\"></a></li>
    <?php } ?>
    <?php if(is_page('locations')) { ?>
    <li class=\"nav-loc-on\"><a href=\"<?php bloginfo('siteurl'); ?>?page_id=32\"></a></li>
    <?php } else { ?>
    <li class=\"nav-loc-off\"><a href=\"<?php bloginfo('siteurl'); ?>?page_id=32\"></a></li>
    <?php } ?>
    <?php if(is_page('careers')) { ?>
    <li class=\"nav-car-on\"><a href=\"<?php bloginfo('siteurl'); ?>?page_id=30\"></a></li>
    <?php } else { ?>
    <li class=\"nav-car-off\"><a href=\"<?php bloginfo('siteurl'); ?>?page_id=30\"></a></li>
    <?php } ?>
    </ul>
    </div>
    </div>
    <div id=\"content-wrap\">
    <ul id=\"motto\">\"Dedicated to providing personalized and professional care to our community.\"</ul>
    <div id=\"feattop\"></div>
    <div id=\"appcon\">
    <div id=\"feedback\"></div>
    <h1 id=\"feedh1\">NorthStar EMS values input received from our Customers and Team Members. While the following information about you is \"Optional\" NorthStar EMS would ask that you provide your contact information so that we can respond back to you. Thanks for taking the time to provide us Feedback!</h1>
    <form action=\"index.php\" method=\"post\" id=\"change-form\">
    <input type=\"hidden\" name=\"token\" value=\"<?php echo $newToken; ?>\">
    <div class=\"rowElem\">
    <label for=\"req-name\">Your Name*:</label>
    <input type=\"text\" id=\"req-name\" name=\"req-name\" class=\"required\" minlength=\"8\" value=\"<?php echo $_COOKIE[\"WRCF-Name\"]; ?>\" />
    </div>
    <div class=\"rowElem\">
    <label for=\"req-phone\">Area Code + Phone Number*:</label>
    <input type=\"text\" id=\"req-phone\" name=\"req-phone\" class=\"required\" minlength=\"10\" value=\"<?php echo $_COOKIE[\"WRCF-Phone\"]; ?>\" onkeyup=\"javascript:this.value=this.value.replace(/[^0-9]/g, '');\" />
    </div>
    <div class=\"rowElem\">
    <label for=\"req-position\">Position*:</label>
    <input type=\"text\" id=\"req-position\" name=\"req-position\" class=\"required\" minlength=\"8\" value=\"\" />
    </div>
    <div class=\"rowElem\">
    <label for=\"req-email\">Your Email:</label>
    <input type=\"text\" name=\"req-email\" class=\"required email\" value=\"<?php echo $_COOKIE[\"WRCF-Email\"]; ?>\" />
    </div>
    <div id=\"curTextArea\">
    <div class=\"rowElem\">
    <label for=\"curText\">Message:</label>
    <textarea cols=\"40\" rows=\"8\" name=\"curText\"></textarea>
    </div>
    </div>
    <div class=\"rowElem\">
    <label> &nbsp; </label>
    <input type=\"submit\" value=\"Submit Feedback\" />
    </div>
    <div class=\"rowElem\">
    <label> &nbsp; </label>
    <input type=\"checkbox\" name=\"save-stuff\" />
    <label for=\"save-stuff\">&nbsp; Save Name and Email?</label>
    </div>
    </form>
    </div>
    <div id=\"boxend\"></div>
    </div>
    </body>
    </html>

    <?php get_footer(); ?>
  • To clarify this post. I have edited this form a few times over the past month. I don't expect a hand out, just someone who knows php to check and see if I combined something in the php which is preventing it from functioning correctly.