Validating user input

strpos(): finds the position of the first case-sensitive occurrence of a substring in a string strpos($string,sub-string); strrpos(): finds the position of the last case-sensitive occurrence of a substring in a string strrpos($string,sub-string); substr_count(): returns the number of times one string occurs within another substr_count($string,sub-string);

pptx30 trang | Chia sẻ: dntpro1256 | Lượt xem: 576 | Lượt tải: 0download
Bạn đang xem trước 20 trang tài liệu Validating user input, để xem tài liệu hoàn chỉnh bạn click vào nút DOWNLOAD ở trên
Validating user inputBasharat Mahmood, Department of Computer Science,CIIT,Islamabad, Pakistan.1Summary of the previous lectureSuper Global variablesPassing form dataPassing data with sessionsBasharat Mahmood, Department of Computer Science,CIIT,Islamabad,Pakistan.2OutlineRegular expressions in PHPValidating user input at serverString functionsBasharat Mahmood, Department of Computer Science,CIIT,Islamabad, Pakistan.31. Regular expressions in PHPA regular expression is a concise notation to describe patterns in strings Regular expressions provide the foundation for describing or matching data according to defined syntax rulesExample: |^[0-9]{2}-[0-9]{2}-[0-9]{4}$| Basharat Mahmood, Department of Computer Science,CIIT,Islamabad, Pakistan.41. Regular expressions in PHP |^[0-9]{2}-[0-9]{2}-[0-9]{4}$| Basharat Mahmood, Department of Computer Science,CIIT,Islamabad, Pakistan.5Start and end of RESub patternSub patternSub patternStart matching from the startMatch the end of the stringSub pattern with fixed characterAllowed characterslength[0-9]{2}1. Regular expressions in PHPBasharat Mahmood, Department of Computer Science,CIIT,Islamabad, Pakistan.6Start and end of the RE: optional, ||Sub-patterns: range of allowed charactersAllowed lengthSub-patterns with fixed character:1. Regular expressions in PHPBasharat Mahmood, Department of Computer Science,CIIT,Islamabad, Pakistan.7Matching from the start:1212-12-2014Matching till end:12-12-2014123For exact match we should use both ^ and $Pattern exists if do not match from startPattern exists if do not match till end1.1 Notations for RE^: match strings that start with the given pattern$: match strings that end with the given pattern-: means a range of characters[ ]: makes a class of characters[^ ]: negates the class of characterBasharat Mahmood, Department of Computer Science,CIIT,Islamabad, Pakistan.81.1 Notation for REQuantifiers:{n}: matches a character, class or sub-pattern for n times{ n, m}: matches a character, class or sub-pattern for minimum n times and maximum m timesBasharat Mahmood, Department of Computer Science,CIIT,Islamabad, Pakistan.91.1 Notation for RE?: matches the character, class or sub-pattern 0 or 1 time equal to {0,1}+: matches the character, class or sub-pattern 1 or more times equals to {1, } *: matches the character, class or sub-pattern 0 or any number of time equals {0, }Basharat Mahmood, Department of Computer Science,CIIT,Islamabad, Pakistan.101.1 Notation for REPredefined character ranges:\d: means exactly as [0-9]\D: means exactly as [^0-9] \w: means exactly as [a-zA-Z0-9]Basharat Mahmood, Department of Computer Science,CIIT,Islamabad, Pakistan.111.1 Notation for RERE examples:Validating date:|^\d{2}-\d{2}-\d{4}$|Validating CNIC:|^\d{5}-\d{7}-\d{1}$|Validating Email:|^[a-zA-Z0-9_.]+@[a-z]{3,5}.[a-z]{2,3}$|Basharat Mahmood, Department of Computer Science,CIIT,Islamabad, Pakistan.121.1 Notation for REValidating name:|^[a-zA-Z ]{5,25}$|Validating Password:must contain ‘@’|@|Basharat Mahmood, Department of Computer Science,CIIT,Islamabad, Pakistan.132. Validating user’s inputpreg_match():searches a string for a specific patternreturns TRUE if it exists and FALSE otherwisepreg_match(“pattern”,$string);Basharat Mahmood, Department of Computer Science,CIIT,Islamabad, Pakistan.142. Validating user’s inputBasharat Mahmood, Department of Computer Science,CIIT,Islamabad, Pakistan.15nameemailcnicdobPost, action.php2. Validating user’s inputBasharat Mahmood, Department of Computer Science,CIIT,Islamabad, Pakistan.16Receiving values Validating name2. Validating user’s inputBasharat Mahmood, Department of Computer Science,CIIT,Islamabad, Pakistan.17emailCNICDoB3. String functions in PHPstrlen():Returns the length of the stringstrlen($string);strcmp():Compares two stringsReturns 0 if strings are equal, 1 if first string is greater and -1 if second is greaterstrcmp($string1,$string2);Strcasecmp():Compares two strings in case insensitive mannerstrcasecmp($string1,$string2);Basharat Mahmood, Department of Computer Science,CIIT,Islamabad, Pakistan.183. String functions in PHPBasharat Mahmood, Department of Computer Science,CIIT,Islamabad, Pakistan.19namepasspass1Method=post3. String functions in PHPBasharat Mahmood, Department of Computer Science,CIIT,Islamabad, Pakistan.20Getting variablesUsing strlen()3. String functions in PHPBasharat Mahmood, Department of Computer Science,CIIT,Islamabad, Pakistan.21Password is short3. String functions in PHPBasharat Mahmood, Department of Computer Science,CIIT,Islamabad, Pakistan.22Compares pass and pass13. String functions in PHPstrtolower():Convert a string in lower casestrtolower($string);strtoupper():Convert a string in upper casestrtoupper($string);ucfirst():Convert the first character of a string to upper caseucfirst($string);ucwords():Convert the first character of each word in a string to upper caseucfirst($string);Basharat Mahmood, Department of Computer Science,CIIT,Islamabad, Pakistan.233. String functions in PHPBasharat Mahmood, Department of Computer Science,CIIT,Islamabad, Pakistan.24Converts name to lowercaseConverts name to uppercaseUsing ucwords()Using ucfirst()3. String functions in PHPBasharat Mahmood, Department of Computer Science,CIIT,Islamabad, Pakistan.25Lowercaseuppercaseucfirst()ucwords()3. String functions in PHPstrpos():finds the position of the first case-sensitive occurrence of a substring in a stringstrpos($string,sub-string);strrpos():finds the position of the last case-sensitive occurrence of a substring in a stringstrrpos($string,sub-string);substr_count():returns the number of times one string occurs within anothersubstr_count($string,sub-string);Basharat Mahmood, Department of Computer Science,CIIT,Islamabad, Pakistan.263. String functions in PHPBasharat Mahmood, Department of Computer Science,CIIT,Islamabad, Pakistan.27Finding first occurrence of ‘a’Last occurrence of ‘a’Finding number of occurrences of ‘a’3. String functions in PHPBasharat Mahmood, Department of Computer Science,CIIT,Islamabad, Pakistan.28First occurrence of ‘a’Last occurrence of ‘a’Number of occurrences of ‘a’SummaryWriting regular expression in PHPValidating user’s inputString functionsBasharat Mahmood, Department of Computer Science,CIIT,Islamabad, Pakistan.29References Chapter 9, “Beginning PHP and MySQL” by W. Jason Gilmore, Apress publisher, 4th edition; 2010, ISBN-13 (electronic): 978-1-4302-3115-8.Basharat Mahmood, Department of Computer Science,CIIT,Islamabad, Pakistan.30

Các file đính kèm theo tài liệu này:

  • pptxlec27_validating_user_input_4598_2026975.pptx
Tài liệu liên quan