250 likes | 516 Views
Server Side Validation with PHP. By Ben Dougherty. What is Server Side Validation ?. A term used to validate user input on a web server. To understand server side validation we need to first understand HTTP requests. What are HTTP Requests?.
E N D
Server Side Validation with PHP By Ben Dougherty
What is Server Side Validation? A term used to validate user input on a web server. To understand server side validation we need to first understand HTTP requests.
What are HTTP Requests? Hyper Text Transfer Protocol or HTTP is a term used to describe 8 methods available to us for communicating with a resource on a server. We are only going to talk about two of these today, GET and POST
HTTP Request Steps HTTP requests are sent from the users browser to our web server. The web server processes the request and responds sending the requested page back to the web browser Validation code runshere at the server Database Client Requests Web Page Client’s Browser Server Server processes request and serves up web page
POST Vs GET Both POST and GET requests use URL encoding and data is sent in key, value pairs. The GET method sends all data in the URL The POST method sends data in the header of the HTTP request.
How to decide which one to use? Generally speaking GET should be used for displaying data, such as pagination. Eghttp://www.domain.com/index.php?page=5 POST is used when we want to hide the data been sent. Eg, Sending email or updating a database .
Why do we need validation? To prevent intentional and accidental incorrect user input. To stop people without access from entering restricted area’s. To prevent damaging behaviour on the server, such as updating a database.
Server Side Vs Client Side Client Side Client side validation is displayed faster to the user. Client side validation means no page refresh and therefore is instantly displayed. Unfortunately the user can disable client side validation.
Server Side Vs Client Side Server Side Allows for more thorough validation Can not be disabled and therefore is more secure. Slower as information has to be sent to and from the server.
PHP and $_POST PHP or HyperText Pre-processor is a server side scripting language making dynamic web development easy. Through PHP we can use $_POST which is a global array allowing us to access all information sent via a POST request. $_REQUEST also give’s you access to information inside the HTTP request but is now out dated and insecure.
Lets see the code? Check for empty values Make sure user name is available Validate Phone numbers are numeric Validate email Check both emails match Validate length of password Check both passwords match