Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP Build a Simple PHP Application Wrapping Up The Project Validating Contact Form Data

Mayur Pande
PLUS
Mayur Pande
Courses Plus Student 11,711 Points

How would I put the sercurity validator in silex?

So in the video Randy says to use the code below to prevent spam.

//security validation to make to make sure that a spammer bot is not hijacking our form to send spam to other people
foreach($_POST as $value){
    if(stripos($value,'Content-Type:') !== FALSE){
        echo "There was a problem with the information you entered.";
        exit;
    }
  }

I get that but how would I incorporate this code within silex. I know to get all the post values from silex we use the command

$this->get('request')->request->all();

However when I store this in a variable and put that in the foreach loop from above I get a white screen, and have turned on error reporting but no errors are shown.

Below is the function I have written that is not working;

$app->post('/login', function(Request $request) use($app) {
        $email = $app['request']->get('email');
        $password = $app['request']->get('password');

        //gets all post values;
        $post =  $this->get('request')->request->all();
       //security validation to make to make sure that a spammer bot is not hijacking our form to send spam to other people
  foreach($post as $value){
    if(stripos($value,'Content-Type:') !== FALSE){
        echo "There was a problem with the information you entered.";
        exit;
    }
  }

 //security validation uses spam honeypot technique to make sure that the address field is blank
         $postcode = $app['request']->get('postcode');
         if($postcode != ""){
                echo "Your form submission has an error";
                exit;
        }

        if ($app['auth']->login($email, $password)) {
                return $app->redirect('/');
        } else {
                return $app->redirect('/login');
        }
});

As I need this for multiple forms on my website it would be helpful. It works if I use the honeypot technique but not with foreach spammer bot protection

Mayur Pande
Mayur Pande
Courses Plus Student 11,711 Points

Actually think I have managed to sort it I have replaced the ;

$post

variable in the foreach loop directly with

$this->get('request')->request->all()

so now my foreach loop looks like this;

 foreach($this->get('request')->request->all() as $value){
    if(stripos($value,'Content-Type:') !== FALSE){
        echo "There was a problem with the information you entered.";
        exit;
    }
 }

so now my whole function looks like this;

$app->post('/login', function(Request $request) use($app) {
        $email = $app['request']->get('email');
        $password = $app['request']->get('password');

       //security validation to make to make sure that a spammer bot is not hijacking our form to send spam to other people
       foreach($this->get('request')->request->all() as $value){
           if(stripos($value,'Content-Type:') !== FALSE){
              echo "There was a problem with the information you entered.";
               exit;
            }
        }

     //security validation uses spam honeypot technique to make sure that the address field is blank
         $postcode = $app['request']->get('postcode');
         if($postcode != ""){
                echo "Your form submission has an error";
                exit;
         }

        if ($app['auth']->login($email, $password)) {
                return $app->redirect('/');
        } else {
                return $app->redirect('/login');
        }
})

I am interested in knowing would this be the correct way of doing it? If not does anyone know as way of going about it?