if statements and validation. if statement in programming the if statement allows one to test...

19
If statements and validation

Upload: ophelia-daniela-horn

Post on 04-Jan-2016

221 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: If statements and validation. If statement In programming the if statement allows one to test certain conditions and respond differently depending on

If statements and validation

Page 2: If statements and validation. If statement In programming the if statement allows one to test certain conditions and respond differently depending on

If statement

• In programming the if statement allows one to test certain conditions and respond differently depending on the outcome of the test. – In our example the condition will be that the

user actually entered some text. • If it is true, one set of actions will be performed.• If it is false, a different set of actions will be

performed.

Page 3: If statements and validation. If statement In programming the if statement allows one to test certain conditions and respond differently depending on

Two places to validate

• Since we are considering a client-server interaction, there are two locations in which the validation can occur – on the client and on the server. – Client-side validation should be seen mainly as not

adding to internet traffic and not wasting the server’s time until the data is acceptable.

– Server-side validation should be seen as maintaining data integrity (ensuring the data is of valid format) and security (making sure the user is not trying to access more than they should_

Page 4: If statements and validation. If statement In programming the if statement allows one to test certain conditions and respond differently depending on

Server-side if

Test if the user entered any text in the text field. If the text field was left blank print one message. The “else” handles the other case and prints the original Thank-you message.

Notice when asking if two things are equal one uses two equal signs!

Page 5: If statements and validation. If statement In programming the if statement allows one to test certain conditions and respond differently depending on

Result of invalid user data

Page 6: If statements and validation. If statement In programming the if statement allows one to test certain conditions and respond differently depending on

Using elseif to ask another question

Page 7: If statements and validation. If statement In programming the if statement allows one to test certain conditions and respond differently depending on

Another approach is to use a Boolean operator – in this case || the OR operator

If it is true that either of the text fields was left blank then the first message will be printed out.

Page 8: If statements and validation. If statement In programming the if statement allows one to test certain conditions and respond differently depending on

If the user includes HTML tags

Page 9: If statements and validation. If statement In programming the if statement allows one to test certain conditions and respond differently depending on

Code to strip away any HTML (or PHP) in user’s data

Page 10: If statements and validation. If statement In programming the if statement allows one to test certain conditions and respond differently depending on

Example: <script> tag eliminated

Eliminating tags that signal code may help with a problem known as “cross site scripting.”

Page 11: If statements and validation. If statement In programming the if statement allows one to test certain conditions and respond differently depending on

The quote - slash quote problem

Page 12: If statements and validation. If statement In programming the if statement allows one to test certain conditions and respond differently depending on

The stripslashes function

Page 13: If statements and validation. If statement In programming the if statement allows one to test certain conditions and respond differently depending on

Result of stripslashes

Page 14: If statements and validation. If statement In programming the if statement allows one to test certain conditions and respond differently depending on

Sometimes the slashes are a good thing

• If a user attempts to put in SQL (database query) code, this is known as “SQL Injection.”

• SQL Injection often uses quotes (single or double).

• The slash tells the system to interpret the quote as a data quote not as a SQL quote.

• In fact PHP has an addslashes function for this purpose

Page 15: If statements and validation. If statement In programming the if statement allows one to test certain conditions and respond differently depending on

PHP addslashes function

Page 16: If statements and validation. If statement In programming the if statement allows one to test certain conditions and respond differently depending on

Related function

Page 17: If statements and validation. If statement In programming the if statement allows one to test certain conditions and respond differently depending on

Result with a space in the First Name field

Page 18: If statements and validation. If statement In programming the if statement allows one to test certain conditions and respond differently depending on

The trim function

Page 19: If statements and validation. If statement In programming the if statement allows one to test certain conditions and respond differently depending on

Reference

• PHP for the World Wide Web, Second edition, Larry Ullman