Download Workshop Lecture


Published on

  • Be the first to comment

  • Be the first to like this

No Downloads
Total Views
On Slideshare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide
  • Almost from the beginning, the Internet allowed people to make files available for downloading. This was by means of ftp, the file transfer protocol, which you’ve already used. Ftp was developed over the years 1971-1974. But there was no mechanism for automatically fetching a document that was referred to in another document – a human had to run the ftp tool, log in on the remote machine, navigate to the directory, and request the file. Except for plain text documents, any formatting required having the correct application to display the particular format, and formats and applications were specific to each type of operating system. Even if automated linking of documents had existed, the only way to find out about a document would be for one person to tell another, or to find a link to some other document in a document one already had.
  • While working as a software engineer at CERN, the main European high-energy physics lab, Tim Berners-Lee called attention to the reference problem, and proposed a way of dealing with it: links embedded in documents that specify location of another document, automated retrieval, simple format for text documents, means of displaying them. Marc Andreessen and co-workers wrote Mosaic with public funding, then went off to form Netscape, which offered a browser based on Mosaic. This was the original no-apparent-business-model Internet company, because they offered their browser for free. No choice, as they were already taking flak for using a publicly funded tool as the basis for their product. The W3 Consortium is the semi-official standards organization for the Web – semi-official in the sense that it can’t enforce its standards. Certain large companies with a lot of market share tend to ignore the standards or the desire for interoperability when it is convenient for themselves. WebCrawler is often given credit for being the first crawler-based search engine. It was developed right here at the UW. Lycos, like Netscape, served as a model for later Internet companies. It holds the record for the fastest time from launch to IPO in the history of the NASDAQ. AltaVista was for years the main search site. It did not rank results, but had features available nowhere else that allowed the user to control the sort order for results. Only Ask Jeeves today has something similar.
  • Download Workshop Lecture

    1. 1. Web Development Workshop May 24-25, 2008 Presenter: Md. Mahbubul Islam Lecturer Dept. of Computer Science and Engineering, University of Rajshahi. Organized By: Web Club, CSE, RU
    2. 2. Course Outline (Day1) <ul><li>WWW Basic </li></ul><ul><li>HTML Basic </li></ul><ul><li>Introduction to PHP </li></ul><ul><li>Why use PHP </li></ul><ul><li>Data Types </li></ul><ul><li>Constants </li></ul><ul><li>Expressions </li></ul><ul><li>Arrays </li></ul><ul><li>Control Structures </li></ul><ul><li>PHP functions </li></ul><ul><li>Intro to HTML Forms </li></ul><ul><li>PHP Configuration </li></ul><ul><li>PHP Cookies and Sessions </li></ul><ul><li>String Manipulation </li></ul><ul><li>Sending email </li></ul><ul><li>File Handling </li></ul><ul><li>Handling file uploads </li></ul><ul><li>Introduction To Database </li></ul><ul><li>MySQL Introduction </li></ul><ul><li>Basic MySQL Operations </li></ul><ul><li>Accessing MySQL </li></ul><ul><li>Graphics in PHP </li></ul><ul><li>Tricks and Tips </li></ul>
    3. 3. Course Outline (Day2) <ul><li>Using Dreamweaver </li></ul><ul><li>Using FTP Software </li></ul><ul><li>Using Hosting Cpanel </li></ul><ul><li>Using PHPMyAdmin </li></ul><ul><li>Developing a data driven dynamic web site. </li></ul>
    4. 4. Tools and Language <ul><li>Macromedia Dreamweaver8.0 </li></ul><ul><li>Adobe Photoshop </li></ul><ul><li>HTML </li></ul><ul><li>PHP </li></ul><ul><li>MySQL </li></ul><ul><li>Apache </li></ul><ul><li>Cute FTP </li></ul><ul><li>CSS </li></ul><ul><li>Java Script </li></ul><ul><li>Coffee Cup Menu Builder </li></ul>XAMPP
    5. 5. History of the WWW <ul><li>Issues that drove development of the Web </li></ul><ul><ul><li>No transparent way to link documents or objects </li></ul></ul><ul><ul><ul><li>References by citation or ftp address </li></ul></ul></ul><ul><ul><li>No automatic way to retrieve linked documents </li></ul></ul><ul><ul><ul><li>Downloading via ftp required human intervention </li></ul></ul></ul><ul><ul><li>No standard document format </li></ul></ul><ul><ul><ul><li>Each format required its own application </li></ul></ul></ul><ul><ul><li>Even with links, no way to find documents </li></ul></ul><ul><ul><ul><li>except by being told where to find them, following links </li></ul></ul></ul>
    6. 6. History of the WWW <ul><li>Web beginnings </li></ul><ul><ul><li>1989: Tim Berners-Lee </li></ul></ul><ul><ul><ul><li>URLs, http, first browser </li></ul></ul></ul><ul><ul><li>1993: NCSA Mosaic </li></ul></ul><ul><ul><ul><li>Then Netscape, then Mozilla </li></ul></ul></ul><ul><ul><li>1994: World Wide Web Consortium </li></ul></ul><ul><ul><ul><li> </li></ul></ul></ul><ul><ul><ul><li>Standards organization for Web protocols and formats </li></ul></ul></ul><ul><ul><li>1994-5: Web crawlers and search engines </li></ul></ul><ul><ul><ul><li>WebCrawler, Lycos, AltaVista, Yahoo </li></ul></ul></ul>
    7. 7. World Wide Web <ul><li>World Wide Web (WWW) is a collection of servers (subset of Internet computers) and the info they give access to using the HTTP protocol </li></ul><ul><ul><li>WWW is not the same as the Internet </li></ul></ul><ul><ul><li>The “server” is a web site computer and the “client” is a web browser (like Internet Explorer) </li></ul></ul><ul><ul><li>Many Web server’s domain names begin with www by tradition, but any name is OK </li></ul></ul><ul><ul><li>Often multiple servers map to the same site: and </li></ul></ul>
    8. 8. Essential Components of WWW <ul><li>URL -- uniform resource locator </li></ul><ul><ul><li>Strings of characters represent generalized addresses that may contain instructions for accessing the identified resource </li></ul></ul><ul><ul><li>http:// is used to identify the RU homepage </li></ul></ul><ul><li>HTTP -- hypertext transfer protocol </li></ul><ul><ul><li>Conventions that regulate the communication between a browser (web user agent) and a server </li></ul></ul><ul><li>HTML -- hypertext markup language </li></ul><ul><ul><li>Conceptual mappings to concrete or abstract entities. </li></ul></ul><ul><ul><li>ex: website (web pages and other kinds of files) </li></ul></ul><ul><ul><li>Other Resources.. </li></ul></ul>
    9. 9. Client/Server Interaction <ul><li>For Web pages, the client requests a page the server returns it: there’s no permanent connection, just a short conversation </li></ul><ul><ul><li>Details of the conversation are specified by HTTP </li></ul></ul>Client Server Client Client Client Client Client Client Server Client Server Server Server Server Server Server request response
    10. 10. Markup Language - HTML <ul><li>HyperText Markup Language </li></ul><ul><ul><li>a language for describing the content and presentation of a web page </li></ul></ul><ul><ul><ul><li>content: The meaning and structure of the web page </li></ul></ul></ul><ul><ul><ul><li>presentation: How the page is to be presented </li></ul></ul></ul><ul><ul><li>HTML pages have a basic hierarchical structure defined by the tags </li></ul></ul><ul><ul><ul><li><html>, <head>, <body>, and so on </li></ul></ul></ul><ul><ul><li>Basic HTML describes a static page </li></ul></ul><ul><ul><ul><li>once parsed and rendered, the page doesn’t change </li></ul></ul></ul><ul><ul><ul><li>hyperlinking was the big step forward in basic HTML </li></ul></ul></ul>
    11. 11. HTML Functionalities <ul><li>HTML gives authors the means to: </li></ul><ul><li>Publish online documents with headings, text, tables, lists, photos, etc </li></ul><ul><ul><li>Include spread-sheets, video clips, sound clips, and other applications directly in their documents </li></ul></ul><ul><li>Link information via hypertext links, at the click of a button </li></ul><ul><li>Design forms for conducting transactions with remote services, for use in searching for information, making reservations, ordering products, etc </li></ul>
    12. 12. Accessing HTML Files Note: The file is just retrieved no code is executed on server Note2: HTML tags interpreted on client by browser. http protocol Your PC (Internet connected) Web Server (Internet connected) URL: funstuff.html Web browser Find Web server at Web server receives request Sends file with funstuff.html The file funstuff.html Here are some fun things to do: 1. Play baseball. 2. Go swimming. 3. Go to a concert. Web browser
    13. 13. Basic HTML <ul><li>HTML uses tags to specify the formatting </li></ul><ul><ul><li>The tags are not displayed in the browser, they tell the browser how to display the document </li></ul></ul><ul><ul><li>Tags in HTML appear as angle brackets: </li></ul></ul><ul><ul><ul><li><the tag format> </li></ul></ul></ul><ul><ul><li>Each Tag needs to have an end: </li></ul></ul><ul><ul><li></the tag format> </li></ul></ul><ul><ul><li>Tags can be used to apply formatting to individual words, sentences, paragraphs or the whole HTML page </li></ul></ul><ul><ul><li><center><u> This is a sample <b> HTML </b> line formatted </u></center> </li></ul></ul><ul><ul><li>Is displayed in the internet browser as the following: </li></ul></ul><ul><ul><li>This is a sample HTML line formatted </li></ul></ul>
    14. 14. Basic HTML <ul><li>Important Points </li></ul><ul><ul><li>All file names need to be in the correct case (html page names, image names etc), </li></ul></ul><ul><ul><ul><li>Cars.html is treated differently than cars.html </li></ul></ul></ul><ul><ul><ul><li>Myphoto.jpg is treated differently than myphoto.jpg </li></ul></ul></ul><ul><ul><li>Page names should be meaningful not just Noname.html or page1.html </li></ul></ul><ul><ul><li>Don’t use a whitespace character in the file name like ‘my page.html’ use an underscore instead like ‘my_page.html’ </li></ul></ul><ul><ul><ul><li>Acceptable Characters: (A-Z) (a-z)(0-9) or _ . </li></ul></ul></ul><ul><ul><li>First pages of a website need to be called index.html </li></ul></ul><ul><ul><li>HTML TAGS use American spelling like color not colour </li></ul></ul>
    15. 15. Basic HTML Page <HTML> <HEAD> <TITLE> </TITLE> </HEAD> <BODY> </BODY> </HTML> All HTML pages require the following basic structured tags The text is added here These tags must appear at the end of the HTML page These basic structured tags should only appear once per HTML page i.e: only one <body> tag and one </body> tag etc
    16. 16. Basic HTML Page <HTML> <HEAD> <TITLE> My Dublin Homepage </TITLE> </HEAD> <BODY> Dublin is a city...... <center><u> This is a sample <b> HTML </b> line formatted </u></center> </BODY> </HTML>
    17. 17. HTML Formatting <ul><li>Font Formatting </li></ul><ul><ul><li>To bold text the following tag is used: <B> … </B> </li></ul></ul><ul><ul><li>To underline text use the following: <U> … </U> </li></ul></ul><ul><ul><li>To italics text: <I> …. </I> </li></ul></ul><ul><li>The size, colour and font face type can also be modified </li></ul><ul><li><FONT SIZE=&quot;7&quot; COLOR=&quot;#FF0000&quot; FACE=&quot;&quot;> The font test </FONT> </li></ul><ul><li>HTML 4.0: <font style=&quot;font-family:verdana;font-size:60;color:#660000&quot;> </li></ul><ul><li>This is the text </font> </li></ul>Some Acceptable fonts courier, courier new comic sans ms arial, garamond verdana Some Acceptable Colours Red, green, blue, yellow, orange…. Specific codes can also be used for colour: #FFFFFF= white #000000=black #FF0000=red #808080=Navy
    18. 18. HTML Formatting <ul><li>Text Formatting </li></ul><ul><ul><li>To move text onto a new line use <BR> </li></ul></ul><ul><ul><li>To start a new paragraph use <P> </li></ul></ul><ul><ul><li>To add more than one white space character use the following: &nbsp; </li></ul></ul><ul><ul><li>To add a line separating text use <HR> or <HR color=&quot;#330000&quot; width=&quot;80%&quot;> </li></ul></ul>Unordered List (bullet points) <UL> <LI>Part 1 <BR> <LI>Part 2 <BR> <LI> Part 3 <BR> </UL> Ordered List (Numbers) <OL> <LI>Part 1 <BR> <LI>Part 2 <BR> <LI> Part 3 <BR> </OL>
    19. 19. HTML Formatting <ul><li>Hyperlinking Webpages: </li></ul><ul><ul><li>External page: </li></ul></ul><ul><ul><ul><li><a href=&quot;; > Link to google </a> </li></ul></ul></ul><ul><ul><li>Internal page: </li></ul></ul><ul><ul><ul><li><a href=&quot;MyPage.html&quot; > MyPage </a> </li></ul></ul></ul><ul><ul><li>Linking within page: <a href=&quot;#C4&quot;> Images </a> (Link) </li></ul></ul><ul><ul><ul><li>Section to link to: <a name=&quot;C4&quot;><h2>Chapter 4</h2></a> </li></ul></ul></ul><ul><li>Inserting an Image: </li></ul><ul><ul><li>Using an Image as a link: </li></ul></ul><ul><ul><ul><li><IMG SRC=“picture.jpg&quot; WIDTH=&quot;2272&quot; HEIGHT=&quot;1704&quot; BORDER=&quot;0&quot; ALT=&quot;text&quot;> </li></ul></ul></ul><ul><ul><li>Using an Image as a link: </li></ul></ul><ul><ul><li><a href=“nextpage.html&quot;> </li></ul></ul><ul><ul><li><img border=&quot;0&quot; src=&quot;nextpage.jpg&quot; width=&quot;65&quot; height=&quot;38&quot;> </li></ul></ul><ul><ul><li></a> </li></ul></ul>
    20. 20. HTML Background <ul><li>Modifying Background Colour </li></ul><ul><ul><li>Instead of <BODY> use: <BODY bgcolor=&quot;#FFFF99&quot; > </li></ul></ul><ul><ul><li>The Colour of hyperlinks can also be modified: </li></ul></ul><ul><ul><ul><li><BODY bgcolor=&quot;#FFFF99&quot; link=&quot;#330000&quot; alink=&quot;#330000&quot; vlink=&quot;#330000&quot; > </li></ul></ul></ul><ul><li>Embedding video, shockwave flash… </li></ul><ul><ul><li><embed width=100 height=168 pluginspage=&quot;; src=&quot;2002.mpg&quot; width=192 loop=&quot;true&quot; controller=&quot;true&quot; autoplay=&quot;true&quot;></embed> </li></ul></ul>
    21. 21. HTML Tables <ul><li>Tables in HTML consist ‘cells’ of rows and columns </li></ul><ul><ul><li>Very important method of formatting </li></ul></ul><ul><ul><li>A Table of one row two columns could place an image in one cell and text in the other… </li></ul></ul><ul><ul><li>Things can get very complicated like tables within tables </li></ul></ul><ul><ul><li><table border=&quot;1&quot; bordercolor=&quot;#330000&quot; </li></ul></ul><ul><ul><li>cellpadding=&quot;10&quot;> </li></ul></ul><ul><ul><li><tr> </li></ul></ul><ul><ul><li><td bgcolor=&quot;#FF3300&quot; width=&quot;300&quot;>First</td> </li></ul></ul><ul><ul><li><td>Row</td> </li></ul></ul><ul><ul><li></tr> </li></ul></ul><ul><ul><li><tr> </li></ul></ul><ul><ul><li><td>Second</td> </li></ul></ul><ul><ul><li><td>Row</td> </li></ul></ul><ul><ul><li></tr> </li></ul></ul><ul><ul><li></table> </li></ul></ul>
    22. 22. Worried about HTML code? <ul><li>Use Editor like </li></ul><ul><ul><li>Dreamweaver </li></ul></ul><ul><ul><li>Frontpage </li></ul></ul><ul><ul><li>Coffee cup HTML editor </li></ul></ul><ul><ul><li>And many more…. </li></ul></ul>
    23. 23. Client Side Scripting <ul><li>Client-side programming is based on the idea that the computer which the client is using to browse the web has quite a bit of CPU power sitting there doing nothing. </li></ul><ul><li>Meanwhile, web servers are being tasked to death handling hundreds of CGI requests above and beyond their regular duties. </li></ul><ul><li>Thus, it makes sense to share some of that burden between the client and server by taking some of the processing load off the server and giving it to the client. </li></ul>
    24. 24. What Client side scripts can do? <ul><li>Scripts can be used to add interesting (Interactive Web Pages) and useful effects to Web pages (Dynamic pages) </li></ul><ul><li>Validations and error checking </li></ul><ul><li>Maintaining state </li></ul><ul><li>Creating Dynamic Forms with Client-Side Scripting. </li></ul><ul><li>Instant Feedback to Users </li></ul><ul><li>Client-Side Scripts Move Processing Tasks back to the Client </li></ul>
    25. 25. Disadvantages of Client Side Scripting <ul><li>Browser-Dependent Client-Side Scripts </li></ul><ul><li>Different set of codes for both the browsers </li></ul><ul><li>Secure Source Code of Client-Side Scripts. </li></ul><ul><li>Pages Take Longer to Download </li></ul><ul><li>Program Scope Is Limited to a Single HTML Page </li></ul><ul><li>No Direct Access to System Objects </li></ul>
    26. 26. Which Should I Use? Client- or Server-Side? <ul><li>If you want to have dynamic client forms with client-side validation, you must use client-side scripting. </li></ul><ul><li>If you want your site to have highly interactive pages, you should use client-side scripting. </li></ul><ul><li>If you need to provide your client with advanced functionality that can be created only using ActiveX controls, you must use client-side scripting. </li></ul>
    27. 27. Which Should I Use? Client- or Server-Side? Cont… <ul><li>If you want to control the user's browser (that is, you want to turn off the menus and place the browser in kiosk mode), you must use client-side scripting </li></ul><ul><li>If your Web site must work with every browser on the market, and you do not want to create several different versions for different browsers, you should avoid client-side scripting </li></ul><ul><li>If you want to protect your source code, you must use only server-side scripting. All client-side source code is transferred to the browser. </li></ul>
    28. 28. Which Should I Use? Client- or Server-Side? Cont… <ul><li>If you need to track user information across several Web pages to create a &quot;Web application,&quot; you must use server-side scripting </li></ul><ul><li>If you need to interact with server-side databases, you must use server-side scripting. </li></ul><ul><li>If you need to use HTTP server variables or check the capabilities of the user's browser, you must use server-side scripting </li></ul>
    29. 29. Server and Client Side Scripting <ul><li>Client Side </li></ul><ul><ul><li>Java script </li></ul></ul><ul><ul><li>VB Script </li></ul></ul><ul><li>Server Side </li></ul><ul><ul><li>PHP </li></ul></ul><ul><ul><li>JSP </li></ul></ul><ul><ul><li>ASP </li></ul></ul><ul><ul><li>ASP.NET </li></ul></ul>
    30. 30. Brief History of PHP <ul><li>PHP (PHP: Hypertext Preprocessor) was created by Rasmus Lerdorf in 1994 . It was initially developed for HTTP usage logging and server-side form generation in Unix. PHP originally stood for “ Personal Home Page ”. </li></ul><ul><li>PHP 2 (1995) transformed the language into a Server-side embedded scripting language. Added database support, file uploads, variables, arrays, recursive functions, conditionals, iteration, regular expressions, etc. </li></ul><ul><li>PHP 3 (1998) added support for ODBC data sources, multiple platform support, email protocols (SNMP,IMAP), and new parser written by Zeev Suraski and Andi Gutmans . </li></ul><ul><li>PHP 4 (2000) became an independent component of the web server for added efficiency. The parser was renamed the Zend Engine. Many security features were added. </li></ul><ul><li>PHP 5 (2004) adds Zend Engine II with object oriented programming, robust XML support using the libxml2 library, SOAP extension for interoperability with Web Services, SQLite has been bundled with PHP . Last version is PHP 5.2.6 </li></ul>
    31. 31. Why is PHP used? <ul><li>Easy to Use Code is embedded into HTML. The PHP code is enclosed in special start and end tags that allow you to jump into and out of &quot;PHP mode&quot;. </li></ul><ul><ul><li><html>    <head>        <title>Example</title>    </head>    <body>        <?php        echo &quot;Hi, I'm a PHP script!&quot;;        ?>    </body> </html> </li></ul></ul>
    32. 32. Why is PHP used? <ul><li>Cross Platform Runs on almost any Web server on several operating systems. One of the strongest features is the wide range of supported databases </li></ul><ul><li>Web Servers: Apache, Microsoft IIS, Caudium, Netscape Enterprise Server. 30% of web servers on Internet have PHP installed Operating Systems: UNIX (HP-UX,OpenBSD,Solaris,Linux), Mac OSX, Windows NT/98/2000/XP/2003 </li></ul><ul><li>Supported Databases: Adabas D, dBase,Empress, FilePro (read-only), Hyperwave,IBM DB2, Informix, Ingres, InterBase, FrontBase, mSQL, Direct MS-SQL, MySQL, ODBC, Oracle (OCI7 and OCI8), Ovrimos, PostgreSQL, SQLite, Solid, Sybase, Velocis,Unix dbm </li></ul>
    33. 33. Why is PHP used? <ul><li>Cost Benefits PHP is free. Open source code means that the entire PHP community will contribute towards bug fixes. There are several add-on technologies (libraries) for PHP that are also free. </li></ul>Free PHP Coder , jEdit Development Tools Free (Linux) Platform Software Free PHP
    34. 34. PHP and ASP market share (February 2008) <ul><li>Source: </li></ul><ul><li>PHP dominates ASP by a wide margin. </li></ul><ul><li>Note: PHP runs on Windows and *n*x. </li></ul>
    35. 35. PHP Adoption by Country
    36. 36. Web servers distribution
    37. 37. Apache adoption by country
    38. 38. How PHP Pages are Accessed and Interpreted 2. Send Request for PHP file 6. Return Results Please Enter A Phone Number Submit Erase 1. Web Browser Web Browser Phone Query Results: That is John Doe's Phone Number 7. Web Browser Your PC (Internet connected) WebServer (Internet connected) Web Server Software 3. Receive request, find file and read it. 4. Execute PHP statements 5. Send results back.
    39. 39. HTML in a PHP page <head> <title>PHP Info</title> <?php print(&quot;PHP Stats on the Server&quot;); ?> </head> <body> <?php phpinfo(); ?> And print some more text here! </body> </html> <script> block in <head> <script> block in <body> Language we are using is PHP Generate HTML “ on the fly” with print(...);
    40. 40. Data Types in PHP <ul><li>PHP supports eight primitive data types </li></ul><ul><ul><li>There are four scalar types </li></ul></ul><ul><ul><ul><li>boolean </li></ul></ul></ul><ul><ul><ul><li>integer </li></ul></ul></ul><ul><ul><ul><li>floating-point number </li></ul></ul></ul><ul><ul><ul><li>string </li></ul></ul></ul><ul><ul><li>There are two structured types </li></ul></ul><ul><ul><ul><li>array </li></ul></ul></ul><ul><ul><ul><li>object </li></ul></ul></ul><ul><ul><li>There are two special data types </li></ul></ul><ul><ul><ul><li>resource </li></ul></ul></ul><ul><ul><ul><li>NULL </li></ul></ul></ul><ul><ul><li>The programmer does not need to specify the type of a variable </li></ul></ul><ul><ul><ul><li>a variable’s type is determined from the context of its usage </li></ul></ul></ul>
    41. 41. Booleans <ul><li>The boolean data type admits two values </li></ul><ul><ul><li>true (case-insensitive) </li></ul></ul><ul><ul><li>false (case-insensitive) </li></ul></ul><ul><li>Example </li></ul><ul><ul><li>$itIsRainingToday = true; </li></ul></ul><ul><ul><li>$thePrinterIsBusy = True; </li></ul></ul><ul><ul><li>$theQueueIsEmpty = FALSE; </li></ul></ul>
    42. 42. Integers <ul><li>Integers can be specified in decimal, hexadecimal or octal notation, optionally preceded by a sign </li></ul><ul><ul><li>In octal notation, the number must have a leading 0 </li></ul></ul><ul><ul><li>In hexadecimal notation , the number must have a leading 0x. </li></ul></ul><ul><li>Example s </li></ul><ul><ul><li>$a = 1234; # decimal number </li></ul></ul><ul><ul><li>$a = 0123; # octal number (i.e., 83 decimal) </li></ul></ul><ul><ul><li>$a = -123; # a negative number </li></ul></ul><ul><ul><li>$a = 0x1 B ; # hexadecimal number (i.e., 2 7 decimal) </li></ul></ul><ul><li>The maximum size of an integer is platform-dependent, but usually it’s 32 bits signed – about 2,000,000,000 </li></ul><ul><li>PHP does not support unsigned integers. </li></ul>
    43. 43. Floating Point Numbers <ul><li>Specified using any of these forms </li></ul><ul><ul><ul><li>$a = 1.234; </li></ul></ul></ul><ul><ul><ul><li>$a = 1.2e3; </li></ul></ul></ul><ul><ul><ul><li>$a = 7E-10; </li></ul></ul></ul><ul><li>The maximum size of a float is platform-dependent, although most support a maximum of about 1.8e308 with a precision of roughly 14 decimal digits </li></ul>
    44. 44. Strings <ul><li>Specified in different ways </li></ul><ul><ul><li>single quoted </li></ul></ul><ul><ul><ul><li>In single-quoted strings, single-quotes and backslashes must be escaped with a preceding backslash </li></ul></ul></ul><ul><ul><ul><ul><ul><li>echo 'this is a simple string'; </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><ul><li>echo 'You c an embed newlines in strings, </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><li>just like this .'; </li></ul></ul></ul></ul><ul><ul><ul><ul><ul><li>echo ‘ Douglas MacArthur said &quot;I'll be back ” when leaving the Phillipines '; </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><ul><li>echo 'Are you sure you want to delete C:.*?'; </li></ul></ul></ul></ul></ul><ul><ul><li>double quoted In double-quoted strings, </li></ul></ul><ul><ul><ul><ul><li>variables are interpreted to their values, and </li></ul></ul></ul></ul><ul><ul><ul><ul><li>various characters can be escaped </li></ul></ul></ul></ul><ul><ul><ul><ul><ul><li> linefeed </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><ul><li> carriage return </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><ul><li> horizontal tab </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><ul><li> backslash </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><ul><li>$ dollar sign </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><ul><li>” double quote </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><ul><li>[0-7]{1,3} a character in octal notation </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><ul><li>x[0-9A-Fa-f]{1,2} a character in hexadecimal notation </li></ul></ul></ul></ul></ul>
    45. 45. Variables Names <ul><li>In PHP variable starts with a $ </li></ul><ul><li>Followed by letter or underscore </li></ul><ul><li>Can contain letters, numbers, underscores, or dashes </li></ul><ul><li>No spaces </li></ul><ul><li>Case-sensitive </li></ul><ul><ul><li>$student and $Student are different as it is case sensitive </li></ul></ul>
    46. 46. Assign a value to a variable <ul><li>The universal form of the assignment statement </li></ul><ul><li>PHP uses the single equals sign = </li></ul><ul><ul><ul><li>$greeting = &quot;Hello World!&quot;; </li></ul></ul></ul><ul><ul><ul><li>$balance = 52; </li></ul></ul></ul><ul><ul><ul><li>$isMatch = false; </li></ul></ul></ul><ul><ul><li>variable gets value </li></ul></ul><ul><ul><ul><li>greeting gets the value “Hello World!” </li></ul></ul></ul><ul><ul><ul><li>balance gets the value 52 </li></ul></ul></ul><ul><ul><ul><li>isMatch gets the value false </li></ul></ul></ul>NOTE: The equals sign = is used differently in math and programming .
    47. 47. Predefined variables <ul><li>$GLOBALS </li></ul><ul><li>$_SERVER </li></ul><ul><li>$_GET </li></ul><ul><li>$_POST </li></ul><ul><li>$_COOKIE </li></ul><ul><li>$_FILES </li></ul><ul><li>$_ENV </li></ul><ul><li>$_REQUEST </li></ul><ul><li>$_SESSION </li></ul>
    48. 48. Variable Scope <ul><li>Local – declared Inside a function </li></ul><ul><li>global – declared Outside a function </li></ul><ul><li>static – retains value between calls </li></ul><ul><li>Parameters and variables declared inside a function are available only inside the function </li></ul><ul><ul><li>They come into existence when the function is called </li></ul></ul><ul><ul><li>They disappear when the function exits </li></ul></ul><ul><li>Variables declared outside a function are not available inside the function </li></ul><ul><li>Automatically global </li></ul><ul><ul><li>$_POST, $_GET </li></ul></ul><ul><ul><li>$_SERVER, $_ENV </li></ul></ul><ul><li>The keyword global </li></ul><ul><ul><li>Makes internal variables available externally </li></ul></ul><ul><ul><li>Makes external variables available internally </li></ul></ul><ul><ul><li>global $fred </li></ul></ul>
    49. 49. Constants <ul><li>A constant is an identifier (name) for a simple value. As the name suggests, that value cannot change during the execution of the script (except for magic constants , which aren't actually constants). A constant is case-sensitive by default. By convention, constant identifiers are always uppercase. </li></ul><ul><li><?php // Valid constant names define(&quot;FOO&quot;,     &quot;something&quot;); define(&quot;FOO2&quot;,    &quot;something else&quot;); define(&quot;FOO_BAR&quot;, &quot;something more&quot;); // Invalid constant names define(&quot;2FOO&quot;,    &quot;something&quot;); // This is valid, but should be avoided: // PHP may one day provide a magical constant // that will break your script define(&quot;__FOO__&quot;, &quot;something&quot;);  ?> </li></ul>
    50. 50. Constants & Magic constants <ul><li>PHP_VERSION </li></ul><ul><li>PHP_OS </li></ul><ul><li>PHP_INT_MAX </li></ul><ul><li>PHP_INT_SIZE </li></ul><ul><li>DEFAULT_INCLUDE_PATH </li></ul><ul><li>__LINE__ </li></ul><ul><li>__FILE__ </li></ul><ul><li>__FUNCTION__ </li></ul>
    51. 51. Expressions <ul><li>The right-hand side of an assignment statement can be any valid expression </li></ul><ul><li>Expressions are “formulas” saying how to manipulate existing values to compute new values </li></ul><ul><ul><li>$balance = $balance - $transaction; </li></ul></ul><ul><ul><li>$seconds = 60*$minutes; </li></ul></ul><ul><ul><li>$message = 'Status code is '.$codeValue; </li></ul></ul>
    52. 52. Comments <ul><li><html> </li></ul><ul><li><head><title>Don't forget your comments</title></head> </li></ul><ul><li><body> </li></ul><ul><li><!–- Start HTML page here --> </li></ul><ul><li><h2>A few words about comments</h2> </li></ul><ul><li><?php </li></ul><ul><li>// Start PHP script here </li></ul><ul><li>$string1 = &quot;Welcome&quot;; </li></ul><ul><li>/* </li></ul><ul><li>Another way to add some comments to your PHP code </li></ul><ul><li>*/ </li></ul><ul><li>$string2 = &quot;$string1 stranger!&quot;; </li></ul><ul><li>?> </li></ul><ul><li>… </li></ul>
    53. 53. Operators <ul><li>Addition (+) </li></ul><ul><li>Subtraction (-) </li></ul><ul><li>Multiplication (*) </li></ul><ul><li>Division (/) </li></ul><ul><li>Modulus (%) </li></ul><ul><li>Negation (-) </li></ul><ul><li>String concatenation (.) </li></ul><ul><li>Pre- & Post-increment (++) </li></ul><ul><li>Pre & Post-decrement (--) </li></ul><ul><li>Equality (==) </li></ul><ul><li>Identical (===) </li></ul><ul><li>Inequality (!= or <>) </li></ul><ul><li>Not identical (!==) </li></ul><ul><li>Greater than (>) </li></ul><ul><li>Less that (<) </li></ul><ul><li>Greater than or equal (>=) </li></ul><ul><li>Less than or equal (<=) </li></ul><ul><li>Logical AND (&&, and) </li></ul><ul><li>Logical OR (||, or) </li></ul><ul><li>Logical XOR (xor) </li></ul><ul><li>Logical negation (!) </li></ul>
    54. 54. Operators (Cont..) <ul><li>Assignment </li></ul><ul><li>Assignment (=) </li></ul><ul><li>Plus-equals (+=) </li></ul><ul><li>Minus-equals (-=) </li></ul><ul><li>Divide-equals (/=) </li></ul><ul><li>Multiply-equals (*=) </li></ul><ul><li>Modulus-equals (%=) </li></ul><ul><li>Miscellaneous </li></ul><ul><li>Error suppression (@) </li></ul><ul><li>Conditional or Ternary operator (?:) </li></ul>
    55. 55. Arrays <ul><li>$dog1 = &quot;Flash&quot;; </li></ul><ul><li>$dog2 = &quot;Buffy&quot;; </li></ul><ul><li>$bird = &quot;Mr. Bird&quot;; </li></ul><ul><li>$pets = array($dog1, $dog2, $bird); </li></ul><ul><li>where $pets[0] is &quot;Flash&quot;; </li></ul><ul><li>where $pets[1] is &quot;Buffy&quot;; </li></ul><ul><li>and where $pets[2] is &quot;Mr. Bird&quot;; </li></ul><ul><li>Associate Arrays </li></ul><ul><li>$FlashDog = array(name=>&quot;Flash&quot;, color=>&quot;black&quot;, age=>17); </li></ul><ul><li>print $FlashDog[&quot;color&quot;]; </li></ul>
    56. 56. Multi-dimensional Arrays <ul><li>$animals = array ( </li></ul><ul><li>array (name=>&quot;Flash&quot;, </li></ul><ul><li>type=>&quot;dog&quot;, </li></ul><ul><li>color=>&quot;black&quot;), </li></ul><ul><li>array (name=>&quot;Buffy&quot;, </li></ul><ul><li>type=>&quot;dog&quot;, </li></ul><ul><li>color=>&quot;blond&quot;), </li></ul><ul><li>array (name=>&quot;Mr. Bird&quot;, </li></ul><ul><li>type=>&quot;bird&quot;, </li></ul><ul><li>color=>&quot;blue&quot;) </li></ul><ul><li>); </li></ul><ul><li>print $animals[2][&quot;type&quot;]; </li></ul>
    57. 57. &quot;SuperGlobal&quot; Variables in PHP <ul><li>$_SERVER is an array containing data like headers, paths, etc… </li></ul><ul><li>It has several usual elements </li></ul><ul><ul><li>PHP_SELF </li></ul></ul><ul><ul><li>SERVER_NAME </li></ul></ul><ul><ul><li>REQUEST_METHOD </li></ul></ul><ul><ul><li>DOCUMENT_ROOT </li></ul></ul><ul><ul><li>HTTP_REFERER </li></ul></ul><ul><ul><li>HTTP_USER_AGENT </li></ul></ul><ul><li>$_ENV </li></ul><ul><li>$_GET/$_POST </li></ul><ul><li>$_COOKIE </li></ul><ul><li>$_SESSION </li></ul>
    58. 58. Control Structures <ul><li>if </li></ul><ul><li>else </li></ul><ul><li>elseif </li></ul><ul><li>while </li></ul><ul><li>do-while </li></ul><ul><li>for </li></ul><ul><li>foreach </li></ul><ul><li>break </li></ul><ul><li>continue </li></ul><ul><li>switch </li></ul><ul><li>require </li></ul><ul><li>include </li></ul><ul><li>require_once </li></ul><ul><li>include_once </li></ul>
    59. 59. The if / else statement <ul><li>The if statement is a conditional statement </li></ul><ul><ul><li>a conditional expression is evaluated as being true or false </li></ul></ul><ul><ul><ul><li>the expression is a boolean expression (ie, returns true or false ) </li></ul></ul></ul><ul><ul><li>if the condition is true , then one set of statements is executed </li></ul></ul><ul><ul><li>if the statement is false , then a different set of statements is executed </li></ul></ul><ul><ul><ul><ul><li>if ( < boolean expression > ) { </li></ul></ul></ul></ul><ul><ul><ul><ul><li>< statements > </li></ul></ul></ul></ul><ul><ul><ul><ul><li>} else { </li></ul></ul></ul></ul><ul><ul><ul><ul><li>< statements > </li></ul></ul></ul></ul><ul><ul><ul><ul><li>} </li></ul></ul></ul></ul>
    60. 60. The tricky one <ul><li>The reason this works: if ($a = $b) </li></ul><ul><li>The value assigned to a is returned as the result </li></ul><ul><li>This will be interpreted as false if zero and true otherwise </li></ul><ul><li>Allows us to both assign and drive an if at the same time </li></ul><ul><li>However, it is usually a mistake, a left out = </li></ul>
    61. 61. While Loops <ul><li><?php </li></ul><ul><li>/* example 1 */ </li></ul><ul><li>$i = 1; </li></ul><ul><li>while ($i <= 10) { </li></ul><ul><li>echo $i++; /* the printed value would be </li></ul><ul><li>$i before the increment </li></ul><ul><li>(post-increment) */ </li></ul><ul><li>} </li></ul><ul><li>/* example 2 */ </li></ul><ul><li>$i = 1; </li></ul><ul><li>while ($i <= 10): </li></ul><ul><li>echo $i; </li></ul><ul><li>$i++; </li></ul><ul><li>endwhile; </li></ul><ul><li>?> </li></ul>
    62. 62. do-while <ul><li>do-while loops are very similar to while loops, except the truth expression is checked at the end of each iteration instead of in the beginning. </li></ul><?php $i = 0; do {     echo $i++; } while ($i > 0); ?>
    63. 63. The for loop A counting loop is usually implemented with for for ($i=0; $i < $count; $i++) { print(&quot;<br>index value is : $i&quot;); } initialize check for limit update loop control index shorthand for i=i+1 one or more statements in the loop body
    64. 64. foreach loops <ul><li>For looping over an array </li></ul><ul><li>foreach (array_expression as $value) statement </li></ul><ul><li>foreach (array_expression as $key => $value) statement </li></ul><ul><li><?php </li></ul><ul><li>$arr = array(1, 2, 3, 4); </li></ul><ul><li>foreach ($arr as $key=$value) {    echo &quot;$key = $value&quot;; } </li></ul><ul><li>?> </li></ul>0 = 1 1 = 2 2 = 3 3 = 4
    65. 65. break <ul><li>break ends execution of the current for, foreach, while, do-while or switch structure. </li></ul><ul><li><?php </li></ul><ul><li>for ($i = 0; $i < 10; ++$i) { </li></ul><ul><li>if ($i == 5) </li></ul><ul><li>break; </li></ul><ul><li>print &quot;$i<br>&quot;; </li></ul><ul><li>} </li></ul><ul><li>?> </li></ul>0 1 2 3 4
    66. 66. continue <ul><li>continue is used within looping structures to skip the rest of the current loop iteration and continue execution at the condition evaluation and then the beginning of the next iteration. </li></ul><ul><li><?php </li></ul><ul><li>for ($i = 0; $i < 6; ++$i) { </li></ul><ul><li>if ($i == 3) </li></ul><ul><li>continue; </li></ul><ul><li>print &quot;$i<br>&quot;; </li></ul><ul><li>} </li></ul><ul><li>?> </li></ul>0 1 2 4 5
    67. 67. switch <ul><li><?php $i=2; </li></ul><ul><li>switch ($i) { case 0:     echo &quot;i equals 0&quot;;     break; case 1:     echo &quot;i equals 1&quot;;     break; case 2:     echo &quot;i equals 2&quot;;     break; default:     echo &quot;i is not equal to 0, 1 or 2&quot;; } ?> </li></ul>i equals 2
    68. 68. require() <ul><li>The require() statement includes and evaluates the specific file. </li></ul><ul><li>use require() if you want a missing file to halt processing of the page </li></ul><?php require  'prepend.php' ; require  $somefile ; require ( 'somefile.txt' ); ?>
    69. 69. <ul><li>The include() statement includes and evaluates the specified file. </li></ul><ul><li>script will continue in case of missing file </li></ul>include() vars.php <?php $color  =  'green' ; $fruit  =  'apple' ; ?> test.php <?php echo  &quot;A $color $fruit&quot; ;  // A include  'vars.php' ; echo  &quot;A $color $fruit&quot; ;  // A green apple ?>
    70. 70. PHP functions <ul><li>What are Functions? </li></ul><ul><li>A function structure: </li></ul><ul><li><?php </li></ul><ul><li>function myname( $1arg, $2arg,….. $narg) {    echo &quot;Example function. &quot;;    return $retval; } </li></ul><ul><li>?> </li></ul>
    71. 71. Example For Global <ul><li><?php </li></ul><ul><li>$a = 1; $b = 2; </li></ul><ul><li>function Sum() { </li></ul><ul><li>    global $a, $b;    $b = $a + $b; } Sum(); echo $b; </li></ul><ul><li>?> </li></ul>b=3
    72. 72. Example For Static <ul><li><?php </li></ul><ul><li>function Tables() { </li></ul><ul><li>    static $count = 0; static $x=0; </li></ul><ul><li>$y=2; </li></ul><ul><li> $count++; </li></ul><ul><li>   $x= $x + $y; </li></ul><ul><li> echo “$x <br>”; </li></ul><ul><li>  If ($count < 5) </li></ul><ul><li> {         Tables();     } } Tables(); </li></ul><ul><li>?> </li></ul>2 4 6 8 10
    73. 73. Nested Functions <ul><li><? </li></ul><ul><li>function fname() </li></ul><ul><li>{ </li></ul><ul><li>echo &quot;fname<br>&quot;; </li></ul><ul><li>function lname() </li></ul><ul><li>{ </li></ul><ul><li>echo &quot;lname<br>&quot;; </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>//lname(); not accesible </li></ul><ul><li>fname(); </li></ul><ul><li>lname(); </li></ul><ul><li>?> </li></ul><ul><li>Here lname() can only be called after calling fname() </li></ul>
    74. 74. Function Arguments <ul><li>Passing By Value </li></ul><ul><li>Eg: </li></ul><ul><li><? </li></ul><ul><li>function add($num) </li></ul><ul><li>{ </li></ul><ul><li>$sum=$num[0]+$num[1]; </li></ul><ul><li>echo &quot;$sum<br>&quot;; </li></ul><ul><li>} </li></ul><ul><li>$num = array(0,1); </li></ul><ul><li>add($num); </li></ul><ul><li>echo $num[0]; </li></ul><ul><li>?> </li></ul><ul><li>Passing By Reference </li></ul><ul><li><?php </li></ul><ul><li>function name(&$string) </li></ul><ul><li>{ </li></ul><ul><li>$string .= 'lname.'; </li></ul><ul><li>} </li></ul><ul><li>$str = 'fname '; </li></ul><ul><li>name($str); </li></ul><ul><li>echo $str; // outputs ‘fname lname‘ </li></ul><ul><li>?> </li></ul>
    75. 75. Use Of Default Parameters <ul><li>Ex: </li></ul><ul><li><?php </li></ul><ul><li>function comname ($lname=&quot;Softech&quot;) </li></ul><ul><li>{ </li></ul><ul><li>return &quot;company name is $lname<br>&quot;; </li></ul><ul><li> } </li></ul><ul><li> echo comname(); </li></ul><ul><li>echo comname(&quot;Relyon&quot;);//prints Relyon </li></ul><ul><li>?> </li></ul><ul><li><? </li></ul><ul><li>function comname ($fname, $lname=&quot;Softech&quot;) </li></ul><ul><li> { </li></ul><ul><li>return &quot;company name is $fname $lname&quot;; </li></ul><ul><li> } </li></ul><ul><li>echo comname(&quot;Relyon&quot;);//prints Relyon Softech </li></ul><ul><li>?> </li></ul>
    76. 76. Intro to HTML Forms <ul><li>Forms allow us to create interactive websites, by accepting input from users </li></ul><ul><li>Types of input we can collect via forms: </li></ul><ul><ul><li>Text via text boxes and text areas </li></ul></ul><ul><ul><li>Selections via radio buttons, check boxes, pulldown menus, and select boxes </li></ul></ul><ul><li>Form actions do all of the work </li></ul><ul><ul><li>Usually through button clicks </li></ul></ul>
    77. 77. Basic HTML Form <ul><li>< form name=&quot;input&quot; action =&quot;form_action.php&quot; method=&quot;get&quot; > </li></ul><ul><li><label for=&quot;user&quot;>Username</label> </li></ul><ul><li>< input type=&quot;text&quot; name=&quot;user&quot; id=&quot;user&quot;> </li></ul><ul><li><br />< input type=&quot;submit&quot; value=&quot;Submit&quot;> </li></ul><ul><li>< /form > </li></ul>
    78. 78. Method attribute <ul><li><form action=&quot;fileWithWhatToDo&quot; </li></ul><ul><li>method =&quot;post&quot;> </li></ul><ul><li></form> </li></ul><ul><li>Value tells how to send data from the form to the server </li></ul><ul><li>GET / POST </li></ul><ul><ul><li>GET is the default </li></ul></ul><ul><ul><li>GET </li></ul></ul><ul><ul><ul><li>Appends ? to the action URI and then lists the names and values of the form elements in pairs </li></ul></ul></ul><ul><ul><ul><li>New URI is sent to the server </li></ul></ul></ul><ul><ul><li>POST </li></ul></ul><ul><ul><ul><li>Name/value pairs are sent in the message body of an HTTP POST request </li></ul></ul></ul>
    79. 79. Which Method to use? <ul><li>GET </li></ul><ul><ul><li>You can see the data in the URL </li></ul></ul><ul><ul><li>Limit on how many characters can be sent </li></ul></ul><ul><ul><li>Can't send files </li></ul></ul><ul><ul><li>Can be bookmarked and revisited </li></ul></ul><ul><li>POST </li></ul><ul><ul><li>You can't see the data in the URL </li></ul></ul><ul><ul><li>No limit on how much info can be sent </li></ul></ul><ul><ul><li>Can send files </li></ul></ul><ul><ul><li>Can't be bookmarked or revisited </li></ul></ul>
    80. 80. Forms contain controls <ul><li>Buttons </li></ul><ul><ul><li>Regular, submit, reset </li></ul></ul><ul><li>Checkboxes </li></ul><ul><li>Radio buttons </li></ul><ul><li>Menus </li></ul><ul><li>Text input </li></ul><ul><li>Hidden </li></ul><ul><li>etc. </li></ul>
    81. 81. INPUT controls <ul><li><form action=&quot;fred.php&quot; method=&quot;post&quot;> </li></ul><ul><li><input type=&quot; text &quot; name=&quot; aName &quot;> </li></ul><ul><li><input type=&quot; password &quot; name=&quot; aName &quot;> </li></ul><ul><li><input type=&quot; hidden &quot; name=&quot; aName &quot;> </li></ul><ul><li><input type=&quot; submit &quot; name=&quot; submit &quot; </li></ul><ul><li>value=&quot; submit &quot;> </li></ul><ul><li><input type=&quot; reset &quot; name=&quot; reset &quot; </li></ul><ul><li>value=&quot; reset &quot;> </li></ul><ul><li></form> </li></ul>
    82. 82. INPUT controls (checkbox) <ul><li><form action=&quot;fred.php&quot; method=&quot;post&quot;> </li></ul><ul><li><input type=&quot; checkbox &quot; name=&quot; check1 &quot; </li></ul><ul><li>value=&quot; c1 &quot;> Checkbox1 text </li></ul><ul><li><input type=&quot; checkbox &quot; name=&quot; check2 &quot; </li></ul><ul><li>value=&quot; c2 &quot;> Checkbox2 text </li></ul><ul><li>Checkbox3 text </li></ul><ul><li><input type=&quot; checkbox &quot; name=&quot; check3 &quot; </li></ul><ul><li>value=&quot; c3 &quot;> </li></ul><ul><li></form> </li></ul><ul><li>(Lets the user see meaningful values, but return the primary key.) </li></ul>
    83. 83. INPUT controls (radio buttons) <ul><li><form action=&quot;fred.php&quot; method=&quot;post&quot;> </li></ul><ul><li><input type=&quot; radio &quot; name=&quot; group &quot; </li></ul><ul><li>value=&quot; r1 &quot;> Radio1 text </li></ul><ul><li><input type=&quot; radio &quot; name=&quot; group &quot; </li></ul><ul><li>value=&quot; r2 &quot;> Radio2 text </li></ul><ul><li>Radio3 text </li></ul><ul><li><input type=&quot; radio &quot; name=&quot; group &quot; </li></ul><ul><li>value=&quot; r3 &quot;> </li></ul><ul><li></form> </li></ul><ul><li>(all radio buttons in a group must have the same name) </li></ul>
    84. 84. TEXTAREA <ul><li><form action=&quot;fred.php&quot; method=&quot;post&quot;> </li></ul><ul><li><textarea name=&quot; aTextArea &quot; </li></ul><ul><li>rows=&quot; 2 &quot; </li></ul><ul><li>cols=&quot; 20 &quot;> </li></ul><ul><li></textarea> </li></ul><ul><li></form> </li></ul>
    85. 85. SELECT control (drop down menu) <ul><li><form action=&quot;fred.php&quot; method=&quot;post&quot;> </li></ul><ul><li><select size=&quot; 1 &quot; name=&quot; aName &quot;> </li></ul><ul><li><option value=&quot; v1 &quot;> Choice one </option> </li></ul><ul><li><option value=&quot; v2 &quot;> Choice Two </option> </li></ul><ul><li><option value=&quot; v3 &quot; selected> </li></ul><ul><li>Choice Three </option> </li></ul><ul><li></select> </li></ul><ul><li></form> </li></ul><ul><li>(Size = 1 and not multiple makes it display as a drop down menu) </li></ul>
    86. 86. SELECT (list box) <ul><li><form action=&quot;fred.php&quot; method=&quot;post&quot;> </li></ul><ul><li><select size=&quot; 3 &quot; name=&quot; aName &quot;> </li></ul><ul><li><option value=&quot; v1 &quot;> Choice one </option> </li></ul><ul><li><option value=&quot; v2 &quot;> Choice Two </option> </li></ul><ul><li><option value=&quot; v3 &quot; selected=&quot; true &quot;> </li></ul><ul><li>Choice Three </option> </li></ul><ul><li></select> </li></ul><ul><li></form> </li></ul><ul><li>(size > 1, so options are displayed in a list; </li></ul><ul><li>size = number of options, so no scroll bar) </li></ul>
    87. 87. SELECT (list with scroll bar) <ul><li><form action=&quot;fred.php&quot; method=&quot;post&quot;> </li></ul><ul><li><select size=&quot; 2 &quot; name=&quot; aName &quot;> </li></ul><ul><li><option value=&quot; v1 &quot;> Choice one </option> </li></ul><ul><li><option value=&quot; v2 &quot;> Choice Two </option> </li></ul><ul><li><option value=&quot; v3 &quot; selected=&quot; true &quot;> </li></ul><ul><li>Choice Three </option> </li></ul><ul><li></select> </li></ul><ul><li></form> </li></ul><ul><li>(size > 1, so options are displayed in a list; </li></ul><ul><li>size < number of options, so scroll bar appears) </li></ul>
    88. 88. SELECT (multi-line) <ul><li><form action=&quot;fred.php&quot; method=&quot;post&quot;> </li></ul><ul><li><select size=&quot; 3 &quot; name=&quot; aName [] &quot; multiple=&quot; t &quot;> </li></ul><ul><li><option value=&quot; v1 &quot;> Choice one </option> </li></ul><ul><li><option value=&quot; v2 &quot;> Choice Two </option> </li></ul><ul><li><option value=&quot; v3 &quot; selected=&quot; true &quot;> </li></ul><ul><li>Choice Three </option> </li></ul><ul><li></select> </li></ul><ul><li></form> </li></ul><ul><li>(multiple, so options are displayed in a list. </li></ul><ul><li>Name must be an array so it can return all the values. </li></ul><ul><li>Hold down the shift key and click to select more than one option) </li></ul>
    89. 89. PHP Configuration <ul><li>Language Options </li></ul><ul><li>short_open_tag ( boolean ) </li></ul><ul><ul><li>Tells whether the short form (<? ?>) of PHP's open tag should be allowed. </li></ul></ul><ul><li>asp_tags ( boolean ) </li></ul><ul><ul><li>Enables the use of ASP-like <% %> tags in addition to the usual <?php ?> tags. This includes the variable-value printing shorthand of <%= $value %>. </li></ul></ul><ul><li>precision ( integer ) </li></ul><ul><ul><li>The number of significant digits displayed in floating point numbers. </li></ul></ul><ul><li>y2k_compliance( boolean ) </li></ul><ul><ul><li>Enforce year 2000 compliance </li></ul></ul>
    90. 90. PHP Configuration (cont..) <ul><li>Resource Limits </li></ul><ul><li>memory_limit integer </li></ul><ul><ul><li>This sets the maximum amount of memory in bytes that a script is allowed to allocate. This helps prevent poorly written scripts for eating up all available memory on a server. Note that to have no memory limit, set this directive to -1 . </li></ul></ul>
    91. 91. PHP Configuration (cont..) <ul><li>Data Handling </li></ul><ul><li>track_vars boolean </li></ul><ul><ul><li>If enabled, then Environment, GET, POST, Cookie, and Server variables can be found in the global associative arrays $_ENV, $_GET, $_POST, $_COOKIE, and $_SERVER. </li></ul></ul><ul><li>register_globals boolean </li></ul><ul><ul><li>Whether or not to register the EGPCS (Environment, GET, POST, Cookie, Server) variables as global variables. </li></ul></ul><ul><li>post_max_size integer </li></ul><ul><ul><li>Sets max size of post data allowed. This setting also affects file upload. To upload large files, this value must be larger than upload_max_filesize. </li></ul></ul>
    92. 92. PHP Configuration (cont..) <ul><li>Paths and Directories </li></ul><ul><li>include_path string </li></ul><ul><ul><li>Specifies a list of directories where the require() , include() , fopen () , file() , readfile () and file_get_contents () functions look for files. </li></ul></ul><ul><li>user_dir string </li></ul><ul><ul><li>The base name of the directory used on a user's home directory for PHP files, for example public_html . </li></ul></ul><ul><li>extension_dir string </li></ul><ul><ul><li>In what directory PHP should look for dynamically loadable extensions. </li></ul></ul><ul><li>extension string </li></ul><ul><ul><li>Which dynamically loadable extensions to load when PHP starts up. </li></ul></ul>
    93. 93. PHP Configuration (cont..) <ul><li>File Uploads </li></ul><ul><li>file_uploads boolean </li></ul><ul><ul><li>Whether or not to allow HTTP file uploads . </li></ul></ul><ul><li>upload_tmp_dir string </li></ul><ul><ul><li>The temporary directory used for storing files when doing file upload. Must be writable by whatever user PHP is running as. If not specified PHP will use the system's default. </li></ul></ul><ul><li>upload_max_filesize integer </li></ul><ul><ul><li>The maximum size of an uploaded file. </li></ul></ul>
    94. 94. PHP Cookies and Sessions
    95. 95. Stateful & Stateless <ul><li>Stateful </li></ul><ul><ul><li>Stateful means the computer or program keeps track of the state of interaction, usually by setting values in a storage field designated for that purpose. </li></ul></ul><ul><li>Stateless </li></ul><ul><ul><li>Stateless means there is no record of previous interactions and each interaction request has to be handled based entirely on information that comes with it. </li></ul></ul>
    96. 96. HTTP is stateless <ul><li>The World Wide Web is intrinsically stateless </li></ul><ul><li>Each request from a user for a Web page/ URL results in the requested pages being served </li></ul><ul><li>The Web (HTTP) server never remembers the request later. </li></ul><ul><li>There is no recorded continuity. </li></ul><ul><li>Each communication is discrete and unrelated to those that precede or follow. </li></ul>
    97. 97. Methods for maintaining state <ul><ul><ul><li>Cookies </li></ul></ul></ul><ul><ul><ul><li>Sessions </li></ul></ul></ul><ul><ul><ul><li>Passing [hidden] variables </li></ul></ul></ul>
    98. 98. What is a cookie? <ul><li>Cookies are simple text strings of the form of name=value </li></ul><ul><li>Cookies are stored persistently on the client’s machine and not on the server. </li></ul><ul><li>A URL is stored with each cookie and it is used by the browser to determine whether it should send the cookie to the web server. </li></ul>
    99. 99. How to Set a Cookie <ul><li>PHP provides setcookie() function to set a cookie. </li></ul><ul><li>This function should be called before <html> tag. </li></ul><ul><li>For each cookie this function has to be called separately. </li></ul><ul><li>Format of the function is </li></ul><ul><li>setcookie(name, value, expire, path, domain, security); </li></ul>
    100. 100. Arguments of setcookie() <ul><li>Name - This sets the name of the cookie and is stored in an environment variable called HTTP_COOKIE_VARS. This variable is used while accessing cookies. (Ex. “user”) </li></ul><ul><li>Value -This sets the value of the named variable and is the content that you actually want to store. (Eg. “Raju”) </li></ul><ul><li>Expiry - This specify a future time in seconds since 00:00:00 GMT on 1st Jan 1970. After this time cookie will become inaccessible. If this parameter is not set then cookie will automatically expire when the Web Browser is closed. (Eg. time()+60*60*24*7) </li></ul><ul><li>Path -This specifies the directories for which the cookie is valid. A single forward slash character permits the cookie to be valid for all directories and files on the web server. If a specific directory is named, this cookie is valid only for the pages within that directory (Eg. “/”) </li></ul><ul><li>Domain - This can be used to specify the domain name in very large domains and must contain at least two periods to be valid. All cookies are only valid for the host and domain which created them. (Eg. </li></ul><ul><li>Security - This can be set to 1 to specify that the cookie should only be sent by secure transmission using HTTPS otherwise set to 0 which mean cookie can be sent by regular HTTP. (Eg. 0 or 1) </li></ul>
    101. 101. Cookie Example <ul><ul><ul><li><?php </li></ul></ul></ul><ul><ul><ul><li>setcookie(“user”,”Raju”); </li></ul></ul></ul><ul><ul><ul><li>setcookie(“user&quot;, “nic&quot;, time()+3600); </li></ul></ul></ul><ul><ul><ul><li>setcookie(“user&quot;, “nic&quot;, time()+3600, &quot;/&quot;,&quot;&quot;, 0); </li></ul></ul></ul><ul><ul><ul><li>?> </li></ul></ul></ul><ul><ul><ul><li><html><head> </li></ul></ul></ul><ul><ul><ul><li><title> Cookie Example </title></head> </li></ul></ul></ul><ul><ul><ul><li><body> </li></ul></ul></ul><ul><ul><ul><li><?php </li></ul></ul></ul><ul><ul><ul><ul><li>echo “The cookie is set for variable ‘user’“; </li></ul></ul></ul></ul><ul><ul><ul><ul><li>?> </body></html> </li></ul></ul></ul></ul>
    102. 102. How to use a Cookie <ul><li><html> </li></ul><ul><ul><ul><li><head> </li></ul></ul></ul><ul><ul><ul><li><title>Using cookies in PHP</title> </li></ul></ul></ul><ul><ul><ul><li></head> </li></ul></ul></ul><ul><ul><ul><li><body> </li></ul></ul></ul><ul><ul><ul><li><?php </li></ul></ul></ul><ul><ul><ul><ul><ul><li>//Display the value of ‘user’ </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><ul><li>echo $_COOKIE[“user&quot;]. &quot;<br />&quot;; </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><ul><li>//This is another way </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><ul><li>echo $HTTP_COOKIE_VARS[“user&quot;]. &quot;<br />&quot;; </li></ul></ul></ul></ul></ul><ul><ul><ul><li>?></body></html> </li></ul></ul></ul>
    103. 103. How to check whether a cookie is set - isset() <ul><ul><ul><li><html> </li></ul></ul></ul><ul><ul><ul><li><head> </li></ul></ul></ul><ul><ul><ul><li><title>Checking whether a cookie is set</title> </li></ul></ul></ul><ul><ul><ul><li></head> </li></ul></ul></ul><ul><ul><ul><li><body> </li></ul></ul></ul><ul><ul><ul><li><?php </li></ul></ul></ul><ul><ul><ul><ul><ul><li>// Using isset() function </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><ul><li>if( isset($_COOKIE[“user&quot;])) </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><ul><li>echo “Hello &quot; . $_COOKIE[“user&quot;] . &quot;<br />&quot;; </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><ul><li>else </li></ul></ul></ul></ul></ul><ul><ul><ul><ul><ul><li>echo “Unknown user&quot; . &quot;<br />&quot;; </li></ul></ul></ul></ul></ul><ul><ul><ul><li>?> </li></ul></ul></ul><ul><ul><ul><li></body></html> </li></ul></ul></ul>
    104. 104. How to Erase a Cookie <ul><li>Set the cookie's expiration date to some time in the past. </li></ul><ul><ul><ul><li><?php </li></ul></ul></ul><ul><ul><ul><li>setcookie( “user&quot;, &quot;&quot;, time()- 3600, &quot;/&quot;,&quot;&quot;, 0); </li></ul></ul></ul><ul><ul><ul><li>?> </li></ul></ul></ul><ul><ul><ul><li><html><head> </li></ul></ul></ul><ul><ul><ul><li><title>Erasing Cookies</title></head><body> </li></ul></ul></ul><ul><ul><ul><li><?php </li></ul></ul></ul><ul><ul><ul><li>echo “The cookie is erased“; </li></ul></ul></ul><ul><ul><ul><li>echo “The user is :”. $_COOKIE[“user&quot;] . &quot;<br />&quot;; </li></ul></ul></ul><ul><ul><ul><li>?></body></html> </li></ul></ul></ul>
    105. 105. Limitations of Cookies <ul><li>Can’t call setCookie() after output has been sent to the browser </li></ul><ul><li>Can’t have more than 20 cookies/server </li></ul><ul><li>Cookies ONLY persist until the browser closes UNLESS you specify an expiry date </li></ul><ul><li>Restrictions on use of cookies </li></ul>
    106. 106. Benefit of Cookies <ul><li>Cookies are used for authenticating, tracking, and maintaining specific information about users </li></ul><ul><li>Personolised home pages </li></ul><ul><li>Electronic shopping carts. </li></ul>
    107. 107. Browser settings <ul><li>Most modern browsers support cookies. </li></ul><ul><li>A user can choose whether cookies should be used or not. The following are common options: </li></ul><ul><ul><li>cookies are never accepted, </li></ul></ul><ul><ul><li>the browser asks the user whether to accept every individual cookie or allows them to generate a white list of acceptable sites, or </li></ul></ul><ul><ul><li>cookies are always accepted, or </li></ul></ul><ul><ul><li>cookies are accepted, except from sites specified by the user, generating a black list for cookie </li></ul></ul>
    108. 108. Why use sessions <ul><li>A normal HTML website will not pass data from one page to another </li></ul><ul><li>All information is forgotten when a new page is loaded </li></ul><ul><li>Many websites need to pass user data from one page to another </li></ul><ul><ul><ul><li>for tasks like a shopping cart, which requires data(the user's selected product) to be remembered from one page to the next </li></ul></ul></ul><ul><li>Using PHP sessions is one solution. </li></ul>
    109. 109. PHP Sessions - Overview <ul><li>An alternative way to make data accessible across the various pages of an entire website is to use a PHP Session. </li></ul><ul><li>A series of related interactions between a single client and the web server, over an extended period of time. </li></ul><ul><li>A session creates a file in a temporary directory on the server where registered session variables and their values are stored. This data will be available to all pages on the site during that visit. </li></ul><ul><li>A session ends when the user loses the browser or after leaving the site, the server will terminate the session after a predetermined period of time, commonly 30 minutes duration. </li></ul><ul><li>Sessions are used to create multi page forms and store user authentication information across pages and persistent user preferences on a site. </li></ul>
    110. 110. Session IDs <ul><li>Sessions work by creating a unique identification(UID) number for each visitor and storing variables based on this ID. </li></ul><ul><li>Session Identifier is known as Session Id (SID) </li></ul><ul><li>It is a special variable for any particular session. </li></ul><ul><li>It is a Unique ID. </li></ul>
    111. 111. Starting a PHP Session <ul><li>Before storing user information in a PHP session, a session is to be started using session_start() function </li></ul><ul><li>This must be at the very beginning of the code, before any HTML or text is sent. </li></ul><ul><li>This function first checks if a session is already started and if none is started then it starts one </li></ul><ul><li>Session variables are stored in an associative array called $_SESSION[]. These variables can be accessed during lifetime of a session. </li></ul>
    112. 112. Sample Session Start Code <ul><li><?php </li></ul><ul><li>session_start(); // start up your PHP session! </li></ul><ul><li>?> </li></ul><ul><li>This tiny piece of code will register the user's session with the server </li></ul><ul><li>Allow you to start saving user information </li></ul><ul><li>Assign a session ID (SID) for that user's session. </li></ul>
    113. 113. Showing Session ID <ul><li>Whenever a session is created a unique session ID is created. </li></ul><ul><li>The function session_id() displays the SID of the current session </li></ul><ul><ul><ul><li><?php </li></ul></ul></ul><ul><ul><ul><li>//showing session id </li></ul></ul></ul><ul><ul><ul><li>session_start(); </li></ul></ul></ul><ul><ul><ul><li>echo &quot;Current Session ID = <b>&quot;.session_id().&quot;</b>&quot;; </li></ul></ul></ul><ul><ul><ul><li>?> </li></ul></ul></ul>
    114. 114. Store and Display Session Variable <ul><li>When you want to store user data in a session use the $_SESSION associative array. </li></ul><ul><li>This is where you both store and retrieve session data. </li></ul><ul><ul><ul><ul><li><?php </li></ul></ul></ul></ul><ul><ul><ul><ul><li>session_start(); </li></ul></ul></ul></ul><ul><ul><ul><ul><li>$_SESSION['views'] = 1; // store session data </li></ul></ul></ul></ul><ul><ul><ul><ul><li>echo &quot;Pageviews = &quot;. $_SESSION['views'] ; //retrieve data </li></ul></ul></ul></ul><ul><ul><ul><ul><li>?> </li></ul></ul></ul></ul><ul><ul><ul><ul><li>Display: </li></ul></ul></ul></ul><ul><ul><ul><ul><li>Pageviews = 1 </li></ul></ul></ul></ul>
    115. 115. Seeing the session variables on the Server <ul><li>The session variables are stored on the web server </li></ul><ul><li>The Path is /var/lib/php/session </li></ul><ul><li>The file name is the session ID </li></ul><ul><li>Eg. </li></ul><ul><li>sess_076opqrstu56kldr670ndft0op </li></ul>
    116. 116. Using PHP's isset() Function <ul><li>Before using a session variable, check if it exists already </li></ul><ul><li>isset is a function that takes any variable you want to use and checks to see if it has been set . </li></ul><ul><li>That is, whether it has already been assigned a value. </li></ul>
    117. 117. Isset() ..Sample Code <ul><li><?php </li></ul><ul><ul><ul><li>session_start(); </li></ul></ul></ul><ul><ul><ul><li>if( isset($_SESSION['views']) ) </li></ul></ul></ul><ul><ul><ul><li> $_SESSION['views'] = $_SESSION['views']+ 1; </li></ul></ul></ul><ul><ul><ul><li>else </li></ul></ul></ul><ul><ul><ul><li>$_SESSION['views'] = 1;  </li></ul></ul></ul><ul><ul><ul><li>echo &quot;views = &quot;. $_SESSION['views']; </li></ul></ul></ul><ul><li>?> </li></ul><ul><ul><li>we can create a very simple pageview counter </li></ul></ul><ul><ul><li>use isset to check if the pageview variable exists. </li></ul></ul><ul><ul><li>If yes, increment the counter.If it doesn't exist create a pageview counter and set it to one. </li></ul></ul>
    118. 118. Passing Session Variables <ul><li>Sessions are most useful to create multi page forms and store user authentication information across pages </li></ul><ul><li>This require session ID to be passed across web pages. </li></ul><ul><li>This can be done through the URL </li></ul>
    119. 119. Clearing a Session Variable <ul><li>To destroy a single session variable, use unset() function to unset a session variable. </li></ul><ul><ul><ul><ul><li><?php </li></ul></ul></ul></ul><ul><ul><ul><ul><li>unset($_SESSION[‘views']); </li></ul></ul></ul></ul><ul><ul><ul><ul><li>?> </li></ul></ul></ul></ul>
    120. 120. Destroying a Session <ul><li>A PHP session can be destroyed by session_destroy() function. This function does not need any argument and a single call can destroy all the session variables. </li></ul><ul><ul><ul><ul><li><?php </li></ul></ul></ul></ul><ul><ul><ul><ul><li>session_destroy(); </li></ul></ul></ul></ul><ul><ul><ul><ul><li>?> </li></ul></ul></ul></ul>
    121. 121. String Manipulation <ul><li>String format : </li></ul><ul><ul><li>Trimming: chop(), ltrim(), trim() </li></ul></ul><ul><ul><li>nl2br(), htmlspecialchars () </li></ul></ul><ul><ul><li>printf(): printf(“the price is %s.”,$total) </li></ul></ul><ul><ul><li>Case: strtoupper(), strtolower(), ucfirst(), ucwords() </li></ul></ul><ul><ul><li>Storage: AddSlashes(), StripSlashes () </li></ul></ul><ul><ul><li>Others: strlen(); </li></ul></ul><ul><li>Join and Split </li></ul><ul><ul><li>explode(), implode()/join(), strtok(), substr() </li></ul></ul><ul><ul><ul><li>Examples </li></ul></ul></ul><ul><ul><ul><ul><li>$email_array = explode (‘@’,$email); </li></ul></ul></ul></ul><ul><ul><ul><ul><li>$new_email = implode (‘@’,$email_array); </li></ul></ul></ul></ul><ul><ul><ul><ul><li>$token = strtok ($feedback,” “);//get one once a time </li></ul></ul></ul></ul><ul><ul><ul><ul><li>substr ( string astring, int start, [, int length]); </li></ul></ul></ul></ul>
    122. 122. String Manipulation (cont’) <ul><li>Match and Replace </li></ul><ul><ul><li>Finding: strstr()/strchr(), stristr(), strrchar(), </li></ul></ul><ul><ul><ul><li>Examples: </li></ul></ul></ul><ul><ul><ul><li>if( strstr ($email, “”)) { ..} </li></ul></ul></ul><ul><ul><ul><li>//stristr() ignore cases, strrchar() reverse </li></ul></ul></ul><ul><ul><li>Position: strpos(),strrpos(); </li></ul></ul><ul><ul><ul><li>Examples: </li></ul></ul></ul><ul><ul><ul><li>Echo strpos($email, “unc”); </li></ul></ul></ul><ul><ul><li>Replacing: str_replace(), substr_replace(); </li></ul></ul><ul><ul><ul><li>Examples: str_replace(“unc”, “ils.unc”,$email) </li></ul></ul></ul><ul><ul><li>Regular expressions: </li></ul></ul><ul><ul><ul><li> ^ $ . | () * + {} </li></ul></ul></ul><ul><ul><ul><li>ereg(string pattern, string string, array [matches]) . eregi </li></ul></ul></ul><ul><ul><ul><li>// if string contains pattern, the matched substring is stored in matches array </li></ul></ul></ul><ul><ul><ul><li>ereg_replace(string pattern, string replacement, string string); </li></ul></ul></ul><ul><ul><ul><li>array split(string pattern, string string, int [max]) </li></ul></ul></ul>
    123. 123. Sending HTML email <ul><li><?php // multiple recipients $to  = '' . ', '; // note the comma $to .= ''; // subject $subject = 'Birthday Reminders for August'; // message $message = ' <html> <head>   <title>Birthday Reminders for August</title> </head> <body>   <p>Here are the birthdays upcoming in August!</p>   <table>     <tr>       ………    </tr>   </table> </body> </html>'; </li></ul><ul><li>// To send HTML mail, the Content-type header must be set $ headers  = 'MIME-Version: 1.0' . &quot; &quot;; $headers .= 'Content-type: text/html; charset=iso-8859-1' . &quot; &quot;; // Additional headers $headers .= 'To: Mary <>, Kelly <>' . &quot; &quot;; $headers .= 'From: Birthday Reminder <>' . &quot; &quot;; $headers .= 'Cc:' . &quot; &quot;; $headers .= 'Bcc:' . &quot; &quot;; // Mail it mail($to, $subject, $message, $headers); ?> </li></ul>
    124. 124. Reading file with PHP <ul><li>Opening a file and reading it with fgets </li></ul><ul><li><?php </li></ul><ul><ul><li>$file_handle = fopen(&quot;myfile&quot;, &quot;r&quot;); </li></ul></ul><ul><ul><li>while (!feof($file_handle)) { </li></ul></ul><ul><ul><li>$line = fgets($file_handle); </li></ul></ul><ul><ul><li>echo $line; </li></ul></ul><ul><li>} </li></ul><ul><ul><li>fclose($file_handle); </li></ul></ul><ul><li>?> </li></ul>
    125. 125. Simple Page Counter <ul><ul><ul><li><?php </li></ul></ul></ul><ul><ul><ul><li>$COUNTER_FILE = “webcounter.txt&quot;; </li></ul></ul></ul><ul><ul><ul><li>if (file_exists($COUNTER_FILE)) { </li></ul></ul></ul><ul><ul><ul><li>$fp = fopen(&quot;$COUNTER_FILE&quot;, &quot;r+&quot;); </li></ul></ul></ul><ul><ul><ul><li>flock($fp, 1); </li></ul></ul></ul><ul><ul><ul><li>$hits = fgets($fp, 4096); </li></ul></ul></ul><ul><ul><ul><li>$hits += 1; </li></ul></ul></ul><ul><ul><ul><li>fseek($fp,0); </li></ul></ul></ul><ul><ul><ul><li>fputs($fp, $hits); </li></ul></ul></ul><ul><ul><ul><li>flock($fp, 3); </li></ul></ul></ul><ul><ul><ul><li>fclose($fp); </li></ul></ul></ul><ul><ul><ul><li>} </li></ul></ul></ul><ul><ul><ul><li>?> </li></ul></ul></ul>
    126. 126. Handling file uploads <ul><li>For uploading a file we need a form for the user to enter the file name or browse their computer and select a file. The input type=&quot;file&quot; is used for that purpose. </li></ul><form method=&quot;post&quot; enctype=&quot;multipart/form-data&quot; > <table width=&quot;350&quot; border=&quot;0&quot; cellpadding=&quot;1&quot; cellspacing=&quot;1&quot; class=&quot;box&quot;> <tr> <td width=&quot;246&quot;> <input type=&quot;hidden&quot; name=&quot;MAX_FILE_SIZE&quot; value=&quot;2000000&quot;> <input name=&quot;userfile&quot; type=&quot;file&quot; id=&quot;userfile&quot;> </td> <td width=&quot;80&quot;><input name=&quot;upload&quot; type=&quot;submit&quot; class=&quot;box&quot; id=&quot;upload&quot; value=&quot; Upload &quot;></td> </tr> </table> </form>
    127. 127. Handling file uploads (Cont..) <ul><li>After the form is submitted the we need to read the autoglobal $_FILES. In the example above the input name for the file is userfile so the content of $_FILES are like this : </li></ul><ul><ul><li>$_FILES['userfile']['name'] The original name of the file on the client machine. </li></ul></ul><ul><ul><li>$_FILES['userfile']['type'] The mime type of the file, if the browser provided this information. An example would be &quot;image/gif&quot;. </li></ul></ul><ul><ul><li>$_FILES['userfile']['size'] The size, in bytes, of the uploaded file. </li></ul></ul><ul><ul><li>$_FILES['userfile']['tmp_name'] The temporary filename of the file in which the uploaded file was stored on the server. </li></ul></ul><ul><ul><li>$_FILES['userfile']['error'] The error code associated with this file upload. ['error'] was added in PHP 4.2.0 </li></ul></ul>
    128. 128. Handling file uploads (Cont..) <ul><li><?php // In PHP versions earlier than 4.1.0, $HTTP_POST_FILES should be used instead // of $_FILES. $uploaddir = '/var/www/uploads/'; $uploadfile = $uploaddir . basename($_FILES['userfile']['name']); echo '<pre>'; if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {    echo &quot;File is valid, and was successfully uploaded. &quot;; } else {    echo &quot;Possible file upload attack! &quot;; } ?> </li></ul>
    129. 129. Introduction To Database <ul><li>Why Databases? </li></ul><ul><ul><li>Databases are the preferred method of storage for large multi-user applications. </li></ul></ul><ul><ul><li>Databases enhance applications, e.g. Web. </li></ul></ul><ul><ul><li>Databases introduce interesting ideas. </li></ul></ul><ul><li>A relational database </li></ul><ul><ul><li>It splits the data into a series of tables, linked by key elements that are common between tables. </li></ul></ul><ul><ul><li>It provides storage efficiency because data do not need to be duplicated between records. </li></ul></ul>
    130. 130. DBMS <ul><li>A database management system ( DBMS ) is a system, usually automated and computerized, used to manage any collection of compatible, and ideally normalized data. </li></ul><ul><li>The DBMS manages storage and access to the data, and provides a query language to specify actions associated with the data. The standard query language is SQL. </li></ul><ul><li>A DBMS generally provides a client application that can be used to directly manipulate the data, as well as a programmable interface. MySQL provides a set of command line clients, as well as a program interface. </li></ul>
    131. 131. Relational Databases <ul><li>Information is stored in tables </li></ul><ul><ul><li>Tables store information about entities </li></ul></ul><ul><ul><li>Entities have characteristics called attributes </li></ul></ul><ul><ul><li>Each row in a table represents a single entity </li></ul></ul><ul><ul><ul><li>Each row is a set of attribute values </li></ul></ul></ul><ul><ul><ul><li>Every row must be unique, identified by a key </li></ul></ul></ul><ul><ul><li>Relationships -- associations among the data values are stored </li></ul></ul>Table structure = schema Table contents = instance
    132. 132. A Table in a Database <ul><li>Tables have names, attributes {fields}, entities {rows} </li></ul>Schema for Example table : ID number unique number(Key) Last text person’s last name First text person’s first name JobCode number current position Hire date first day on job ... instance schema
    133. 133. Relationships
    134. 134. MySQL Introduction <ul><li>MySQL is a database management system </li></ul><ul><li>SQL stands for the Structured Query Language. It defines how to insert, retrieve, modify and delete data </li></ul><ul><li>Free from </li></ul><ul><li>Reference sites </li></ul><ul><ul><li>NASA, Yahoo!, Compaq, Motorola </li></ul></ul>
    135. 135. Basic MySQL Operations <ul><li>Create table </li></ul><ul><li>Insert records </li></ul><ul><li>Load data </li></ul><ul><li>Retrieve records </li></ul><ul><li>Update records </li></ul><ul><li>Delete records </li></ul><ul><li>Modify table </li></ul><ul><li>Join table </li></ul><ul><li>Drop table </li></ul><ul><li>Optimize table </li></ul><ul><li>Count, Like, Order by, Group by </li></ul><ul><li>More advanced ones (sub-queries, stored procedures, triggers, views …) </li></ul>
    136. 136. How MySQL stores data (by default) <ul><li>A MySQL server can store several databases </li></ul><ul><li>Databases are stored as directories </li></ul><ul><ul><li>Default is at /usr/local/mysql/var/ </li></ul></ul><ul><li>Tables are stored as files inside each database (directory) </li></ul><ul><li>For each table, it has three files: </li></ul><ul><ul><li>table.FRM file containing information about the table structure </li></ul></ul><ul><ul><li>table.MYD file containing the row data </li></ul></ul><ul><ul><li>table.MYI containing any indexes belonging with this table, as well as some statistics about the table. </li></ul></ul>
    137. 137. <ul><li>Table structure for following examples: </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) )
    138. 138. <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
    139. 139. <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
    140. 140. <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
    141. 141. <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
    142. 142. <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
    143. 143. Layered Architecture Database Web Browser Client Hardware Server Hardware Server-side Programming Interchange Language Client-side Programming (PC, Unix) (MySQL) (PHP) (HTML, XML) (JavaScript) (IE, Firefox) (PC) Business rules Interaction Design Interface Design
    144. 144. Accessing MySQL <ul><li>Steps in querying a MySQL database </li></ul><ul><ul><li>Set up a connection </li></ul></ul><ul><ul><ul><li>@ $db=mysql_pconnect(‘’, ’webdb_24’ , ’webdb_24’ ); </li></ul></ul></ul><ul><ul><ul><li>if(!$db){..} </li></ul></ul></ul><ul><ul><li>Choose a database </li></ul></ul><ul><ul><ul><li>mysql_select_db(‘webdb_24’); </li></ul></ul></ul><ul><ul><li>Query the database </li></ul></ul><ul><ul><ul><li>$query=“select * from accounts”; </li></ul></ul></ul><ul><ul><ul><li>$result = mysql_query($query); </li></ul></ul></ul><ul><ul><li>Retrieve the query results </li></ul></ul><ul><ul><ul><li>$num_results=mysql_num_rows($result); </li></ul></ul></ul><ul><ul><ul><li>$row = mysql_fetch_array($result); </li></ul></ul></ul><ul><ul><ul><li>//return an associative array with the field name as the key and record as value. </li></ul></ul></ul><ul><ul><li>Free result and disconnect from the database </li></ul></ul><ul><ul><ul><li>mysql_free_result($result); </li></ul></ul></ul><ul><ul><ul><li>mysql_close($db); </li></ul></ul></ul>
    145. 145. Retrieve the Query Results <ul><li>Return an array: </li></ul><ul><ul><li>$row= mysql_fetch_array ($result); </li></ul></ul><ul><ul><li>echo $row[‘name’]; </li></ul></ul><ul><li>Return an enumerated array: </li></ul><ul><ul><li>$row= mysql_fetch_row ($result); </li></ul></ul><ul><ul><li>echo $row[0]; </li></ul></ul><ul><li>Return an object : </li></ul><ul><ul><li>$row = mysql_fetch_object ($result); </li></ul></ul><ul><ul><li>echo $row->name; </li></ul></ul>
    146. 146. Image Functions <ul><li>PHP is not limited to creating just HTML output. It can also be used to create and manipulate image files in a variety of different image formats, including gif, png, jpg, wbmp, and xpm. Even more convenient, PHP can output image streams directly to a browser. </li></ul>
    147. 147. Tricks and Tips <ul><li>Coding </li></ul><ul><ul><li>Prototype your web pages first </li></ul></ul><ul><ul><ul><li>Separate the design of the site from the coding </li></ul></ul></ul><ul><ul><li>Turn repetitive code into functions </li></ul></ul><ul><ul><ul><li>Makes for more maintainable and reusable code </li></ul></ul></ul><ul><ul><li>Turn grunt code into functions </li></ul></ul><ul><ul><ul><li>Database access, configuration file access </li></ul></ul></ul>
    148. 148. Tricks and Tips <ul><li>Debugging </li></ul><ul><ul><li>Feature: PHP is not a strongly typed language </li></ul></ul><ul><ul><ul><li>Variables can be created anywhere in your code </li></ul></ul></ul><ul><ul><li>Undocumented Feature: PHP is not a strongly typed language </li></ul></ul><ul><ul><ul><li>Typos in variable names will cause stuff to happen </li></ul></ul></ul>
    149. 149. Tricks and Tips <ul><li>Debugging </li></ul><ul><ul><li>Use scripts to dump form and session variables </li></ul></ul><ul><ul><ul><li>Write scripts to dump data to discover bad or missing data </li></ul></ul></ul>
    150. 150. Debugging <ul><li>Removing the ‘bugs’ that prevent things from working </li></ul><ul><li>Go through step-by-step by inserting echo statements </li></ul><ul><ul><li>Test parts </li></ul></ul><ul><ul><li>See what works </li></ul></ul><ul><ul><li>See where it stops working </li></ul></ul>
    151. 151. Debugging techniques <ul><li>If something was working, look at what was changed </li></ul><ul><ul><li>Keep multiple versions of your pages </li></ul></ul><ul><li>Show your team mates </li></ul><ul><ul><li>Explain how it works </li></ul></ul><ul><ul><li>They will see things you can’t </li></ul></ul>
    152. 152. .php Debugging <ul><li>Include headers and footers on you pages </li></ul><ul><li>Add echo statements to see where the error occurs </li></ul><ul><li>Isolate the problem systematically </li></ul><ul><li>Test your SQL using phpMyAdmin </li></ul><ul><li>Is your PHP code displayed? </li></ul><ul><ul><li>Check for both <% and %> </li></ul></ul><ul><ul><li>Check your .htaccess file </li></ul></ul>
    153. 153. PHP Debugging checklist <ul><li>Check for undefined variable names </li></ul><ul><ul><li>Must set variables before referencing them </li></ul></ul><ul><li>Check use of &quot;&quot; { } and [ ] </li></ul><ul><ul><li>Balanced and nested correctly? </li></ul></ul><ul><ul><ul><li>Use UltraEdit Match Brace ^B </li></ul></ul></ul><ul><ul><li>Appropriate grouping characters? </li></ul></ul><ul><li>Check the parameters for routines </li></ul><ul><ul><li>Right number of parameters? Defined? $? </li></ul></ul>
    154. 154. SQL Debugging checklist <ul><li>Can't connect to the database? Check your file </li></ul><ul><li>No ‘;’ at the end of your SQL within PHP </li></ul><ul><li>Check for the correct characters for </li></ul><ul><ul><li>Apostrophe ' </li></ul></ul><ul><ul><li>Quote &quot; </li></ul></ul><ul><ul><li>Not Word SmartQuotes </li></ul></ul>
    155. 155. SQL Debugging techniques <ul><li>Use phpMyAdmin to test SQL statements </li></ul><ul><li>Test complicated SQL statements in parts </li></ul><ul><li>For example: </li></ul><ul><ul><li>Three way Join, test each pair of tables </li></ul></ul><ul><ul><li>Select with Order By, test the select first </li></ul></ul><ul><li>Check for Null values in columns </li></ul><ul><li>Check on the number of rows returned </li></ul><ul><ul><li>May be zero, one, or more </li></ul></ul>
    156. 156. <ul><li>Thank you </li></ul>
    157. 157. <ul><li>Any Question </li></ul>?