Simple input filtering in PHP

Here’s one I don’t see talked about much:

$name = filter_input(INPUT_POST, 'name', FILTER_SANITIZE_STRING);

What this does is grab the “name” element from your POST array and run it through the the filter: FILTER_SANITIZE_STRING… which removes all HTML tags from the string (since we know a person’s name doesn’t have HTML in it).

It’s a really simple way of quickly filtering your data.

Here’s another:

if ( ! $email = filter_input( INPUT_POST, 'email', FILTER_VALIDATE_EMAIL ) ) {
 die('Invalid email');
}

This one validates the submitted email address.

And, returns false if it’s invalid.

Again, a very simple way of quickly validating your data.

And, there’s a roughly 20-30 different filters you can run your data through. Things like: FILTER_VALIDATE_URL, FILTER_VALIDATE_INT, FILTER_SANITIZE_SPECIAL_CHARS, FILTER_SANITIZE_EMAIL and more.

The idea here is:

  1. Filter data like this on input
  2. Escape it on submission to MySQL
  3. Escape it on output to the browser

That little routine will help you prevent invalid data, SQL injection, XSS attacks and whole host of other problems.

Anyway, it’s one of the things I show you in Module 3 of PHP 101. Along with, of course: CRUD basics, PDO, MySQLi, Prepared Statements… plus building a database class AND submitting HTML form data to MySQL using all of this.

Use this link to get the details on the course and grab Modules 1-3: http://johnmorrisonline.com/php

Later,

John

You might also like

Get off your knees

Stop acting like your client’s servant — like you’re there to kowtow to their every whim and wish and you’re lucky they’ve graced you with

Read More »

Chicks dig this s***

Saw this scrub on Twitter post this namby-pamby bullshizen: === I will NEVER be impressed by your money, car, looks, social status or job title.

Read More »
Share on facebook
Facebook
Share on twitter
Twitter
Share on linkedin
LinkedIn
Share on reddit
Reddit
Share on pinterest
Pinterest
John Morris

JOHN MORRIS

I’m a 15-year veteran of freelance web development. I’ve worked with bestselling authors and average Joe’s next door. These days, I focus on helping other freelancers build their freelance business and their lifestyles.

The simple secrets to high-paying freelance clients

What makes clients willing to pay $5,000, $10,000 even $20,000 and up for your services? Download and install my mobile app and I’ll show you. It’s free. Just click the button below:

Clients Like:

Inc. Magazine Logo
Lewis Howes Logo
Ray Edwards Logo

WHAT OTHERS ARE SAYING

Oliver Wainwright

Oliver Wainwright

I’m a fan. I have completed several of John’s courses. I find him very knowledgeable and he has a great delivery.

Aaron Gott

Aaron Gott

John has a particular knack for the development and training of others.

Lewis Howes

John is amazing at building membership sites. He converted one of my sites over from it’s existing (hardly working) platform over to the clean and simple to use WishList membership platform. I highly recommend using John and WishList for any of your membership site needs.

Bradley Smith

John and I have worked together on numerous projects. John is very quick and efficient and was a pleasure to work with.

Steve Dimmick

Steve Dimmick

John has provided expert knowledge and advice on multiple occasions that have helped me better serve my clients. John is a Rockstar!

Andrew Malone

Andrew Malone

John Morris is exceptional in his ability to give focused insight into Freelancing and starting one’s business. His direct methods inspire confidence in his honesty.

Thabo Motsoahae

John is one of the best instructors I have come across, I learned a lot from his online tutorials.

Jason Rumley

Jason Rumley

John has a habit of over-delivering on the expectations he sets up. That’s why he’s the best.

Bob Patterson

Not only is John a very talented programmer and developer, he is also an excellent communicator. He has a talent for taking complex subjects and communicating them in terms that anyone can understand. This is a rare combination. This ability has enabled me to take my skills and knowledge to the next level. Thank you John for for all that you do.

Lori Grant

John did an outstanding job on my project. I highly recommend him and look forward to working with him on future projects.

Ray Edwards

I recommend John every chance I get. If every person I worked with were as committed to excellence, punctuality, value, and unquestionable integrity… the world would be a better place. Highest recommendation.

Jim DeJonge

Jim DeJonge

John has a relaxed and engaging manner. His advice is solid and the explanations are well thought out.

Xan Barksdale

Xan Barksdale

Very professional worker who is extremely knowledgable in WordPress and Wishlist Member. I would definitely hire again.