Published on

1 Comment
1 Like
    Are you sure you want to  Yes  No
    Your message goes here
No Downloads
Total Views
On Slideshare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide


  1. 1. PHP & MySQL CSI4118 Fall 2005
  2. 2. Introduction to PHP Basic principles and syntax
  3. 3. <ul><li>PHP == ‘Hypertext Preprocessor’ </li></ul><ul><li>Open-source, server-side scripting language </li></ul><ul><li>Used to generate dynamic web-pages </li></ul><ul><li>PHP scripts reside between reserved PHP tags </li></ul><ul><ul><li>This allows the programmer to embed PHP scripts within HTML pages </li></ul></ul>What is PHP?
  4. 4. What is PHP (cont’d) <ul><li>Interpreted language, scripts are parsed at run-time rather than compiled beforehand </li></ul><ul><li>Executed on the server-side </li></ul><ul><li>Source-code not visible by client </li></ul><ul><ul><li>‘ View Source’ in browsers does not display the PHP code </li></ul></ul><ul><li>Plethora of built-in functions allow for fast development </li></ul><ul><li>Compatible with many popular databases </li></ul>What is PHP (Cont’d)
  5. 5. What does PHP code look like? <ul><li>Structurally similar to C/C++ </li></ul><ul><li>Supports procedural and object-oriented paradigm (to some degree) </li></ul><ul><li>All PHP statements end with a semi-colon </li></ul><ul><li>Each PHP script must be enclosed in the reserved PHP tag </li></ul><?php … ?>
  6. 6. Comments in PHP <ul><li>Standard C, C++, and shell comment symbols </li></ul>// C++ and Java-style comment # Shell-style comments /* C-style comments These can span multiple lines */
  7. 7. Variables in PHP <ul><li>PHP variables must begin with a “$” sign </li></ul><ul><li>Case-sensitive ($Foo != $foo != $fOo) </li></ul><ul><li>Global and locally-scoped variables </li></ul><ul><ul><li>Global variables can be used anywher </li></ul></ul><ul><ul><li>Local variables restricted to a function or class </li></ul></ul><ul><li>Certain variable names reserved by PHP </li></ul><ul><ul><li>Form variables ($_POST, $_GET) </li></ul></ul><ul><ul><li>Server variables ($_SERVER) </li></ul></ul><ul><ul><li>Etc. </li></ul></ul>
  8. 8. Variable usage <?php $foo = 25; // Numerical variable $bar = “Hello”; // String variable $foo = ($foo * 7); // Multiplies foo by 7 $bar = ($bar * 7); // Invalid expression ?>
  9. 9. Echo <ul><li>The PHP command ‘ echo ’ is used to output the parameters passed to it </li></ul><ul><ul><li>The typical usage for this is to send data to the client’s web-browser </li></ul></ul><ul><li>Syntax </li></ul><ul><ul><li>void echo (string arg1 [, string argn...]) </li></ul></ul><ul><ul><li>In practice, arguments are not passed in parentheses since echo is a language construct rather than an actual function </li></ul></ul>
  10. 10. Echo example <ul><li>Notice how echo ‘5x5=$foo’ outputs $foo rather than replacing it with 25 </li></ul><ul><li>Strings in single quotes (‘ ‘) are not interpreted or evaluated by PHP </li></ul><ul><li>This is true for both variables and character escape-sequences (such as “ ” or “) </li></ul><?php $foo = 25; // Numerical variable $bar = “Hello”; // String variable echo $bar; // Outputs Hello echo $foo,$bar; // Outputs 25Hello echo “5x5=“,$foo; // Outputs 5x5=25 echo “5x5=$foo”; // Outputs 5x5=25 echo ‘5x5=$foo’; // Outputs 5x5=$foo ?>
  11. 11. Functions <ul><li>Functions MUST be defined before then can be called </li></ul><ul><li>Function headers are of the format </li></ul><ul><ul><li>Note that no return type is specified </li></ul></ul><ul><li>Unlike variables, function names are not case sensitive (foo(…) == Foo(…) == FoO(…)) </li></ul>function functionName($arg_1, $arg_2, …, $arg_n)
  12. 12. Functions example <ul><li><?php </li></ul><ul><li>// This is a function </li></ul><ul><ul><li>function foo($arg_1, $arg_2) </li></ul></ul><ul><ul><li>{ $arg_2 = $arg_1 * $arg_2; </li></ul></ul><ul><ul><li>  return $arg_2; } </li></ul></ul><ul><ul><li>$result_1 = foo(12, 3); // Store the function </li></ul></ul><ul><ul><li>echo $result_1; // Outputs 36 </li></ul></ul><ul><ul><li>echo foo(12, 3); // Outputs 36 </li></ul></ul><ul><li>?> </li></ul>
  13. 13. The Big Picture for Assignment 2 <ul><li>Learn about web-servers </li></ul><ul><li>Learn about Apache </li></ul><ul><ul><li>Download / Installation </li></ul></ul><ul><li>Learn about PHP </li></ul><ul><ul><li>Download / Installation </li></ul></ul><ul><li>Configure Apache </li></ul><ul><ul><li>Modify and save the configuration file, then restart </li></ul></ul><ul><li>Learn about file I/O without the aid of SQL </li></ul><ul><li>Design, write, and test scripts to solve Assignment 2 </li></ul>
  14. 14. Saving Data in Text Files <ul><li>PHP has built in functions for File I/O processing </li></ul><ul><ul><li>fopen (..), fwrite(..), fclose(..), fflush(..), file_get_contents(..) </li></ul></ul><ul><li>Using these pre-made functions, File I/O in PHP is similar to that of C </li></ul><ul><li>General Flow: </li></ul><ul><ul><li>Open file </li></ul></ul><ul><ul><li>Read data </li></ul></ul><ul><ul><li>Modify data </li></ul></ul><ul><ul><li>Write data </li></ul></ul><ul><ul><li>Close file </li></ul></ul>
  15. 15. Saving Data in Text Files <ul><li>Reading from a file </li></ul><ul><li><?php     $file = fopen ( &quot;sample.txt&quot; , &quot;r&quot; );     while (! feof ( $file )) {         echo fgets ( $file ), &quot;<BR>&quot; ;     } ?> </li></ul><ul><li>Reading from a URL </li></ul><ul><li><?php $file = fopen ( &quot;; , &quot;r&quot; ); ?> </li></ul><ul><li>Writing to a file </li></ul><ul><li><?php     $file = fopen ( &quot;agent.log&quot; , &quot;a&quot; );      fputs ( $file , $HTTP_USER_AGENT . &quot; &quot; ); ?> </li></ul>
  16. 16. Saving Data in Text Files <ul><li>Reading from a file </li></ul><ul><li><?php     $file = fopen ( &quot;sample.txt&quot; , &quot;r&quot; );     while (! feof ( $file )) {         echo fgets ( $file ), &quot;<BR>&quot; ;     } ?> </li></ul><ul><li>Reading from a URL </li></ul><ul><li><?php $file = fopen ( &quot;; , &quot;r&quot; ); ?> </li></ul><ul><li>Writing to a file </li></ul><ul><li><?php     $file = fopen ( &quot;agent.log&quot; , &quot;a&quot; );      fputs ( $file , $HTTP_USER_AGENT . &quot; &quot; ); ?> </li></ul>
  17. 17. Saving Data in Text Files <ul><li>Reading a File directly into Array or String </li></ul><ul><li>The file() function reads entire file into an array: array file (string filename [, int use_include_path]) Each element of the array corresponds to a line in the file, with the newline still attached. You can use the optional second parameter and set it to &quot;1&quot;, if you want to search for the file in the include_path, too. </li></ul><ul><li>Example (read a web page into an array and print it out): </li></ul><ul><li>$fcontents = file ('http://localhost'); </li></ul><ul><li>while (list ($line_num, $line) = each ($fcontents)) </li></ul><ul><li>{ echo &quot;Line $line_num: &quot; . htmlspecialchars ($line) . &quot; &quot;; } </li></ul><ul><li>Example (read a web page into an array and join it to a string): </li></ul><ul><li>$fcontents = join ('', file ('http://localhost')); </li></ul>
  18. 18. Saving Data in Text Files: Example <?php $filename = 'test.txt'; /* Filename for writing. This is assumed to be in the same directory as the script */ $somecontent = &quot;Add this to the file &quot;; // String to append to the file // Let's make sure the file exists and is writable first. if (is_writable($filename)) { // Open the file in append mode ( ‘a’ ) so that the string being stored is written at the end of the // file rather than replacing the existing text    if (!$handle = fopen($filename, 'a')) {          echo &quot;Cannot open file ($filename)&quot;;          exit;    }    // Write $somecontent to the opened file.    if (fwrite($handle, $somecontent) === FALSE) {        echo &quot;Cannot write to file ($filename)&quot;;        exit;    }      echo &quot;Success, wrote ($somecontent) to file ($filename)&quot;;       fclose($handle); } else {    echo &quot;The file $filename is not writable&quot;; } ?>
  19. 19. For more information… <ul><li> </li></ul><ul><li> </li></ul><ul><li>http://www. zend .com/ zend / tut / </li></ul>
  20. 20. Various Notes on PHP Several useful tidbits
  21. 21. Header <ul><li>The PHP function header(string) is used to send a raw HTTP header to the browser </li></ul><ul><li>Most useful for redirecting the browser to another page (after a successful login, for example) </li></ul><?php // string storing the destination $url = http://www. uottawa .ca /* Sends a header telling the browser to navigate to $url */ header(“Location: “ . $url); ?>
  22. 22. IMPORTANT NOTE! It is absolutely vital that any calls to header(…) must be made before any other output is sent to the browser (either by standard HTML tags, HTML comments, PHP’s echo command, or even blank lines. Failure to ensure this will result in an error when your PHP script runs
  23. 23. Preventing Browser-caching <ul><li>The header(…) function can also be used to prevent the browser from caching the data sent by PHP. This forces the browser to refresh the page everytime the page is loaded. </li></ul>
  24. 24. Anti-caching example <?php // Date in the past header(&quot;Expires: Mon, 26 Jul 1997 05:00:00 GMT&quot;); // always modified header(&quot;Last-Modified: &quot; . gmdate(&quot;D, d M Y H:i:s&quot;) . &quot; GMT&quot;);   // HTTP/1.1 header(&quot;Cache-Control: no-store, no-cache, must-revalidate&quot;); header(&quot;Cache-Control: post-check=0, pre-check=0&quot;, false); // HTTP/1.0 header(&quot;Pragma: no-cache&quot;); ?>
  25. 25. Anti-caching example IE 6 Fix: when you click your back button to make changes in the form, you have to click the REFRESH button on that page to get the information that you posted back into the form. This only works about 50% of the time, the other 50% the users information is lost and they have to type it over again. Not a good thing if you are trying to get the person to enter their billing information to process an order. They might just get irritated and leave. So, here's a solution for that. Enter this right below the session_start() of each script (yes it still must be before anything is output to the browser). Solution: PHP Example: header ( &quot;Cache-control: private&quot; );
  26. 26. Anti-caching example <?php // start the session session_start(); header(&quot;Cache-control: private&quot;); //IE 6 Fix echo &quot;<strong>Step 2 - Register Session </strong><br />&quot;; // Get the user's input from the form    $name = $_POST['name']; // Register session key with the value    $_SESSION['name'] = $name; // Display the sssion information: ?> Welcome to my website <strong><? echo $_SESSION['name']; ?></strong>!<br /> Let's see what happens on the <a href=&quot;page3.php&quot;>next page.</a><br /><br />
  27. 27. MD5 Hashing <ul><li>The PHP Function md5(string) takes a string parameter and returns the md5 hash of that string (a 32-bit hexadecimal number) </li></ul><ul><li>This function does not encrypt the data sent to it, it merely returns a hash-value for that data. </li></ul><?php $password = ‘tomatePotate'; if (md5($password) === /*MD5 of password stored in file*/) { echo “The passwords match!” } ?>
  28. 28. Resources <ul><li>PHP function header(…) </li></ul><ul><li>PHP function md5(…) </li></ul><ul><li> </li></ul><ul><li>RSA MD5 Message Digest Algorithm http://www. faqs .org/ rfcs /rfc1321 </li></ul>
  29. 29. PHP the [Easy] way A simple way to configure PHP and Apache for Assignment 2
  30. 30. Configuration woes… <ul><li>For various reasons, manually configuring PHP to work with Apache can present a great deal of trouble </li></ul><ul><li>There exists an elegant software package which is designed to abstract the configuration process from web-developers </li></ul>
  31. 31. [Easy]PHP <ul><li>http://www. easyphp .org/ </li></ul><ul><li>Automagically installs and configures Apache, PHP, and MySQL </li></ul><ul><li>Defaults to running from http:// localhost / </li></ul><ul><li>Installer: </li></ul><ul><ul><li> </li></ul></ul><ul><li>English Readme files (Extract to EasyPHP Installation Directory): </li></ul><ul><ul><li> </li></ul></ul>
  32. 32. Benefits of Web Applications <ul><li>Free Infrastructure: A major benefit is that the whole infrastructure is already in place and well developed. Only a web browser is needed. The new applications can be available to all the relevant users immediately – IT support staff do not have to go to each client workstation to install the Client Software </li></ul><ul><li>Free Upgrades: As the application resides on the server, new versions will be immediately and simultaneously available to every user. There is no need to distribute updated application files to every user. </li></ul><ul><li>Interchangeable components: It is possible to exchange either the server or the browser without breaking the application. </li></ul>
  33. 33. Web Technolgies <ul><li>Client-side </li></ul><ul><ul><li>HTML (HyperText Markup Language) </li></ul></ul><ul><ul><li>CSS (Cascading Style Sheets) </li></ul></ul><ul><ul><li>XML (Extensible Markup Language) </li></ul></ul><ul><ul><li>DTD (Document Type Declaration) </li></ul></ul><ul><ul><li>XSLT (Extensible Style Sheet Language Translator) </li></ul></ul><ul><ul><li>JavaScript </li></ul></ul><ul><ul><li>VBScript/Jscript </li></ul></ul><ul><ul><li>Animation (Flash ) </li></ul></ul><ul><ul><li>Dynamic Hypertext Markup Language (DHTML – HTML, JavaScript, CSS & Document Object Model (DOM) </li></ul></ul>
  34. 34. <ul><li>Server-side </li></ul><ul><ul><li>PHP </li></ul></ul><ul><ul><li>ASP (Active Server Pages) & ASP.NE </li></ul></ul><ul><ul><li>JSP (Java Server Pages) </li></ul></ul><ul><ul><li>Java Servlets </li></ul></ul><ul><ul><li>C++/Java </li></ul></ul><ul><ul><li>PERL </li></ul></ul><ul><ul><li>XML </li></ul></ul><ul><li>Database </li></ul><ul><ul><li>ODBC </li></ul></ul><ul><ul><li>JDBC </li></ul></ul><ul><ul><li>OleDB </li></ul></ul>Web Technolgies
  35. 35. <ul><li> </li></ul><ul><li>Software in a community that is: </li></ul><ul><ul><li>Freely Used (no warranty, no limits on usage) </li></ul></ul><ul><ul><li>Source code is available for any modifications </li></ul></ul><ul><ul><li>Freely Extended (must share source, represent original works and owners) </li></ul></ul><ul><ul><li>License is not specific to a product or restrict other software and also technology neutral. </li></ul></ul><ul><li>There's always plenty of professional and peer support from documentation and mailing lists. </li></ul><ul><li>Runs on any Platform. Bugs are fixed rapidly, and requests for features are always heard, evaluated, and if feasible, implemented. </li></ul>Open Source Software
  36. 36. <ul><li>Linux ( www. linux .com ) </li></ul><ul><ul><li>*nix flavour that is all the rage at the moment. </li></ul></ul><ul><ul><li>Similar to Unix but free and runs on just about anything. </li></ul></ul><ul><ul><li>Latest version: 9.2 </li></ul></ul><ul><li>Apache ( ) </li></ul><ul><ul><li>Most popular web page serving software </li></ul></ul><ul><ul><li>Latest version: 2.0 </li></ul></ul><ul><li>MySQL ( www. mysql .com ) </li></ul><ul><ul><li>Open source SQL database that is free and extremely powerful </li></ul></ul><ul><ul><li>Latest version: 4.1 (though 5.0.1 is testing version) </li></ul></ul><ul><li>PHP ( ) </li></ul><ul><ul><li>PHP ( Hypertext Preprocessor) is mainly focused on server-side scripting, so you can do anything any other CGI program can do, such as collect form data, generate dynamic page content, or send and receive cookies. But PHP can do much more. </li></ul></ul><ul><ul><li>Latest version: 4.3 (though 5 is testing) </li></ul></ul>LAMP <ul><li>Free </li></ul><ul><li>Coordinated </li></ul><ul><li>Cross-platform </li></ul><ul><li>Plenty of support </li></ul><ul><li> </li></ul>
  37. 37. Software Usage
  38. 38. <ul><li>One of the most powerful development models for the Web has been the notion of “AMP.” </li></ul><ul><li>AMP stands for “ A pache / M ySQL / P HP & Perl working together.” </li></ul>AMP <ul><ul><li>Apache is the industry-leading web server that runs a majority of web servers on the Internet. </li></ul></ul><ul><ul><li>MySQL is a very popular database that runs on most operating systems. </li></ul></ul><ul><ul><li>PHP / Perl is a general purpose scripting environment widely used for building dynamic web sites. </li></ul></ul>Together, they form the nucleus of a web application system. -> contains the software needed for all three packages. You can install and configure very easily. I am showing the installation separately
  39. 39. Apache, MySQL and PHP (AMP) Integration User User
  40. 40. Apache - Benefits <ul><li>Apache is well supported - Most support for Apache is free and available 24 hours a day via Internet mail or newsgroups. </li></ul><ul><li>Apache is multi-platform - Apache can run on virtually any hardware platform (from PCs to mainframes), and almost any operating system, such as Linux, Windows, NetWare, Macintosh, xBSD, etc. </li></ul><ul><li>Apache is secure - security holes are rare but when they exist they are discovered and fixed quickly </li></ul><ul><li>Apache is extensible - anyone can write modules that easily plug in to Apache. If Apache doesn't do what you want or need it to do, anyone with programming skills can write the modules you need. </li></ul><ul><li>Apache is database-friendly - you can interface Apache with virtually any commercial database, such as Oracle, Sybase, DB2, and Informix, as well as free databases such as MySQL and Postgres. </li></ul><ul><li>Apache is hardware-friendly - Apache generally consumes far fewer hardware resources that commercial web servers. </li></ul><ul><li>No Microsoft Viruses - Apache is immune to the Code Red, Nimda, and other viruses that target at Microsoft Web servers. </li></ul>
  41. 41. Installation – Apache (ver 2.0.52) <ul><li>Go to </li></ul><ul><li>If you are downloading the Win32 distribution, please read these important notes at the website </li></ul><ul><li>Download the binary version </li></ul><ul><ul><li>Win32 Binary (MSI Installer): apache_2.0.52-win32-x86-no_ssl.msi </li></ul></ul>
  42. 42. <ul><li>Move to the folder where the Apache is installed and double click on the file to start the installation. </li></ul><ul><li>Welcome screen - Press Next to continue </li></ul><ul><li>Apache license – Accept the terms and Press Next </li></ul><ul><li>Brief intro – Read and Press Next </li></ul><ul><li>Server Information – Enter admin user’s email </li></ul><ul><li>Setup Type – Typical </li></ul><ul><li>Destination Folder : leave to the default and press Next </li></ul><ul><li>Wait for installation to complete </li></ul><ul><li>You can change the settings in httpd.conf file in conf folder </li></ul>Installation – Apache (ver 2.0.52)
  43. 43. <ul><li>Find the port address </li></ul><ul><ul><li>Open httpd.conf and find the port number : 80, 8080 or 8088 (on this machine)  c:program filesApachegroupApache2conf folder </li></ul></ul><ul><li>Starting Apache service </li></ul><ul><ul><li>C:Program FilesApacheGroupApache2inApacheMonitor.exe. You will see at the task bar </li></ul></ul><ul><ul><li>Open the monitor and start the apache service if it is not done already </li></ul></ul><ul><li>Testing the Apache web server </li></ul><ul><ul><li>http://localhost:8088/  You may have a different port number </li></ul></ul><ul><ul><li>you will see the test web page. </li></ul></ul>Starting Apache
  44. 44. <ul><li>Edit and save httpd.conf file: </li></ul><ul><ul><li>Alter Options Indexes FollowSymLinks into </li></ul></ul><ul><ul><li>Options -Indexes FollowSymLinks </li></ul></ul><ul><li>Reboot Apache (not computer) </li></ul>Configuration - Apache
  45. 45. MySQL - Benefits <ul><li>In 1996 T.c.X. DataKonsultAB , a consulting firm in Sweden developed MySQL. </li></ul><ul><li>The largest growing relational database out on the market as it can handle large databases that can be accessed over the Web - Meets the ANSI SQL92 regulations (SQL-Structured Query Language) </li></ul><ul><li>Mainly runs on UNIX-based environments, but also used on windows </li></ul><ul><li>One of the most used open source databases in the world. </li></ul><ul><li>Capacity to handle 50,000,000+ records. </li></ul><ul><li>Very fast command execution, perhaps the fastest to be found on the market. </li></ul><ul><li>Flexible and secure password system to protect your data - powerful security system </li></ul><ul><li>Fast, reliable, easy to use, and affordable! </li></ul><ul><li>On-line help facility - (type –help or -?,) </li></ul><ul><li>Comes with a source code </li></ul><ul><li>Multi-User and works on Several Platforms </li></ul>
  46. 46. Installation - MySQL <ul><li>Go to and move to Windows (x86)5.0.0a-alpha26.7M </li></ul><ul><li>Click Download </li></ul><ul><li>Create folder called mysql and Unzip the file </li></ul><ul><li>Execute setup.exe </li></ul><ul><li>Welcome screen – Press Next to continue </li></ul><ul><li>Information: Note down and press Next </li></ul><ul><li>Destination Folder: Default (c:mysql) </li></ul><ul><li>Wait for installation to complete - Press Finish </li></ul>
  47. 47. Starting MySQL <ul><li>Go to C:mysqlin </li></ul><ul><li>Execute winmysqladmin.exe . You will be prompted for username and password. Type as admin with password as admin </li></ul><ul><li>You will see the Traffic light signal on the task bar and also the admin window </li></ul>
  48. 48. Configuration - MySQL
  49. 49. MySQL Control Center <ul><li>Graphical administrative interface for MySQL database(s) </li></ul><ul><li>Can administer several DB servers that are hosted on different machines </li></ul><ul><li>Download from </li></ul><ul><li>Execute the file </li></ul>
  50. 50. <ul><li>Open program in programs menu </li></ul><ul><li>Establish connection </li></ul><ul><li>to DB server </li></ul>MySQL Control Center - Configuration
  51. 51. <ul><li>in Databases ignore mysql; delete test </li></ul><ul><li>in User Administration delete </li></ul><ul><li>first two users and assign </li></ul><ul><li>password to root users </li></ul><ul><li>ignore Server Administration </li></ul><ul><li>adapt connection (password) </li></ul>MySQL Control Center - Configuration
  52. 52. PHP - Benefits <ul><li>Easy, powerful, popular </li></ul><ul><li>Server-side scripting language </li></ul><ul><li>Supports many DB’s (not only MySQL) </li></ul><ul><li>Platform Independent </li></ul><ul><li>Web Server Independent </li></ul><ul><li>Free and Open Source </li></ul><ul><li>PHP Overtakes Microsoft ASP as the Web’s Number 1 server side Web technology for the Internet. </li></ul><ul><li>An April Netcraft surveys indicate 24 percent of the 37.6 million websites, or are running PHP scripts. PHP adoption is growing by 6.5 percent each month. (9 million sites) </li></ul>
  53. 53. Installation - PHP <ul><li>Download php 5.0 (binaries) from </li></ul><ul><li>Unzip the folder to c:php-5.0.2-Win32 </li></ul>
  54. 54. Configuration- PHP <ul><li>Move to c:php-5.0.2-Win32 </li></ul><ul><li>Rename php.ini-dist file to php.ini </li></ul><ul><li>Copy php.ini to C:Windows </li></ul><ul><li>Copy php5ts.dll to C:WindowsSYSTEM </li></ul><ul><li>Go to Start-> All Programs -> HTTP Apache Server 2.0.52-> Configure HTTP Server </li></ul><ul><li>-> Edit the Apache config.httpd config file </li></ul><ul><li>Look for a section that contains a number of LoadModule directives (from line number 131) as </li></ul><ul><li># LoadModule foo_module modules/ </li></ul><ul><li># </li></ul>
  55. 55. <ul><li>Add the line at the end as </li></ul><ul><li>LoadModule php5_module &quot;c:/php-5.0.2-Win32/php5apache2.dll“ </li></ul><ul><li>Search for AddType and insert the following lines </li></ul><ul><ul><ul><ul><ul><li>#AddType application/x-tar .tgz </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><ul><li>AddType application/x-httpd-php .php </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><ul><li>AddType application/x-httpd-php .phtml .php </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><ul><li>AddType application/x-httpd-source .phps </li></ul></ul></ul></ul></ul><ul><li>Configure the path to php.ini </li></ul><ul><ul><li>PHPIniDir &quot;C:/php-5.0.2-Win32“ </li></ul></ul><ul><ul><li>Start Apache Server </li></ul></ul>Installation & Configuration- PHP
  56. 56. Test PHP and Apache <ul><li>Test the Apache Server as </li></ul><ul><li>Create php folder in D:SoftwareInstallationApacheGroupApache2htdocs or where you have installed Apache </li></ul><ul><li>Create first.php in htdocsphp folder (D:SoftwareInstallationApacheGroupApache2htdocsphp) with the following lines: </li></ul><ul><ul><li><?php </li></ul></ul><ul><ul><li> phpinfo(); </li></ul></ul><ul><ul><li>?> </li></ul></ul><ul><li>Open the browser and type the following link </li></ul><ul><li>http://localhost:8088/php/first.php </li></ul><ul><li>the port number 8088 may be different in your installation. You will be prompted the php information on the browser. </li></ul>
  57. 57. How it works
  58. 58. PHP Overview <ul><li>Easy learning curve </li></ul><ul><li>Syntax Perl- and C-like syntax. Relatively easy to learn. </li></ul><ul><li>Large function library </li></ul><ul><li>Embedded directly into HTML </li></ul><ul><li>Interpreted, no need to compile </li></ul><ul><li>Open Source server-side scripting language designed specifically for the web. </li></ul><ul><li>Conceived in 1994, now used on +10 million web sites. </li></ul><ul><li>Outputs not only HTML but can output XML, images (JPG & PNG), PDF files and even Flash movies all generated on the fly. Can write these files to the filesystem. </li></ul><ul><li>Supports a wide-range of databases (20 + ODBC). </li></ul><ul><li>PHP also has support for talking to other services using protocols such as LDAP, IMAP, SNMP, NNTP, POP3, HTTP. </li></ul><ul><li>Refer to php manual </li></ul><ul><ul><li> </li></ul></ul>
  59. 59. <ul><li>Save as sample.php in htdocs: </li></ul><ul><ul><li><!– sample.php --> </li></ul></ul><ul><ul><li><html><body> </li></ul></ul><ul><ul><ul><li><strong>Hello World!</strong><br /> </li></ul></ul></ul><ul><ul><ul><li><?php </li></ul></ul></ul><ul><li>echo “<h2>Hello, World</h2>”; ?> </li></ul><ul><ul><ul><li><?php </li></ul></ul></ul><ul><ul><li>$myvar = &quot;Hello World&quot;; </li></ul></ul><ul><ul><li>echo $myvar; </li></ul></ul><ul><ul><li> ?> </li></ul></ul><ul><ul><li></body></html> </li></ul></ul><ul><li>Browser: http ://localhost:8088/php/sample.php </li></ul>First PHP script
  60. 60. PHP Control Structures <ul><li>Control Structures: Are the structures within a language that allow us to control the flow of execution through a program or script. </li></ul><ul><li>Grouped into conditional (branching) structures (e.g. if/else) and repetition structures (e.g. while loops). </li></ul><ul><li>Example if/else if/else statement: </li></ul><ul><ul><ul><li>if ($foo == 0) { </li></ul></ul></ul><ul><ul><ul><li>echo ‘The variable foo is equal to 0’; </li></ul></ul></ul><ul><ul><ul><li>} </li></ul></ul></ul><ul><ul><ul><li>else if (($foo > 0) && ($foo <= 5)) { </li></ul></ul></ul><ul><ul><ul><li> echo ‘The variable foo is between 1 and 5’; </li></ul></ul></ul><ul><ul><ul><li>} </li></ul></ul></ul><ul><ul><ul><li>else { </li></ul></ul></ul><ul><ul><ul><li> echo ‘The variable foo is equal to ‘.$foo; </li></ul></ul></ul><ul><ul><ul><li>} </li></ul></ul></ul>
  61. 61. PHP - Forms <ul><li>Access to the HTTP POST and GET data is simple in PHP </li></ul><ul><li>The global variables $_POST[] and $_GET[] contain the request data <?php </li></ul><ul><ul><li>if ($_POST[&quot;submit&quot;]) </li></ul></ul><ul><ul><li>echo &quot;<h2>You clicked Submit!</h2>&quot;; </li></ul></ul><ul><ul><li>else if ($_POST[&quot;cancel&quot;]) </li></ul></ul><ul><ul><li>echo &quot;<h2>You clicked Cancel!</h2>&quot;; </li></ul></ul><ul><ul><li>?> </li></ul></ul><ul><ul><li><form action=&quot;form.php&quot; method=&quot;post&quot;> </li></ul></ul><ul><ul><li> </li></ul></ul><ul><ul><li><input type=&quot;submit&quot; name=&quot;submit&quot; value=&quot;Submit&quot;> </li></ul></ul><ul><ul><li><input type=&quot;submit&quot; name=&quot;cancel&quot; value=&quot;Cancel&quot;> </li></ul></ul><ul><ul><li></form> </li></ul></ul>
  62. 62. WHY PHP – Sessions ? Whenever you want to create a website that allows you to store and display information about a user, determine which user groups a person belongs to, utilize permissions on your website or you just want to do something cool on your site, PHP's Sessions are vital to each of these features. Cookies are about 30% unreliable right now and it's getting worse every day. More and more web browsers are starting to come with security and privacy settings and people browsing the net these days are starting to frown upon Cookies because they store information on their local computer that they do not want stored there. PHP has a great set of functions that can achieve the same results of Cookies and more without storing information on the user's computer. PHP Sessions store the information on the web server in a location that you chose in special files. These files are connected to the user's web browser via the server and a special ID called a &quot;Session ID&quot;. This is nearly 99% flawless in operation and it is virtually invisible to the user.
  63. 63. PHP - Sessions <ul><li>Sessions store their identifier in a cookie in the client’s browser </li></ul><ul><li>Every page that uses session data must be proceeded by the session_start() function </li></ul><ul><li>Session variables are then set and retrieved by accessing the global $_SESSION[] </li></ul><ul><li>Save it as session.php <?php </li></ul><ul><ul><li>session_start(); </li></ul></ul><ul><ul><li>if (!$_SESSION[&quot;count&quot;]) </li></ul></ul><ul><ul><li>$_SESSION[&quot;count&quot;] = 0; </li></ul></ul><ul><ul><li>if ($_GET[&quot;count&quot;] == &quot;yes&quot;) </li></ul></ul><ul><ul><li>$_SESSION[&quot;count&quot;] = $_SESSION[&quot;count&quot;] + 1; </li></ul></ul><ul><ul><li>echo &quot;<h1>&quot;.$_SESSION[&quot;count&quot;].&quot;</h1>&quot;; </li></ul></ul><ul><ul><li>?> </li></ul></ul><ul><ul><li><a href=&quot;session.php?count=yes&quot;>Click here to count</a> </li></ul></ul>
  64. 64. Avoid Error PHP - Sessions PHP Example: <?php echo &quot;Look at this nasty error below:<br />&quot;; session_start(); ?> Error! PHP Example: <?php session_start(); echo &quot;Look at this nasty error below:&quot;; ?> Correct Warning: Cannot send session cookie - headers already sent by (output started at session_header_error/session_error.php:2) in session_header_error/session_error.php on line 3 Warning: Cannot send session cache limiter - headers already sent (output started at session_header_error/session_error.php:2) in session_header_error/session_error.php on line 3
  65. 65. Destroy PHP - Sessions Destroying a Session why it is necessary to destroy a session when the session will get destroyed when the user closes their browser. Well, imagine that you had a session registered called &quot;access_granted&quot; and you were using that to determine if the user was logged into your site based upon a username and password. Anytime you have a login feature, to make the users feel better, you should have a logout feature as well. That's where this cool function called session_destroy() comes in handy. session_destroy() will completely demolish your session (no, the computer won't blow up or self destruct) but it just deletes the session files and clears any trace of that session. NOTE: If you are using the $_SESSION superglobal array like we are in this tutorial, you must clear the array values first, then run session_destroy. Here's how we use session_destroy ():
  66. 66. Destroy PHP - Sessions <?php // start the session session_start(); header(&quot;Cache-control: private&quot;); //IE 6 Fix $_SESSION = array (); session_destroy(); echo &quot;<strong>Step 5 - Destroy This Session </strong><br />&quot;; if($_SESSION['name']){     echo &quot;The session is still active&quot;; } else {     echo &quot;Ok, the session is no longer active! <br />&quot;;     echo &quot;<a href=&quot;page1.php&quot;><< Go Back Step 1</a>&quot;; } ?>
  67. 67. <ul><li>Fast, free, stable database </li></ul><ul><li>Syntax is similar to Oracle </li></ul><ul><li>Many of the same features as Oracle </li></ul><ul><li>Production version still missing subqueries, stored procedures, and triggers </li></ul><ul><li>Frequently used in conjunction with Linux, Apache, and PHP </li></ul>MySQL Overview
  68. 68. MySQL – Database Basics <ul><li>A relational database manager (MySQL) manages databases which holds tables which has records (rows) with attributes (columns) </li></ul><ul><li>Each record must have a unique ID, also known as a Primary Key. When used as an identifier in another table it’s called a Foreign Key. Used for joins. </li></ul><ul><li>Each attribute has to have a data type. (e.g. int, text, varchar) </li></ul><ul><li>A database language (SQL) is used to create and delete databases and manage data </li></ul>
  69. 69. <ul><li>Table structure for following examples: </li></ul><ul><li>Created two tables, ‘oscarpool’ & ‘bestdirector’ using: </li></ul><ul><li>(a) use MySQL either in line mode (cd c:mysqlin and mysql) or </li></ul><ul><li>(b) Use MySQL Control Center </li></ul><ul><li>(c) use phpMyAdmin tool which you can get from </li></ul><ul><li> </li></ul><ul><li>phpMyAdmin is a tool written in PHP intended to handle the administration of MySQL over the Web. Currently it can create and drop databases, create/drop/alter tables, delete/edit/add fields, execute any SQL statement, manage keys on fields, manage privileges,export data into various formats http://localhost:8088/mysql/index.php </li></ul>MySQL – Create Tables CREATE TABLE oscarpool ( uid int(4) auto_increment, username varchar(255), email varchar(255), bestpicture int(2), PRIMARY KEY (uid) ) CREATE TABLE bestdirector ( bdid int(4) auto_increment, name varchar(255), PRIMARY KEY (bdid) )
  70. 70. <ul><li>Common SQL Statement: INSERT </li></ul><ul><ul><ul><li>INSERT INTO </li></ul></ul></ul><ul><ul><ul><li>oscarpool </li></ul></ul></ul><ul><ul><ul><li>(username,email,bestpicture) </li></ul></ul></ul><ul><ul><ul><li>VALUES </li></ul></ul></ul><ul><ul><ul><li>(‘dolsen',‘',1) </li></ul></ul></ul><ul><li>Creates a new record in the table ‘oscarpool’ </li></ul><ul><li>Text fields need to have ‘s. </li></ul><ul><li>Tip: If you have an ‘ in your data you need to escape it before inserting it. Can use the PHP function addslashes(). </li></ul><ul><li>Example: ‘John O’Brien’ </li></ul>MySQL – INSERT
  71. 71. <ul><li>Common SQL Statement: SELECT </li></ul><ul><ul><ul><li>SELECT uid,username </li></ul></ul></ul><ul><ul><ul><li>FROM oscarpool </li></ul></ul></ul><ul><li>Selects the attributes ‘uid’ and ‘username’ from every record in ‘oscarpool’ </li></ul><ul><li>SELECT is how you query the database. You can also: </li></ul><ul><ul><li>limit the number of records returned with LIMIT, </li></ul></ul><ul><ul><li>limit retrieval to those records that match a condition with WHERE, </li></ul></ul><ul><ul><li>sort the data after the query has been evaluated using ORDER BY </li></ul></ul><ul><li>Tip: To easily select every attribute replace ‘uid’ with ‘*’ </li></ul>MySQL – SELECT
  72. 72. <ul><li>Common SQL Statement: UPDATE </li></ul><ul><ul><ul><li>UPDATE oscarpool </li></ul></ul></ul><ul><ul><ul><li>SET email = ‘’ </li></ul></ul></ul><ul><ul><ul><li>WHERE uid = 1 </li></ul></ul></ul><ul><li>Updates the email address where ‘uid = 1’ in the table ‘oscarpool’ </li></ul><ul><li>In this case I know that uid 1 is what my record was. In many cases you’d pass a uid variable from a form. </li></ul>MySQL – UPDATE
  73. 73. <ul><li>Common SQL Statement: DELETE </li></ul><ul><ul><ul><li>DELETE FROM oscarpool </li></ul></ul></ul><ul><ul><ul><li>WHERE uid = 1 </li></ul></ul></ul><ul><li>Deletes the record where ‘uid = 1’ in the table ‘oscarpool’ </li></ul><ul><li>DELETE only removes the record from the table. To remove an entire table from the database you need to use the SQL statement DROP. </li></ul><ul><li>Tip: To remove every record in a table but not remove the table just don’t include the WHERE clause. </li></ul>MySQL – DELETE
  74. 74. <ul><li>SELECT </li></ul><ul><ul><ul><li>FROM oscarpool op, bestdirector bd </li></ul></ul></ul><ul><ul><ul><li>WHERE op.uid = 1 and </li></ul></ul></ul><ul><ul><ul><li>op.bestdirector = bd.bdid </li></ul></ul></ul><ul><li>Selects the name of the Best Director that the user with ‘uid = 1’ has chosen </li></ul><ul><li>bestdirector is a Foreign Key of the Primary Key for the table BestDirector </li></ul>MySQL – JOIN
  75. 75. <ul><li>Entity-Relationship (ER) Modeling </li></ul><ul><li>ER Modeling is the simple and clear method of expressing the design (relations) of a database between tables and attributes. </li></ul><ul><li>Rectangles – Represent entities. </li></ul><ul><li>Diamonds – Represent relationships </li></ul><ul><li>between entities </li></ul><ul><li>Ellipses – Represent attributes that </li></ul><ul><li>describe an entity </li></ul><ul><li>Lines – Connect entities to relationships. </li></ul><ul><li>Can have annotation. </li></ul><ul><li>M = many, 1 = one. </li></ul><ul><li>Lines – Connects entities to attributes. No annotation. </li></ul><ul><li>Entity = Table, Attributes = Attributes </li></ul>MySQL – ERD
  76. 76. MySQL – DB Access <html> <body> <h1>A List of Users Who Have Signed Up For OscarPool</h1> <? $dbh = mysql_connect(&quot;localhost&quot;,&quot;root&quot;,&quot;&quot;) or die(&quot;Couldn't connect to database.&quot;); $db = mysql_select_db(&quot;test&quot;, $dbh) or die(&quot;Couldn't select database.&quot;); $sql = &quot;SELECT username, email FROM oscarpool&quot;; $result = mysql_query($sql, $dbh) or die(&quot;Something is wrong with your SQL statement.&quot;); while ($row = mysql_fetch_array($result)) { $username = $row['username']; $email = $row['email']; echo '<a href=&quot;mailto:'.$email.'&quot;>'.$username.'</a><br /> '; } ?> </body> </html> Save it as data.php
  77. 77. Sample Projects <ul><li>A DB Driven Bulletin Board </li></ul>DB Driven Solutions: Front End Design: Forms (Validation) , Web Page Layout Techs: JavaScripts, Html Tags, Back End Design RDBMS: E.g. MySQL, MSSQL, Oracle PHP Scripts <ul><li>A XML Driven Bulletin Board </li></ul><ul><li>A Bulletin Board with Text File as storage </li></ul>
  78. 78. <ul><li>A Simple Solution: </li></ul><ul><li>Users Table: </li></ul><ul><li>Attr: UserID, Password, First Name, Last Name, Email, Addr, Phone…. </li></ul><ul><li>Topics Table: </li></ul><ul><li>Attr: TopicID, PostedTime, UserID, TopicSubject, TopicBody…. </li></ul><ul><li>Replies Table: </li></ul><ul><li>Attr: ReplyID, PostedTime, TopicID, UserID, ReplySubject, ReplyBody… </li></ul><ul><li>…………………… . </li></ul>Database Design for BBS
  79. 79. <ul><li>Basic Use Cases: </li></ul><ul><li>1. User Registers in the bulletin board </li></ul><ul><li>(Generated UserID and Password) …. Create SQL Users table </li></ul><ul><li>2. A user updates his info </li></ul><ul><li>(Change password, personal Info etc..)….. Update SQL Uers Table </li></ul><ul><li>3. User posts a topic Create SQL, Topics Table </li></ul><ul><li>( Generated TopicID and ….. </li></ul><ul><li>4 . User posts a reply to a topic Create SQL, Replies Table </li></ul><ul><li>( Generated RepliesID and …..) </li></ul><ul><li>…………… </li></ul>Basic Use Cases for BBS
  80. 80. <ul><li>1. If a user was authenticated to post a message in name of his userID? How to create a BBS Administrator? </li></ul><ul><li>(DB Driven Authentication) </li></ul><ul><li>2. How to implement that a user just logs on successfully from the entrance </li></ul><ul><li>page for one-time and the other web pages remember that the user is an </li></ul><ul><li>authenticated user without asking the user to do authentication again? </li></ul><ul><li>(Session) </li></ul><ul><li>3. How to let the browser to remember a userID in the log in Form always? </li></ul><ul><li>(Cookie) </li></ul><ul><li>4. How to implement to sort the bulletin board? </li></ul><ul><li>(Select SQL with some constraints) </li></ul><ul><li>5. How to do form validations, from Client Side or Server Side? Pros and cons? </li></ul><ul><li>Client Scripts Vs. Server Side Scripts </li></ul>Thinking about BBS
  81. 81. PHP References <ul><li> <-- php home page </li></ul><ul><li> </li></ul><ul><li> </li></ul><ul><li> </li></ul><ul><li> </li></ul><ul><li> </li></ul><ul><li> </li></ul><ul><li> </li></ul><ul><li> </li></ul><ul><li> </li></ul><ul><li> <-- php downlad page </li></ul><ul><li> <-- php instllation manual </li></ul><ul><li> <-- PHP resources like sample programs, text book referencs, etc. </li></ul><ul><li>  php forums </li></ul>
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.