Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Introduction to PHP


Published on

This presentation covers most of the important concepts of PHP

Published in: Education, Technology

Introduction to PHP

  1. 1. PHP :Introduction> Powered by K.Sarveswaran Department of Computer Science, University of Jaffna [email_address]
  2. 2. Some references <ul><li> </li></ul><ul><li> </li></ul><ul><li>Beginning PHP and MySQL, W.Jason Gilmore, Apress publication - 2009 </li></ul><ul><li>Programming PHP, Rasmus Lerdorf and Kevin Tatroe, O'REILLY publication - 2002 </li></ul>
  3. 3. Scripting languages <ul><li>What are scripting languages? </li></ul><ul><ul><li>high-level programming language that is interpreted by another program at runtime rather than compiled by the computer’s processor as other programming languages (such as C and C++) are. - </li></ul></ul><ul><ul><li>&quot;Scripts&quot; are distinct from the core code of the application which is usually written in a different language... Often interpreted from source code or bytecode, wheras the applications they control are traditionally compiled to native machine code. - </li></ul></ul>
  4. 4. Scripting languages <ul><li>Web Scripting </li></ul><ul><ul><li>Client-side scripting </li></ul></ul><ul><ul><ul><li>scripts are run by the viewing web browser </li></ul></ul></ul><ul><ul><li>Server-side scripting </li></ul></ul><ul><ul><ul><li>scripts are run by the web server to generate dynamic web page </li></ul></ul></ul><ul><ul><ul><li>PHP is a server-side scripting language </li></ul></ul></ul><ul><ul><ul><ul><li>PHP parser interprets PHP scripts in Server (written in C) </li></ul></ul></ul></ul>
  5. 5. Introduction to PHP <ul><li>“ What it all boils down to is that PHP was never meant to win any beauty contests . It wasn't designed to introduce any new revolutionary programming paradigms . It was designed to solve a single problem : the Web problem . That problem can get quite ugly, and sometimes you need an ugly tool to solve your ugly problem. Although a pretty tool may, in fact, be able to solve the problem as well, chances are that an ugly PHP solution can be implemented much quicker and with many fewer resources . That generally sums up PHP's stubbornness” </li></ul><ul><li>Rasmus Lerdorf </li></ul>
  6. 6. PHP - History <ul><li>PHP : </li></ul><ul><ul><li>Personal Home Page – Deprecated </li></ul></ul><ul><ul><li>PHP Hypertext Preprocessor (From PHP 3.0) </li></ul></ul><ul><li>1995 - PHP 1.0 - Personal Home Page Toolkit </li></ul><ul><li>1997 – PHP 2.0 – PHP/FI (Form Interpreter) </li></ul><ul><ul><li>FI – Database tool + Form handling </li></ul></ul><ul><li>1998 – PHP 3.0 </li></ul><ul><ul><li>New PHP Parsing engine </li></ul></ul>
  7. 7. PHP - History <ul><li>2000 - PHP 4.0 </li></ul><ul><ul><li>Many features including IIS support, OOP support </li></ul></ul><ul><li>2004 – PHP 5.0 </li></ul><ul><ul><li>More new features including Exception handling, Web Services </li></ul></ul><ul><li>? - PHP 6.0 - Dev </li></ul><ul><ul><li>New features like Native Unicode support and security enhancements </li></ul></ul>
  8. 8. Who uses <ul><li>Yahoo!,Youtube, Joomla!, Drupal... </li></ul>
  9. 9. How it works
  10. 10. 2. Setting up environment
  11. 11. 2.0 Setting up environment <ul><li>Requirements </li></ul><ul><li>Install XAMPP </li></ul><ul><li>Test the environment </li></ul>
  12. 12. 2.1 Requirements <ul><li>Web server </li></ul><ul><ul><li>Apache, IIS, GWS, nginx... </li></ul></ul><ul><ul><li>Apache is selected </li></ul></ul><ul><li>PHP engine </li></ul><ul><li>Database server </li></ul><ul><ul><li>MySQL, IBM DB2, PostgreSQL, SQL Server... </li></ul></ul><ul><ul><li>MySQL is selected </li></ul></ul>
  13. 13. 2.2 Install XAMPP <ul><li>Can download appropriate builds and install </li></ul><ul><ul><li>Correct version </li></ul></ul><ul><ul><li>Correct format </li></ul></ul><ul><li>Install as a bunch </li></ul><ul><ul><li>LAMP / WAMP / MAMP – XAMPP </li></ul></ul><ul><ul><li>A Platform </li></ul></ul>
  14. 14. 2.3 Test the environment <ul><li>Create a file called php_prog.php and save that in htdocs folder </li></ul><ul><li>Add following lines to the file </li></ul><ul><li><?php echo “PHP test”; ?> </li></ul><ul><li>Type following URL in the address bar : </li></ul><ul><ul><li>http://localhost/php_prog.php </li></ul></ul>
  15. 15. 3.0 PHP programming fundamentals
  16. 16. 3.0 PHP Programming fundamentals <ul><li>Syntax definitions </li></ul><ul><li>Operators </li></ul><ul><li>Display </li></ul><ul><li>Control structures </li></ul><ul><li>Looping statements </li></ul><ul><li>Function </li></ul><ul><li>Array </li></ul>
  17. 17. 3.1 Syntax definition <ul><li>Embedding PHP </li></ul>Method 1 <?php //php codes ?> Method 2 <? //php codes ?> *short_open_tag Method 3 <script language=''php''> //php codes </script> Method 4 <% //php codes %> *asp_tags
  18. 18. 3.1 Syntax definition <ul><li>Case sensitiveness </li></ul><ul><ul><li>The names of user-defined classes and functions, as well as built-in constructs and keywords such as echo, while, class, etc., are case-insensitive </li></ul></ul><ul><ul><li>Variables are case-sensitive </li></ul></ul>
  19. 19. 3.1 Syntax definition <ul><li>Statements end with semicolons </li></ul><ul><ul><li>Semicolon is optional before a closing PHP tag </li></ul></ul><ul><li><?php </li></ul><ul><ul><li>$variable=20 ; </li></ul></ul><ul><ul><li>ecHo 'Hello PHP' </li></ul></ul><ul><li>?> </li></ul>
  20. 20. 3.1 Syntax definition <ul><li>White spaces are not counted </li></ul><ul><ul><li>Following are same </li></ul></ul><ul><ul><li>- test($var1, $var2); </li></ul></ul><ul><ul><li>- test ( </li></ul></ul><ul><ul><ul><li>$var1 , </li></ul></ul></ul><ul><ul><ul><li>$var2 , </li></ul></ul></ul><ul><ul><ul><li>); </li></ul></ul></ul><ul><ul><li>- test($var1,$var2); </li></ul></ul>
  21. 21. 3.1 Syntax definition <ul><li>Comments : </li></ul><ul><ul><li>C/C++ style </li></ul></ul><ul><ul><ul><li>//something </li></ul></ul></ul><ul><ul><ul><li>/* something */ </li></ul></ul></ul><ul><ul><li>Unix shell style </li></ul></ul><ul><ul><ul><li>#something </li></ul></ul></ul>
  22. 22. 3.1 Syntax definition <ul><li>Identifier naming conversions </li></ul><ul><ul><li>Can be started with A-Z, a-z and _ </li></ul></ul><ul><ul><li>Can be followed by digits </li></ul></ul><ul><ul><li>Variables </li></ul></ul><ul><ul><ul><li>Starts with '$', not spaces in the middle </li></ul></ul></ul><ul><ul><ul><li>$variable </li></ul></ul></ul><ul><ul><li>Constants </li></ul></ul><ul><ul><ul><li>define('LANG', &quot;PHP&quot;); </li></ul></ul></ul>
  23. 23. 3.1 Syntax definition <ul><li>Data types </li></ul><ul><ul><li>Integers </li></ul></ul><ul><ul><ul><li>Decimal </li></ul></ul></ul><ul><ul><ul><li>Octal </li></ul></ul></ul><ul><ul><ul><ul><li>0734 or +010 </li></ul></ul></ul></ul><ul><ul><ul><li>Hex </li></ul></ul></ul><ul><ul><ul><ul><li>0xFF </li></ul></ul></ul></ul><ul><ul><li>Floats, Strings, Booleans, Array, Objects </li></ul></ul><ul><ul><li>NULL / nuLL... </li></ul></ul>
  24. 24. 3.1 Syntax definition <ul><li>Type casting </li></ul><ul><ul><li>(int), (integer) - cast to integer </li></ul></ul><ul><ul><li>(bool), (boolean) - cast to boolean </li></ul></ul><ul><ul><li>(float), (double), (real) - cast to float </li></ul></ul><ul><ul><li>(string) - cast to string </li></ul></ul><ul><ul><li>(binary) - cast to binary string (PHP 6) </li></ul></ul><ul><ul><li>(array) - cast to array </li></ul></ul><ul><ul><li>(object) - cast to object </li></ul></ul><ul><ul><li>(unset) - cast to NULL (PHP 5) </li></ul></ul>
  25. 25. 3.1 Syntax definition <ul><li>Implicit Type casting or Juggling </li></ul><ul><ul><li>Actually PHP does NOT require explicit variable declaration </li></ul></ul>Type of first operand Type of second operand Final form Integer Float Float Integer String Number Float String Float
  26. 26. 3.1 Syntax definition <ul><li>Implicit Type casting or Juggling </li></ul><ul><ul><li>If '.' or 'E' found in string, then it will be considered as a float </li></ul></ul><ul><ul><ul><li>&quot;9 Lives&quot; – 1; // 8 (int) </li></ul></ul></ul><ul><ul><ul><li>&quot;3.14 Pies&quot; * 2; // 6.28 (float) </li></ul></ul></ul><ul><ul><ul><li>&quot;9 Lives.&quot; – 1; // 8 (float) </li></ul></ul></ul><ul><ul><ul><li>&quot;1E3 Points of Light&quot; + 1; // 1001 (float) </li></ul></ul></ul>
  27. 27. 3.1 Syntax definition <ul><li>Variables </li></ul><ul><ul><li>Declaration </li></ul></ul><ul><ul><ul><li>Var $variableName; </li></ul></ul></ul><ul><ul><ul><li>Not necessary </li></ul></ul></ul><ul><ul><li>Variable variable </li></ul></ul><ul><ul><ul><li>$var='first'; </li></ul></ul></ul><ul><ul><ul><li>$$var='second'; # $first='second' </li></ul></ul></ul><ul><ul><li>Variable reference </li></ul></ul><ul><ul><ul><li>$black=& $white; </li></ul></ul></ul>
  28. 28. 3.1 Syntax definition <ul><li>Variable scopes </li></ul><ul><ul><li>Local scope </li></ul></ul><ul><ul><li>Global scope </li></ul></ul><ul><ul><ul><li>Function test(){ global $var=10; } </li></ul></ul></ul><ul><ul><li>Static scope </li></ul></ul><ul><ul><ul><li>Retains its value between calls to a function but local to that function </li></ul></ul></ul>
  29. 29. PHP Programming fundamentals <ul><li>Syntax definitions </li></ul><ul><li>Operators </li></ul><ul><li>Display contents </li></ul><ul><li>Control structures </li></ul><ul><li>Looping statements </li></ul><ul><li>Function </li></ul><ul><li>Array </li></ul>
  30. 30. 3.2 Operators <ul><li> </li></ul><ul><ul><li>String incrementation </li></ul></ul>
  31. 31. 3.3 Display data <ul><li>print(''<h1>Test print </h1>''); </li></ul><ul><li>echo(''Test echo''); </li></ul><ul><li>Printf(''test Printf : %d marks'', 100); </li></ul><ul><ul><li>Place holder identifiers </li></ul></ul>
  32. 32. PHP Programming fundamentals <ul><li>Syntax definitions </li></ul><ul><li>Operators </li></ul><ul><li>Display contents </li></ul><ul><li>Control and Looping structures </li></ul><ul><li>Array </li></ul>
  33. 33. 3.4 Control and Looping structures <ul><li>if/else </li></ul><ul><li>switch </li></ul><ul><li>while </li></ul><ul><li>for </li></ul><ul><li>Foreach - Later </li></ul><ul><li>Refer : </li></ul><ul><ul><li> </li></ul></ul><ul><ul><li> </li></ul></ul>
  34. 34. 3.5 Arrays <ul><li>Method 1 </li></ul><ul><ul><li>$person[0] = &quot;Saman&quot;; </li></ul></ul><ul><ul><li>$person[1] = &quot;Ravi&quot;; </li></ul></ul><ul><ul><li>$person[2] = &quot;Kamal&quot;; </li></ul></ul><ul><li>Method 2 </li></ul><ul><ul><li>$method2['Country'] = &quot;Sri Lanka&quot;; </li></ul></ul><ul><ul><li>$method2['Location'] = &quot;Indian Ocean&quot;; </li></ul></ul>
  35. 35. 3.5 Arrays <ul><li>Method 3 </li></ul><ul><ul><li>$creator = array('Country' => 'Sri Lanka', </li></ul></ul><ul><li>'Location' => 'Indian Ocean') ; </li></ul><ul><li>Access an Array : </li></ul><ul><ul><li>foreach ($creator as $invention => $inventor) { </li></ul></ul><ul><ul><li>echo &quot;$inventor created the $inventionn&quot;; </li></ul></ul><ul><ul><li>} </li></ul></ul>
  36. 36. 4.0 Form handling
  37. 37. 4.0 Form Handling <ul><li>Elements of HTML forms </li></ul><ul><li>Passing values </li></ul><ul><ul><li>GET </li></ul></ul><ul><ul><li>POST </li></ul></ul>
  38. 38. 4.1 Task 1 <ul><li>< FORM NAME =&quot;input&quot; ACTION =&quot;input_test.php&quot; METHOD =&quot;post&quot;> </li></ul><ul><li>Username </li></ul><ul><li><input type=&quot;text&quot; name=&quot;user&quot; id=&quot;user&quot;><br /> </li></ul><ul><li><input type=&quot;submit&quot; value=&quot;Submit&quot;> </li></ul><ul><li>< /FORM > </li></ul>Task 1 : Write a PHP script (input_test.php) that checks whether the username is “user1” and if so it should display “Valid user” else it should give an error message saying “Invalid user”
  39. 39. 4.2 Handling form data <ul><li>POST </li></ul><ul><ul><li>$variable=$_POST['variable']; </li></ul></ul><ul><li>GET </li></ul><ul><ul><li>$variable=$_GET['variable'] </li></ul></ul>
  40. 40. 4.3 HTML Form elements <ul><li>TEXT inputs </li></ul><ul><ul><li>Text boxes </li></ul></ul><ul><ul><ul><li><input type=&quot;text&quot; size=&quot;45&quot; name=&quot;fName“ /> </li></ul></ul></ul><ul><ul><li>Password boxes </li></ul></ul><ul><ul><ul><li><input type=&quot;password&quot; name=&quot;pass&quot; id=&quot;pass&quot; /> </li></ul></ul></ul><ul><ul><li>Textareas </li></ul></ul><ul><ul><ul><li><textarea rows=&quot;3&quot; cols=&quot;4&quot;></textarea> </li></ul></ul></ul>
  41. 41. 4.3 HTML Form elements <ul><li>Single selection Input types </li></ul><ul><ul><li>Radio buttons </li></ul></ul><ul><ul><ul><li><input type=&quot;radio&quot; name=&quot;sex&quot; value=&quot;male&quot; /> Male <input type=&quot;radio&quot; name=&quot;sex&quot; value=&quot;female&quot; /> Female </li></ul></ul></ul><ul><ul><li>Pull Down List </li></ul></ul><ul><ul><ul><li><select name=&quot;pulldown&quot;> </li></ul></ul></ul><ul><ul><ul><li><option value=&quot;1&quot;>pulldown item 1</option> </li></ul></ul></ul><ul><ul><ul><li><option value=&quot;2&quot;>pulldown item 2</option> </li></ul></ul></ul><ul><ul><ul><li></select> </li></ul></ul></ul>
  42. 42. 4.3 HTML Form elements <ul><li>Multiple selection Input types </li></ul><ul><ul><li>Check boxes </li></ul></ul><ul><ul><ul><li><input type=&quot;checkbox&quot; name=&quot;bike&quot; />bike owner <br /><input type=&quot;checkbox&quot; name=&quot;car&quot; />car owner </li></ul></ul></ul><ul><ul><li>Selection lists (Menus) </li></ul></ul><ul><ul><ul><li><select name=&quot;menu&quot; size=&quot;3&quot; multiple=&quot;multiple&quot;> <option value=&quot;1&quot;>menu item 1</option> </li></ul></ul></ul><ul><ul><ul><li><option value=&quot;2&quot;>menu item 2</option> </li></ul></ul></ul><ul><ul><ul><li></select> </li></ul></ul></ul>
  43. 43. 4.3 HTML Form elements <ul><li>Built-in buttons </li></ul><ul><ul><li><input type=&quot;submit&quot; /> </li></ul></ul><ul><ul><li><input type=&quot;reset&quot; /> </li></ul></ul><ul><ul><li><input type=&quot;file&quot; /> </li></ul></ul><ul><li>Hidden field </li></ul><ul><ul><li><input type=&quot;hidden&quot; name=''somename'' value='''somevalue' /> </li></ul></ul>
  44. 44. 4.4 Task - 2 <ul><li>Task 2 : </li></ul><ul><li>Write a form to feed user profile information such as : </li></ul><ul><ul><li>Name </li></ul></ul><ul><ul><li>Age </li></ul></ul><ul><ul><li>Gender </li></ul></ul><ul><ul><li>Academic Qualification (UG / PG/ Other) – Should be able to select one </li></ul></ul><ul><ul><li>Interests (Computer Games, Reading, Writing) – Can select one or more </li></ul></ul><ul><ul><li>Short bio </li></ul></ul><ul><li>Then write a PHP script (profile_display.php) to display the input values. With the profile info, display submitted time as well. </li></ul>
  45. 45. 5.0 Some useful in-build functions
  46. 46. 5.0 Useful in-build functions <ul><li>String handling </li></ul><ul><ul><li> </li></ul></ul><ul><li>Date related functions </li></ul><ul><ul><li> </li></ul></ul>
  47. 47. 6.0 File handling
  48. 48. 6.0 File handling <ul><li>Read and write to a file </li></ul><ul><li>Task 3 </li></ul><ul><li>Integrate source files </li></ul><ul><li>File uploads </li></ul><ul><li>Task 4 </li></ul><ul><ul><li>MIME standards </li></ul></ul><ul><li>PHP File configurations </li></ul>
  49. 49. 6.1 Read and write to a text file <ul><li>Open a file : $file = fopen(&quot;users.txt&quot;, &quot;r&quot; ) </li></ul><ul><ul><li>For more modes : </li></ul></ul><ul><li>End of file identifier : feof($file) </li></ul><ul><li>Read a line : fgets($file) </li></ul><ul><li>Read a character : fgetc($file) </li></ul><ul><li>Write to a file : </li></ul><ul><ul><li>int fwrite ( resource $handle , string $string) </li></ul></ul><ul><li>Close a file : fclose($file) </li></ul>
  50. 50. 6.2 Task 3 Task 3 : Modify the profile_display.php so that the submitted content can be appended to a file
  51. 51. 6.3 Integrate source files <ul><li>include 'file.php'; </li></ul><ul><li>require 'file.php'; </li></ul><ul><li>include_once 'file.php'; </li></ul><ul><li>require_once 'file.php'; </li></ul><ul><li>Include vs Require </li></ul><ul><ul><li>'include' allows PHP to continue to process the page even if there’s an error in the included file, but 'require' will stop the execution </li></ul></ul>
  52. 52. 6.4 File uploads <ul><li>Useful functions : </li></ul><ul><ul><ul><li>Set proper enctype in the input form </li></ul></ul></ul><ul><ul><ul><li>$_FILES[&quot;file&quot;][&quot;name&quot;] - the name of the uploaded file </li></ul></ul></ul><ul><ul><ul><li>$_FILES[&quot;file&quot;][&quot;type&quot;] - the type of the uploaded file </li></ul></ul></ul><ul><ul><ul><li>$_FILES[&quot;file&quot;][&quot;size&quot;] - the size in bytes of the uploaded file </li></ul></ul></ul><ul><ul><ul><li>$_FILES[&quot;file&quot;][&quot;tmp_name&quot;] - the name of the temporary copy of the file stored on the server </li></ul></ul></ul><ul><ul><ul><li>$_FILES[&quot;file&quot;][&quot;error&quot;] - the error code resulting from the file upload </li></ul></ul></ul><ul><ul><ul><li>move_uploaded_file($_FILES[&quot;file&quot;][&quot;tmp_name&quot;], &quot;uploads/&quot; . $_FILES[&quot;file&quot;][&quot;name&quot;]) </li></ul></ul></ul>
  53. 53. 6.5 Task 4 Task 4 : Modify the profile_input.html so that users can upload their CV. Develop file_upload.php to check whether uploaded CV is in 'doc' format or 'pdf' format and the size of the CV is less than 100KB. Also save the uploaded file in a folder called uploads within your current directory
  54. 54. Hints for Task 4 <ul><li>MIME standards </li></ul><ul><ul><li>Content formats in WEB (Rfc2045, rfc2046) </li></ul></ul><ul><ul><li> </li></ul></ul><ul><li>HTML Form encoding standards </li></ul><ul><ul><li>Default : application/x-www-form-urlencoded </li></ul></ul><ul><ul><li>To upload file : multipart/form-data </li></ul></ul>
  55. 55. 6.6 PHP file Configuration directives <ul><li>Following directives control PHP updates </li></ul><ul><li>These directives can be find in php.ini </li></ul><ul><ul><li>file_uploads </li></ul></ul><ul><ul><li>upload_tmp_dir </li></ul></ul><ul><ul><li>upload_max_filesize </li></ul></ul>
  56. 56. 7.0 Database handling
  57. 57. 7.0 Database handling <ul><li>PHP Database support </li></ul><ul><li>MySQL </li></ul><ul><li>Database handing basics </li></ul><ul><li>Task 5 </li></ul><ul><li>Task 6 </li></ul><ul><ul><li>Data encryption </li></ul></ul><ul><li>Task 7 </li></ul>
  58. 58. 7.1 PHP Database support <ul><li>Has in-build functions for more than 20 databases </li></ul><ul><li>PHP-MySQL is well known pair </li></ul>
  59. 59. 7.2 MySQL - 1 <ul><li>RDBMS </li></ul><ul><ul><li>Open-Source database </li></ul></ul><ul><ul><li>Cross platform support – more than 20 </li></ul></ul><ul><ul><li>Part of well known LAMP </li></ul></ul><ul><ul><li>Used by </li></ul></ul><ul><ul><ul><li>Yahoo!, Google, Nokia, YouTube... </li></ul></ul></ul><ul><ul><li>Has lot features </li></ul></ul><ul><ul><ul><li>Replication, clustering, High-Volume support etc. </li></ul></ul></ul>
  60. 60. 7.2 MySQL - 2 <ul><li>Typically accessed through CLI </li></ul><ul><li>There are GUI implementation to access </li></ul><ul><ul><li>PhpMyAdmin </li></ul></ul><ul><ul><ul><li>FOSS </li></ul></ul></ul><ul><ul><ul><li>Web based </li></ul></ul></ul>
  61. 61. 7.3 Database handing basics -1 <ul><li>Connect to a database </li></ul><ul><ul><li>Hostname and port </li></ul></ul><ul><ul><li>Username </li></ul></ul><ul><ul><li>Password </li></ul></ul><ul><li>Select a Database </li></ul>$db_handler = mysql_connect ('hostname', 'mysql_user', 'mysql_password'); if (!$db_handler) { die('Could not connect: ' . mysql_error ()); } mysql_select_db (&quot;my_db&quot;, $db_handler);
  62. 62. 7.3 Database handing basics -2 <ul><li>Tabular data manipulation </li></ul><ul><ul><li>Retrieve data </li></ul></ul><ul><ul><li>Inside the SQL queries, all strings values must be single quoted. </li></ul></ul><ul><ul><li>Insert data </li></ul></ul><ul><ul><ul><li>Returns true / false </li></ul></ul></ul>$result = mysql_query (&quot;SELECT * FROM table&quot;); while($row = mysql_fetch_array ($result)) { echo $row['field1'] . &quot; &quot; . $row['field2']; } mysql_query (&quot;INSERT INTO Table (Field1, Field2) VALUES ('Value1', 'Value2')&quot;);
  63. 63. 7.3 Database handing basics -3 <ul><li>Tabular data manipulation </li></ul><ul><ul><li>Update database </li></ul></ul><ul><ul><ul><li>Returns true / false </li></ul></ul></ul><ul><ul><li>Delete data </li></ul></ul><ul><ul><ul><li>Returns true / false </li></ul></ul></ul>mysql_query (&quot;UPDATE Table SET field1 = '36' WHERE field2 = 'xyz' &quot;); mysql_query (&quot;DELETE FROM Table WHERE filed1=10 &quot;);
  64. 64. 7.3 Database handing basics -4 <ul><li>Following Table / Database manipulation also can be done </li></ul><ul><ul><li>CREATE Database </li></ul></ul><ul><ul><li>DROP table </li></ul></ul><ul><ul><li>DESCRIBE table etc </li></ul></ul><ul><ul><li>The prototype should be : </li></ul></ul><ul><ul><ul><li>mysql_query(“SQL statements”) </li></ul></ul></ul>
  65. 65. 7.4 Task 5 Task 5 : Create a MySQL database called Test_DB . Inside that create a Table called users according to the following instructions Fieldname datatype values id int 1 name text admin age int 23 gender varchar(10) Male qualification varchar(10) UG interests varchar(10) Reading shortbio text Nothing username varchar(20) admin password varchar(32) admin123
  66. 66. 7.5 Task 6 Task 6 : Develop authentication mechanism to your profile update system. Use the database to do the authentication. Develop a script call index.php. In that script, accept the username and password from User and do authentication. Direct authenticated users to Profile update page (profile_display.php). Others should be given with “Access Denied ” message Profile_display.php 1. Write to file 2. save CV index.php Access denied index.php 1. Login form 2. Authentication NO YES File validation script
  67. 67. 7.5 Hints for Task 6 <ul><li>PHP encryption support </li></ul><ul><li>md5 / md5_file </li></ul><ul><ul><li> </li></ul></ul><ul><ul><li> </li></ul></ul><ul><li>sha1 / sha1_file </li></ul><ul><ul><li> </li></ul></ul>
  68. 68. 7.6 Task 7 Task 7 : Alter the profile_display.php so that it displays the existing user details (including a link to CV) at the top and give a input form to add details to database at the bottom. When a new user is added, the CV should be renamed to user's username and added to uploads directory.
  69. 69. 8.0 Session handling
  70. 70. 8.0 Session handling <ul><li>Why and How? </li></ul><ul><li>Cookie </li></ul><ul><li>Session </li></ul><ul><li>Task 8 </li></ul><ul><li>PHP configuration directives for Session </li></ul>
  71. 71. 8.2 Cookie <ul><li>Create Cookie </li></ul><ul><ul><li>Prototype : setcookie(name, value, expire) </li></ul></ul><ul><ul><li>Eg : setcookie(&quot;id&quot;, &quot;123&quot;, time()+3600); </li></ul></ul><ul><ul><li>This should be before the <HTML> </li></ul></ul><ul><li>Access Cookie </li></ul><ul><ul><li>$_COOKIE['name'] </li></ul></ul><ul><li>Delete Cookie </li></ul><ul><ul><li>Set the expiration time is passed </li></ul></ul><ul><ul><li>setcookie(&quot;id&quot;, &quot;&quot;, time()-1000) </li></ul></ul>
  72. 72. 8.3 Session <ul><li>Start session </li></ul><ul><ul><li>session_start(); </li></ul></ul><ul><ul><li>This must be the very 1 st statement of a script </li></ul></ul><ul><li>Access session </li></ul><ul><ul><li>$_SESSION['sessionName'] </li></ul></ul><ul><li>Destroy a session variable </li></ul><ul><ul><li>unset($_SESSION['sessionName']) </li></ul></ul><ul><li>Destroy whole session </li></ul><ul><ul><li>session_destroy(); </li></ul></ul>
  73. 73. 8.4 Task 8 Task 8 : Develop a page, change_password.php, to facilitate user to change the password Make a link in your profile page for change_password.php Profile_display.php 1. Manage Profiles index.php Access denied index.php 1. Login form 2. Authentication NO YES change_password.php Change password
  74. 74. 8.5 PHP Configuration directives <ul><li>session.save_handler </li></ul><ul><li>session.cookie_path </li></ul><ul><li>session.auto_start </li></ul><ul><li>more.. </li></ul>
  75. 75. 9.0 OOP using PHP
  76. 76. 9.1 PHP- OOP basics - 1 <ul><li> </li></ul><ul><li>Class </li></ul><ul><li>class Employee{ </li></ul><ul><li>// </li></ul><ul><li>} </li></ul><ul><li>Object </li></ul><ul><li>$newEmp= new Employee(); </li></ul>
  77. 77. 9.1 PHP- OOP basics - 2 <ul><li>Fields / Attributes </li></ul><ul><li>class Employee{ </li></ul><ul><li>Public $name=”Sara”; </li></ul><ul><li>} </li></ul><ul><li>Invoke attributes </li></ul><ul><li>$newEmp= new Employee(); </li></ul><ul><li>Echo $newEmp->name; # no $ for field Name </li></ul><ul><li>Scopes </li></ul><ul><ul><li>public, private, protected, final and static </li></ul></ul>
  78. 78. 9.1 PHP- OOP basics - 3 <ul><li>Methods </li></ul><ul><ul><li>public, protected, private, abstract, final </li></ul></ul><ul><li>Class Employee </li></ul><ul><li>{ </li></ul><ul><li>Public function greetEmp() </li></ul><ul><li>{ </li></ul><ul><li>echo “Hello”; </li></ul><ul><li>} </li></ul><ul><li>Function sayGoodbye() </li></ul><ul><li>{ </li></ul><ul><li>Echo “Goof bye”; </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>Method invocation : Employee::greetEmp(); $emp=new Employee(); $emp->sayGoodbye();
  79. 79. 9.1 PHP- OOP basics - 4 <ul><li>Constructor - Destructor </li></ul><ul><li>Class Employee </li></ul><ul><li>{ </li></ul><ul><li>function _construct([arg1, arg2...]) </li></ul><ul><li>{ </li></ul><ul><li>} </li></ul><ul><li>Function _destruct() </li></ul><ul><li>{ </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
  80. 80. 9.1 PHP- OOP basics - 5 <ul><li>Static members </li></ul><ul><li>class Employee </li></ul><ul><li>{ </li></ul><ul><li>Private static $emps=0; </li></ul><ul><li>Function _construct(){ </li></ul><ul><li>self:: $emps++; </li></ul><ul><li>} </li></ul><ul><li>Static function getEmp(){ </li></ul><ul><li>Return self ::$emps; </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>Calls : $employee1=new Employee(); Echo Employee::getEmp(); $employee2=new Employee(); Echo Employee::getEmp();
  81. 81. 9.1 PHP- OOP basics - 6 <ul><li>Object cloning </li></ul><ul><ul><li>By nature objects are treated as references rather values </li></ul></ul><ul><li>Class Employee{ </li></ul><ul><li>Public $service=10; </li></ul><ul><li>} </li></ul><ul><li>$emp=new Employee(); </li></ul><ul><li>$emp2 = clone $emp; </li></ul><ul><li>$emp2->service=30; </li></ul><ul><li>Echo $emp->service.'' - ''.$emp2->service </li></ul>
  82. 82. 10.0 PHP runtime Configuration of directives
  83. 83. 10.0 PHP run time Configuration directives <ul><li>ini_get (string $varname) </li></ul><ul><li>ini_set(string $varname, string $newvalue) </li></ul><ul><li>ini_restore (string $varname) </li></ul><ul><li>Eg : </li></ul><ul><ul><li><?php </li></ul></ul><ul><ul><ul><li>ini_set(&quot;date.timezone&quot;,&quot;Asia/Colombo&quot;); </li></ul></ul></ul><ul><ul><ul><li>echo date('D,F j, Y, H:i:s A'); </li></ul></ul></ul><ul><ul><li>?> </li></ul></ul>
  84. 84. 11.0 PHP Libraries
  85. 85. 11.0 PHP Libraries <ul><li>In-build Libraries </li></ul><ul><ul><li>11.1 PEAR </li></ul></ul><ul><ul><li>11.2 PDO </li></ul></ul><ul><li>3 rd party Libraries </li></ul><ul><ul><li>11.3 PHP Classes </li></ul></ul>
  86. 86. 11.1 PEAR <ul><li>Bundled with PHP 4.3.0 or later releases </li></ul><ul><li> </li></ul><?php require_once &quot;DB.php&quot;; $DB = DB::connect(&quot;mysql://user:root123@localhost/testdb&quot;); if (DB::isError($DB)) echo &quot;Cannot connect to database: &quot; . $DB->getMessage(); else { $Query = &quot;SELECT * FROM users&quot;; $Result = $DB->query($Query); echo $NumResults = $Result->numRows(); } ?>
  87. 87. 11.2 PDO <ul><li>Database abstraction layer </li></ul><ul><li>Bundled with PHP 5.1 or later releases </li></ul><ul><li>try { </li></ul><ul><li>$dbh = new PDO('mysql:host=localhost;testdb=test', “user”, “user123”); </li></ul><ul><ul><li>foreach($dbh->query('SELECT * from users') as $row) { </li></ul></ul><ul><li>print_r($row); </li></ul><ul><li>} </li></ul><ul><li>$dbh = null; </li></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>catch (PDOException $e) { </li></ul></ul><ul><li> print &quot;Error!: &quot; . $e->getMessage() . &quot;<br/>&quot;; die(); </li></ul><ul><ul><li>} </li></ul></ul>
  88. 88. 12.0 File System and OS Functions
  89. 89. 12.0 File System and OS Functions <ul><li>Access file system </li></ul><ul><ul><li>fileperms ( string $filename ) </li></ul></ul><ul><ul><li>is_writable ( string $filename ) </li></ul></ul><ul><ul><li>more... </li></ul></ul><ul><ul><li> </li></ul></ul><ul><li>Execute Systems commands </li></ul><ul><ul><li>Exec(String $command) </li></ul></ul><ul><ul><li>System (String $command) </li></ul></ul><ul><ul><li>Shell_exec(String Command) </li></ul></ul><ul><ul><li> </li></ul></ul>
  90. 90. 13.0 Frameworks
  91. 91. 13.0 PHP Frameworks
  92. 92. 14.0 PHP Security
  93. 93. 14.0 PHP Security <ul><li>All the Web related treats should be handled </li></ul><ul><li>Some PHP directives </li></ul><ul><ul><li>magic_quotes_gpc </li></ul></ul><ul><ul><li>register_globals </li></ul></ul><ul><ul><li>Turn off unnecessary extensions </li></ul></ul><ul><ul><li>Execution time </li></ul></ul><ul><li>Some In-build Functions </li></ul><ul><ul><li>htmlentities </li></ul></ul><ul><ul><li>addslashes </li></ul></ul>
  94. 94. 15.0 PHP IDEs
  95. 95. 15.0 PHP IDEs <ul><li>Eclipse </li></ul><ul><li>Komodo** </li></ul><ul><li>PHP Designer* </li></ul><ul><li>PhpED** </li></ul><ul><li>PHPEdit** </li></ul><ul><li>Zend Studio** </li></ul><ul><li> </li></ul>
  96. 96. 16.0 PHP Systems
  97. 97. 16.0 PHP systems <ul><li>Configure PHP based systems </li></ul><ul><ul><li>Edit configuration files </li></ul></ul>
  98. 98. 17.0 Discussions
  99. 99. Thank you ! K.Sarveswaran,