Solved

Form Validation - PHP Variables to URL

Posted on 2007-03-28
14
333 Views
Last Modified: 2013-11-19
I have a validation script that I always use to secure my forms to prevent automatic submissions by bots.

This validation script works as follows:

The individual copies the digits into a textbox that is displayed in an image file, the form is then submitted to a seperate php page that validates the input.

I'm now using another script called Form Tools, this script inserts form submissions into mysql and provides an interface to edit submissions and handle email notifications.

I now need to integrate the two, to prevent automatic submissions into my database.  The only problem I have is that I do not know how to pass the variables in the form to the URL if I use a seperate page for validation.

My current script uses the mailto: function, I now need to know how to pass my variables to a URL via PHP and not via the action field in my form:

as in <form name="subscribeform" action="http://www.splice.co.za/forms/process.php" method="post" onSubmit="return CheckAll();"> but using PHP and not html.

This is what my process script looks like:

<?php

// ------------- MAIL & URL CONFIGURABLE SECTION --------------------- //

$mailto = 'email@domain.com';
$subject = "Contact Form" ;
$formurl = "http://www.domain.com/contact_us.php" ;
$errorurl = "http://www.domain.com/error.php";
$imageerrorurl = "http://www.domain.com/imageerror.php";
$thankyouurl = "http://www.domain.com/thankyou.php" ;

// ------------- END OF CONFIGURABLE SECTION ------------------------ //  



// ------------- VARIABLE CONFIGURABLE SECTION ---------------------- //

$name = $_POST['name'] ;
$email = $_POST['email'] ;
$comments = $_POST['comments'] ;  
$code = $_POST['code'] ;

// ------------- END OF VARIABLE CONFIGURABLE SECTION --------------- //    

      
      // define url that indicates where submission comes from
      $http_referrer = getenv( "HTTP_REFERER" );

      // start PHP session
         session_start();

            // check for posted form
            
            if       (isset($_POST['contact']))
                  {
             
             // see if the code the user typed matched the generated code
       
        if       (strtoupper($_POST['code']) != $_SESSION['code'])
              {
              header("Location: $imageerrorurl");
              }
              else
              {
               
           
                // process the variables to email
               
                $messageproper =

            "This message was sent from:\n" .
            "$http_referrer\n" .
            "------------------------- FORM SUBMISSION -------------------------\n\n" .
      
                   "Client:\n" .
            $name .
            "\n" .
      
            "E-mail:\n" .
                   $email .
                  "\n" .      
            
                 "Comment:\n" .
                  $comments .
                 "\n" .      
            
                 "\n\n------------------------------------------------------------\n" ;


            mail($mailto, $subject, $messageproper, "From: \"$name\" <$email>\nReply-To: \"$name\" <$email>\nX-Mailer: chfeedback.php 2.02" );
            header( "Location: $thankyouurl" );
            exit ;  
            
                     
                     
         }
      }
     
?>

How do I send my variables to a URL instead of mailto: ?

Please Help...
0
Comment
Question by:MariaCM
  • 4
  • 4
  • 3
14 Comments
 
LVL 6

Expert Comment

by:deresh
ID: 18806721
so, you want do send posted variables to another url instead of sending an email?

if so, just in a part of your code where you build and send an email just use header to call your url with parameters, like:


$url = "http://someserver.com/yourscript.php?email=".$email."&name=".$email."&comments=".$comments;

header("Location: $url");


or you could put all your post variables into session and then redirect to plain url
0
 

Author Comment

by:MariaCM
ID: 18806817
I understand in theory what to do, but I would like to learn how to code this.

If had this form:

form.php
##################################################################################

      <form name="subscribeform" action="process.php">
      <input type="hidden" name="form_tools_initialize_form" value="1" />
      <input type="hidden" name="form_tools_form_id" value="3" />
      <table border="0" cellpadding="0" cellspacing="0" summary="feedback form" WIDTH=350>
      <tr><td ALIGN="left"><br></td></tr>
      <tr><td ALIGN="left">Name and Surname:&nbsp;</td><td><input type="text" name="name" size="25"></td></tr>
      <tr><td ALIGN="left">E-mail:</td><td><input type="text" name="email" size="25"></td></tr>  
      <tr><td>Please copy the digits in the image (case sensitive):</td><td><div id="security"><img src="security-image.php?width=160" width="200" height="60" alt="Security Image" /></div></td></tr>
      <tr><td>to the textbox on the right:</td><td><input type="text" name="code" id="code" value="" /></td></tr>
      <tr><td ALIGN="left"><br><input type="submit" name="send" id="contact" value="Submit" /><td></td></tr>  
      </td><td></td>
      </tr>
      </table>
      </form>  

##################################################################################

How would my process page look like:

process.php
##################################################################################
//script to validate security input

// define url that indicates where submission comes from
      $http_referrer = getenv( "HTTP_REFERER" );

      // start PHP session
         session_start();

            // check for posted form
           
            if       (isset($_POST['contact']))
                  {
             
             // see if the code the user typed matched the generated code
       
        if       (strtoupper($_POST['code']) != $_SESSION['code'])
              {
              header("Location: $imageerrorurl");
              }
              else
              {

//script to process variables to URL http://www.splice.co.za/forms/process.php

??????????

###################################################################################

Or is there a way that I can validate the security code input on-page like I do all the other values with javascript and still submitting with action="url"?

Your assistance is appreciated.
0
 
LVL 29

Expert Comment

by:rdivilbiss
ID: 18811088
>>Or is there a way that I can validate the security code input on-page like I do all the other values with javascript and still submitting with action="url"?

No, client side validation is easily defeated.

<?PHP
session_start();
$http_referrer = $_SERVER("HTTP_REFERER");

// ------------- MAIL & URL CONFIGURABLE SECTION --------------------- //

$mailto = 'email@domain.com';
$subject = "Contact Form" ;
$formurl = "http://www.domain.com/contact_us.php" ;
$errorurl = "http://www.domain.com/error.php";
$imageerrorurl = "http://www.domain.com/imageerror.php";
$thankyouurl = "http://www.domain.com/thankyou.php" ;

// ------------- END OF CONFIGURABLE SECTION ------------------------ //  



// ------------- VARIABLE CONFIGURABLE SECTION ---------------------- //

$name = $_POST['name'] ;   // <-- these must be filtered!!!!
$email = $_POST['email'] ;
$comments = $_POST['comments'] ;  
$code = $_POST['code'] ;

// ------------- END OF VARIABLE CONFIGURABLE SECTION --------------- //    



// correct way to check for a post
if ($_SERVER['REQUEST_METHOD']=="POST") {
    if  isset($_POST['code'])) {
        if (strtoupper($_POST['code']) != $_SESSION['code']) {
             // error
            header("Location: $imageerrorurl");
        }else{
             // okay
             processForm($mailto, $subject, $formurl, $name, $email, $contents, $thankyouurl);
        }
    }
}

function processForm($mailto, $subject, $formurl, $name, $email, $contents, $thankyouurl) {
    // code to process for, send e-mail whatever.

    // when done
    header("Location: $thankyouurl");

}
?>
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
LVL 29

Expert Comment

by:rdivilbiss
ID: 18813234
and...

function processForm($mailto, $subject, $formurl, $name, $email, $contents, $thankyouurl) {
    // code to process for, send e-mail whatever.
      $headers  = "MIME-Version: 1.0\r\n";
      $headers .= "Content-type: text/html; charset=utf-8\r\n";

      /* additional headers */
      $headers .= "To: $to\r\n";
      $headers .= "From: Your Site<webmaster@yourdomain.com>\r\n";
      $headers .= "X-Mailer: PHP/" . phpversion();
      
      $mailHead = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\">\n";
      $mailHead .= "<html>\n<head>\n  <META http-equiv=Content-Type content=\"text/html; charset=utf-8\">\n";
      $mailHead .= "  <title>Form Submission</title>";
      }
      $mailHead .= "  <style type=\"text/css\">\n  /*<![CDATA[*/\n";
      $mailHead .= "    BODY {background: #FFF; font-family: Tahoma, Arial, Sans-Serif; font-size: 12pt; color: #000000 }\n";
      $mailHead .= "  /*]]>*/\n  </style>\n</head>\n";
      
      $mailBody = "<body>\n  <div>\n";
        $mailbody .= " From: " . "$http_referrer\n";
      $mailBody .= "    $formurl\n\n";
        $mailBody .= " Client:  $name\n";
        $mailBody .= " E-Mail:  $email\n\n";
        $mailBody .= "    $contents\n";
      $mailBody .= "  </div>\n</body>\n</html>";

      mail($mailto, $subject, $mailHead.$mailBody, $headers);      

       header( "Location: $thankyouurl" );
}
0
 
LVL 6

Expert Comment

by:deresh
ID: 18814333
mail($mailto, $subject, $messageproper, "From: \"$name\" <$email>\nReply-To: \"$name\" <$email>\nX-Mailer: chfeedback.php 2.02" );
            header( "Location: $thankyouurl" );
            exit ;  


replace line with header with:

$url =
"http://www.splice.co.za/forms/process.php?email=".$email."&name=".$email."&comments=".$comments;

header("Location: $url");

the only thing that you have to do now is to in your process.php script is to retrueve sent variables with $_GET['email'] and after saving all that to database redirect to $thankyouurl at the end of your script like:


$thankyouurl = "http://www.domain.com/thankyou.php" ;
header( "Location: $thankyouurl" );


this modification will send you an email and then redirect to proccess.php script.

but you could easily do all that in this script to simplify the proccess.


0
 

Author Comment

by:MariaCM
ID: 18818586
Deresh,

I'm trying the url option, but the script keeps on flaring up problems.  It works perfectly until I replace the $thankyouurl with http://www.splice.co.za/forms/process.php?name=".$name."&email=".$email;

see example: http://www.splice.co.za/formtest/submit.php

Where is my syntax problem?

<?php

// ------------- MAIL & URL CONFIGURABLE SECTION --------------------- //

$mailto = 'mcmbotha@splice.co.za' ;
$subject = "Contact Form" ;
$formurl = "http://www.splice.co.za/test/contact.php" ;
$errorurl = "http://www.splice.co.za/test/error.html";
$imageerrorurl = "http://www.splice.co.za/test/imageerror.html";
$thankyouurl = "http://www.splice.co.za/forms/process.php?name=".$name."&email=".$email;" ;


// ------------- END OF CONFIGURABLE SECTION ------------------------ //  



// ------------- VARIABLE CONFIGURABLE SECTION ---------------------- //

$name = $_POST['name'] ;
$email = $_POST['email'] ;  
$code = $_POST['code'] ;

// ------------- END OF VARIABLE CONFIGURABLE SECTION --------------- //    

      
      // define url that indicates where submission comes from
      $http_referrer = getenv( "HTTP_REFERER" );

      // start PHP session
         session_start();

            // check for posted form
            
            if       (isset($_POST['contact']))
                  {
             
             // see if the code the user typed matched the generated code
       
        if       (strtoupper($_POST['code']) != $_SESSION['code'])
              {
              header("Location: $imageerrorurl");
              }
              else
              {
               
           
                // process the variables to email
               
                $messageproper =

            "This message was sent from:\n" .
            "$http_referrer\n" .
            "------------------------- FORM SUBMISSION -------------------------\n\n" .
      
                   "Client:\n" .
            $name .
            "\n" .
      
            "E-mail:\n" .
                   $email .
                  "\n" .      
            
            
                 "\n\n------------------------------------------------------------\n" ;


            mail($mailto, $subject, $messageproper, "From: \"$name\" <$email>\nReply-To: \"$name\" <$email>\nX-Mailer: chfeedback.php 2.02" );
            header( "Location: $thankyouurl");
            exit ;            
                     
         }
      }
     
?>

0
 

Author Comment

by:MariaCM
ID: 18818621
P.S. I do not need to email the variables at all, I just need to pass them to the url. In other words I need to do exactly what would have happened if I submitted the form with action="www.splice.co.za/froms/process.php", only I do not want to pass the security image code.  I only want to pass &email and $name, so essentailey I can leave out the mail() function and go directly to header (), but I can't seem to get the url format correct.
0
 
LVL 29

Accepted Solution

by:
rdivilbiss earned 250 total points
ID: 18818829
MariaCM, it sounds like your second form processor is expecting to receive posted fields, so if you are passing them on the URL that will not work.

If the second page is doing $_POST['field'] then it will need to be looking for $_GET['field'].
0
 

Author Comment

by:MariaCM
ID: 18819617
Ok, I accept that argument, but I still do not know how to solve the problem.

The second page is expecting the variables via a submitted form > action="url"

How do I simulate this in my "in-between" processing page?
0
 
LVL 29

Expert Comment

by:rdivilbiss
ID: 18819727
I wouldn't.  I'd put your validation and e-mail code in the second page rather than trying to submit to two pages.
0
 
LVL 6

Assisted Solution

by:deresh
deresh earned 250 total points
ID: 18822216
you could do a "fake" POST to your script so you don't have to change processing script

you have and example on this page:

http://www.zend.com/forums/index.php?t=msg&goto=9101&S=5ce46abf9ba994f0b7e7811af13b94ad

just replace mail sending code with this "fake" POST code
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial demonstrates how to identify and create boundary or building outlines in Google Maps. In this example, I outline the boundaries of an enclosed skatepark within a community park.  Login to your Google Account, then  Google for "Google M…

816 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now