PHP Presentation
Upcoming SlideShare
Loading in...5
×

Like this? Share it with your network

Share

PHP Presentation

  • 7,154 views
Uploaded on

Learn PHP installation and sample source codes

Learn PHP installation and sample source codes

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
No Downloads

Views

Total Views
7,154
On Slideshare
7,070
From Embeds
84
Number of Embeds
6

Actions

Shares
Downloads
303
Comments
0
Likes
6

Embeds 84

http://www.ustudy.in 46
http://jainank50.wordpress.com 19
http://jainank.wordpress.com 14
http://www.slideshare.net 3
https://www.mturk.com 1
http://ustudy.in 1

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. PHP INSTALLATION & SAMPLES -BY H. ANKUSH. JAIN
  • 2. WHAT IS PHP?
    • Hypertext Preprocessor
    • widely used, general-purpose scripting language
    • a server side scripting language used on the Internet to create dynamic web pages.
  • 3. INSTALLATION
  • 4. Install PHP 5 on Windows 1- Download the latest Windows PHP Binaries (MSI file)
  • 5. 2- Right click on the file and click “Install“.
  • 6. 3- Click “Next ” once the Welcome page is displayed.
  • 7. 4- Select “I accept the license agreement” and click “Next “.
  • 8. 5- Change your PHP installation path OR just accept the default path - C:Program FilesPHP and click “Next
  • 9. 6- On the Web Server Setup screen, select the Apache 2.2.x Module and click “Next
  • 10. 7-On the Apache Configuration Directory screen, browse for the Apache configuration directory (conf) OR just enter C:Program FilesApache Software FoundationApache2.2conf and click “Next ” to proceed with the installation. select-apache-webserver-configuration-directory
  • 11. 8- For now, accept the default selection and click “Next”.
  • 12. 9-Click “Install ” to install PHP 5 on Windows.
  • 13. 10- Click “Finish ” once completed. PHP 5 is successfully installed.
  • 14. 11- Start your Apache 2.2 server using the “Monitor Apache Servers” console (Start -> Apache HTTP Servers 2.2.4 -> Monitor Apache Servers ). You can see that PHP was successfully installed by checking the Apache status at the bottom of the console.
  • 15. Test your PHP 5 Installation
  • 16. 1- Open up your Windows Notepad. Type in “<?php phpinfo(); ?>” inside the file. Save this file as “info.php” inside your Apache root directory, C:Program FilesApache Software FoundationApache2.2htdocs .
  • 17. 2- Open up your web browser . In the address bar, type in your web server domain name plus info.php as the file name. Example: http://www.syahid.com/info.php . You should get a PHP configuration page just like the one below.
  • 18. Congratulations! You have successfully installed and test PHP 5 on Windows!
  • 19. PHP SAMPLES
  • 20. Sample 1: current date in an HTML page <html> <head> <title>Example #1 TDavid's Very First PHP Script ever!</title> </head> <? print(Date(&quot;l F d, Y&quot;)); ?> <body> </body> </html>
  • 21. 2.current month/day/date format <html> <head> <title>Example #3 TDavid's Very First PHP Script ever!</title> </head> <? print(Date(&quot;m/j/y&quot;)); ?> <body> </body> </html>
  • 22. 3. current month/day/date with HTML colors & formatting <html> <head> <title>PHP Script examples!</title> </head> <font face=&quot;Arial&quot; color=&quot;#FF00FF&quot;><strong><? print(Date(&quot;m/j/y&quot;)); ?> </strong></font> <body> </body> </html>
  • 23. 4. change background color based on day of the week using if else elseif statements <html> <head> <title>Background Colors change based on the day of the week</title> </head> <? $today = date(&quot;l&quot;); print(&quot;$today&quot;); if($today == &quot;Sunday&quot;) { $bgcolor = &quot;#FEF0C5&quot;; }
  • 24. 4. change background color based on day of the week using if else elseif statements elseif($today == &quot;Monday&quot;) { $bgcolor = &quot;#FFFFFF&quot;; } elseif($today == &quot;Tuesday&quot;) { $bgcolor = &quot;#FBFFC4&quot;; } elseif($today == &quot;Wednesday&quot;) { $bgcolor = &quot;#FFE0DD&quot;; } elseif($today == &quot;Thursday&quot;){ $bgcolor = &quot;#E6EDFF&quot;;}
  • 25. 4. change background color based on day of the week using if else elseif statements elseif($today == &quot;Friday&quot;) { $bgcolor = &quot;#E9FFE6&quot;; } else { // Since it is not any of the days above it must be Saturday $bgcolor = &quot;#F0F4F1&quot;; } print(&quot;<body bgcolor=&quot;$bgcolor&quot;> &quot;); ?> <br>This just changes the color of the screen based on the day of the week </body> </html>
  • 26. 4. change background color based on day of the week using array <html> <head> <title>Background Colors change based on the day of the week</title></head> <?$today = date(&quot;w&quot;); $bgcolor = array( &quot;#FEF0C5&quot;, &quot;#FFFFFF&quot;, &quot;#FBFFC4&quot;, &quot;#FFE0DD&quot;, &quot;#E6EDFF&quot;, &quot;#E9FFE6&quot;, &quot;#F0F4F1&quot; ); ?> <body bgcolor=&quot;<?print(&quot;$bgcolor[$today]&quot;);?>&quot;> <br>This just changes the color of the screen based on the day of the week </body> </html>
  • 27. 5. add date time stamp &quot;page last updated on..&quot; using filemtime function using forms, cookies, flat file databases, random number generation <html> <head> <title>Page last updated on month/date/year hour:min PHP Script</title> </head> <? $last_modified = filemtime(&quot;example7.php3&quot;); print(&quot;Last Modified &quot;); print(date(&quot;m/j/y h:i&quot;, $last_modified)); ?> </body> </html>
  • 28. 6. setting and retrieving a cookie <? $check = &quot;test&quot;; $check .= $filename; if ($test == $check) {print(&quot;<HTML><BODY>You have already voted. Thank you.</BODY></HTML>&quot;); } else{ $rated = &quot;test&quot;; $rated .= $filename; setcookie(test, $rated, time()+86400); print(&quot;<HTML><BODY><br>You haven't voted before so I recorded your vote</BODY></HTML>&quot;); } ?>
  • 29. 7. getting an average number using PHP <? $total_ratings = (3+2+3+1+5+2+3); $total_votes = 7; $average = $total_ratings / $total_votes; print(&quot;The Average Rating Is: $average&quot;); ?>
  • 30. 8. showing the surfer average vote statistics, using printf function <? $path = &quot;/YOUR/PATH/TO/public_html/php_diary/data&quot;; $filename = &quot;122499.dat&quot;; $x= -1; if($file = fopen(&quot;$path/$filename&quot;, &quot;r&quot;)) { while(!feof($file)) { $therate = fgetss($file, 255); $x++; $count = $count + $therate; } fclose($file); } $average = ($count / $x); print(&quot;Surfer Average Rating for 12/24/99: &quot;); printf(&quot;%.2f&quot;, $average); print(&quot;<br>Total Surfer Votes: $x&quot;); ?>
  • 31. 9. generating a rand number from 0 to 9 <? srand(time()); $random = (rand()%9); print(&quot;Random number between 0 and 9 is: $random&quot;); ?>
  • 32. 10. php simple slot machine - multiple rand number generation <? function slotnumber() { srand(time()); for ($i=0; $i < 3; $i++) { $random = (rand()%3); $slot[] = $random; } print(&quot;<td width=&quot;33%&quot;><center>$slot[0]</td>&quot;); print(&quot;<td width=&quot;33%&quot;><center>$slot[1]</td>&quot;); print(&quot;<td width=&quot;33%&quot;><center>$slot[2]</td>&quot;); </tr>
  • 33. 11. php simple slot machine - multiple rand number generation <tr> <td width=&quot;100%&quot; colspan=&quot;3&quot; bgcolor=&quot;#008080&quot;><form method=&quot;POST&quot; action=&quot;example13.php3&quot;><div align=&quot;center&quot;><center><p><input type=&quot;submit&quot; value=&quot;Spin!&quot;></p> </center></div> </form> </td> </tr> </table> </center></div>
  • 34. 12. random text link advertising using predefined arrays mail, regular expressions, password protection <? $random_url = array(&quot;http://www.tdscripts.com/linkorg.html&quot;, &quot;http://www.tdscripts.com/keno.html&quot;, &quot;http://www.tdscripts.com/ezibill.shtml&quot;, &quot;http://www.tdscripts.com/tdforum.shtml&quot;, &quot;http://www.tdscripts.com/picofday.html&quot;, &quot;http://www.tdscripts.com/gutsorglory.html&quot;);
  • 35. random text link advertising using predefined arrays mail, regular expressions, password protection $url_title = array(&quot;Link Organizer&quot;, &quot;TD Keno&quot;, &quot;eziBill *Free Promotion!&quot;, &quot;TD Forum&quot;, &quot;TD Pic of Day PHP&quot;, &quot;Guts or Glory Poker PHP&quot;); $url_desc = array(&quot;- A comprehensive link list organizer&quot;, &quot;- Offer your site visitors an engaging Keno game without the monetary risk&quot;, &quot;- Sell access to and protect your membership area using iBill and our eziBill script&quot;, &quot;- An unthreaded messageboard script to exchange ideas with your site visitors&quot;, &quot;- Run your own picture of the day script from any site anywhere with this handy script&quot;, &quot;- A casino-style card game written entirely in PHP&quot;);
  • 36. random text link advertising using predefined arrays mail, regular expressions, password protection srand(time()); $sizeof = count($random_url); $random = (rand()%$sizeof); print(&quot;<center><a href=&quot;$random_url[$random]&quot;>$url_title[$random]</a> $url_desc[$random]</center>&quot;); ?>
  • 37. 13. forcing the text in a string to be all upper or lowercase <? // force all uppercase print(strtoupper(&quot;i bet this will show up as all letters capitalized<br>&quot;)); // force all lowercase print(strtolower(&quot;I BET THIS WILL SHOW UP AS ALL LETTERS IN LOWERCASE<br>&quot;)); // force the first letter of a string to be capitalized print(ucfirst(&quot;i bet this will show the first letter of the string capitalized<br>&quot;)); // force the first letter of each WORD in a string to be capitalized print(ucwords(&quot;i bet this will show the first letter of every word capitalized<br>&quot;)); ?>
  • 38. 14. searching through meta tags for a search engine <HTML> <BODY> <? $all_meta = get_meta_tags(&quot;010600.php3&quot;); print($all_meta[&quot;description&quot;]); print(&quot;<br>&quot;); print($all_meta[&quot;keywords&quot;]); ?> </BODY> </HTML>
  • 39. 15. searching through a directory and picking out files using a regular expression <? $diary_directory = opendir(&quot;.&quot;); while($filename = readdir($diary_directory)) { $filesplit = explode(&quot;.&quot;, $filename); $check_filename = $filesplit[0]; if(ereg(&quot;[0-9]{6}&quot;, $check_filename)) { $check_filename .= &quot;.$filesplit[1]&quot;; $valid_filename[] = $check_filename; } } closedir($diary_directory); for($index = 0; $index < count($valid_filename); $index++) { print(&quot;$valid_filename[$index]<br>&quot;); } ?>
  • 40. 16. shuffling and &quot;cutting&quot; a deck of playing cards <? $cards = array(&quot;ah&quot;, &quot;ac&quot;, &quot;ad&quot;, &quot;as&quot;, &quot;2h&quot;, &quot;2c&quot;, &quot;2d&quot;, &quot;2s&quot;, &quot;3h&quot;, &quot;3c&quot;, &quot;3d&quot;, &quot;3s&quot;, &quot;4h&quot;, &quot;4c&quot;, &quot;4d&quot;, &quot;4s&quot;, &quot;5h&quot;, &quot;5c&quot;, &quot;5d&quot;, &quot;5s&quot;, &quot;6h&quot;, &quot;6c&quot;, &quot;6d&quot;, &quot;6s&quot;, &quot;7h&quot;, &quot;7c&quot;, &quot;7d&quot;, &quot;7s&quot;, &quot;8h&quot;, &quot;8c&quot;, &quot;8d&quot;, &quot;8s&quot;, &quot;9h&quot;, &quot;9c&quot;, &quot;9d&quot;, &quot;9s&quot;, &quot;th&quot;, &quot;tc&quot;, &quot;td&quot;, &quot;ts&quot;, &quot;jh&quot;, &quot;jc&quot;, &quot;jd&quot;, &quot;js&quot;, &quot;qh&quot;, &quot;qc&quot;, &quot;qd&quot;, &quot;qs&quot;, &quot;kh&quot;, &quot;kc&quot;, &quot;kd&quot;, &quot;ks&quot;);
  • 41. shuffling and &quot;cutting&quot; a deck of playing cards srand(time()); for($i = 0; $i < 52; $i++) { $count = count($cards); $random = (rand()%$count); if($cards[$random] == &quot;&quot;) { $i--; } else { $deck[] = $cards[$random]; $cards[$random] = &quot;&quot;; } }
  • 42. shuffling and &quot;cutting&quot; a deck of playing cards srand(time()); $starting_point = (rand()%51); print(&quot;Starting point for cut cards is: $starting_point<p>&quot;); // display shuffled cards (EXAMPLE ONLY) for ($index = 0; $index < 52; $index++) { if ($starting_point == 52) { $starting_point = 0; } print(&quot;Uncut Point: <strong>$deck[$index]</strong> &quot;); print(&quot;Starting Point: <strong>$deck[$starting_point]</strong><br>&quot;); $starting_point++; } ?>
  • 43. 17. Reader rated most useful diary entries in descending order <HTML> <HEAD> <title>Reader Rated Most Useful Entries</title> </HEAD> <BODY> <table border=&quot;0&quot; width=&quot;100%&quot;> <tr> <td width=&quot;6%&quot; bgcolor=&quot;#00FFFF&quot;><p align=&quot;center&quot;><small><font face=&quot;Verdana&quot;>Rank</font></small></td> <td width=&quot;7%&quot; bgcolor=&quot;#00FFFF&quot;><p align=&quot;center&quot;><small><font face=&quot;Verdana&quot;>Rating</font></small></td>
  • 44. Reader rated most useful diary entries in descending order <td width=&quot;11%&quot; bgcolor=&quot;#00FFFF&quot;><p align=&quot;center&quot;><small><font face=&quot;Verdana&quot;>Diary Date</font></small></td> <td width=&quot;76%&quot; bgcolor=&quot;#00FFFF&quot;><p align=&quot;left&quot;><small><font face=&quot;Verdana&quot;>Description of Diary Page</font></small> </td> <script language=&quot;php&quot;> $db = &quot;DATABASE NAME&quot;; $admin = &quot;MYSQL USER NAME&quot;; $adpass = &quot;MYSQL PASSWORD&quot;; $mysql_link = mysql_connect(&quot;localhost&quot;, $admin, $adpass); mysql_select_db($db, $mysql_link);
  • 45. Reader rated most useful diary entries in descending order $query = &quot;SELECT * FROM avg_tally ORDER BY average DESC&quot;; $result = mysql_query($query, $mysql_link); if(mysql_num_rows($result)) { $rank = 1; while($row = mysql_fetch_row($result)) { print(&quot;</tr><tr>&quot;); if($color == &quot;#D8DBFE&quot;) { $color = &quot;#A6ACFD&quot;; } else { $color = &quot;#D8DBFE&quot;; } print(&quot;<td width=&quot;6%&quot; bgcolor=&quot;$color&quot;><center><small>&quot;); print(&quot;<font face=&quot;Verdana&quot;>$rank</font></small></center></td>&quot;); print(&quot;<td width=&quot;7%&quot; bgcolor=&quot;$color&quot;><center><small>&quot;); print(&quot;<font face=&quot;Verdana&quot;><strong>$row[1]</strong></font></small></center></td>&quot;); print(&quot;<td width=&quot;11%&quot; bgcolor=&quot;$color&quot;><center><small>&quot;); $url = $row[2] . &quot;.php3&quot;;
  • 46. Reader rated most useful diary entries in descending order if(!file_exists($url)) { $url = $row[2] . &quot;.html&quot;; } print(&quot;<font face=&quot;Verdana&quot;><a href=&quot;$url&quot;>$row[2]</a></font></small></center></td>&quot;); print(&quot;<td width=&quot;76%&quot; bgcolor=&quot;$color&quot;><left><small>&quot;); print(&quot;<font face=&quot;Verdana&quot;>$row[3]</font></small></left></td>&quot;); $rank++; } } </script> </table> </BODY> </HTML>
  • 47. 18. creating a mySQL table using a PHP script <? $mysql_db = &quot;DATABASE NAME&quot;; $mysql_user = &quot;YOUR MYSQL USERNAME&quot;; $mysql_pass = &quot;YOUR MYSQL PASSWORD&quot;; $mysql_link = mysql_connect(&quot;localhost&quot;, $mysql_user, $mysql_pass); mysql_select_db($mysql_db, $mysql_link); $create_query = &quot;CREATE TABLE tds_counter ( COUNT_ID INT NOT NULL AUTO_INCREMENT, pagepath VARCHAR(250), impressions INT, reset_counter DATETIME, PRIMARY KEY (COUNT_ID) )&quot;;
  • 48. 19. creating a mySQL table using a PHP script mysql_query($create_query, $mysql_link); print(&quot;Table Creation for <b>tds_counter</b> successful!<p>&quot;); $insert = &quot;INSERT into tds_counter VALUES ( 0, '/php_diary/021901.php3', 0, SYSDATE() )&quot;; mysql_query($insert, $mysql_link); print(&quot;Inserted new counter successfully for this page: http://www.php-scripts.com/php_diary/021901.php3<p>&quot;); ?>
  • 49. 20. mySQL based counter script for multiple pagesAutomating manual tasks, date sorting, mktime() <? $db = &quot;DATABASE NAME&quot;; $admin = &quot;MYSQL USER NAME&quot;; $adpass = &quot;MYSQL PASSWORD&quot;; $mysql_link = mysql_connect(&quot;localhost&quot;, $admin, $adpass); mysql_select_db($db, $mysql_link); $result = mysql_query(&quot;SELECT impressions from tds_counter where COUNT_ID='$cid'&quot;, $mysql_link); if(mysql_num_rows($result)) { mysql_query(&quot;UPDATE tds_counter set impressions=impressions+1 where COUNT_ID='$cid'&quot;, $mysql_link); $row = mysql_fetch_row($result); if($inv != 1) { print(&quot;$row[0]&quot;); } }?>
  • 50. 21. Classify Email as Bounce (DSN) or Automated Reply <?php $bounce = new COM(&quot;Chilkat.Bounce&quot;); $success = $bounce->UnlockComponent('Anything for 30-day trial'); if ($success == false) { print 'Failed to unlock component' . &quot; &quot;; exit; } $email = new COM(&quot;Chilkat.Email&quot;);
  • 51. 21. Classify Email as Bounce (DSN) or Automated Reply // Load an email from a .eml file. // (This example loads an Email object from a .eml file, // but the object could have been read // directly from a POP3 or IMAP mail server using // Chilkat's POP3 or IMAP implementations.) $success = $email->LoadEml('sampleBounce.eml'); if ($success == false) { print $email->lastErrorText() . &quot; &quot;; exit; }
  • 52. 21. Classify Email as Bounce (DSN) or Automated Reply $success = $bounce->ExamineMail($email); if ($success == false) { print $bounce->lastErrorText() . &quot; &quot;; exit; } if ($bounce->BounceType == 1) { // Hard bounce, log the email address print 'Hard Bounce: ' . $bounce->bounceAddress() . &quot; &quot;; } if ($bounce->BounceType == 2) { // Soft bounce, log the email address print 'Soft Bounce: ' . $bounce->bounceAddress() . &quot; &quot;; }
  • 53. 21. Classify Email as Bounce (DSN) or Automated Reply if ($bounce->BounceType == 3) { // General bounce, no email address available. print 'General Bounce: No email address' . &quot; &quot;; } if ($bounce->BounceType == 4) { // General bounce, log the email address print 'General Bounce: ' . $bounce->bounceAddress() . &quot; &quot;; } if ($bounce->BounceType == 5) { // Mail blocked, log the email address print 'Mail Blocked: ' . $bounce->bounceAddress() . &quot; &quot;; }
  • 54. 21. Classify Email as Bounce (DSN) or Automated Reply if ($bounce->BounceType == 6) { // Auto-reply, log the email address print 'Auto-Reply: ' . $bounce->bounceAddress() . &quot; &quot;; } if ($bounce->BounceType == 7) { // Transient (recoverable) Failure, log the email address print 'Transient Failure: ' . $bounce->bounceAddress() . &quot; &quot;; } if ($bounce->BounceType == 8) { // Subscribe request, log the email address print 'Subscribe Request: ' . $bounce->bounceAddress() . &quot; &quot;; } if ($bounce->BounceType == 9) { // Unsubscribe Request, log the email address print 'Unsubscribe Request: ' . $bounce->bounceAddress() . &quot; &quot;; }
  • 55. 21. Classify Email as Bounce (DSN) or Automated Reply if ($bounce->BounceType == 10) { // Virus Notification, log the email address print 'Virus Notification: ' . $bounce->bounceAddress() . &quot; &quot;; } if ($bounce->BounceType == 11) { // Suspected bounce. // This should be rare. It indicates that the Bounce // component found strong evidence that this is a bounced // email, but couldn//t quite recognize everything it // needed to be 100% sure. Feel free to notify // support@chilkatsoft.com regarding emails having this // bounce type. print 'Suspected Bounce!' . &quot; &quot;;} ?>
  • 56. 22. program to print a message <html> <h3>My first PHP Program</h3> <? echo &quot;Hello world!&quot;; ?> </html>
  • 57. 23. PHP Array <?php $employee_names[0] = &quot;Dana&quot;; $employee_names[1] = &quot;Matt&quot;; $employee_names[2] = &quot;Susan&quot;; echo &quot;The first employee's name is &quot;.$employee_names[0]; echo &quot;<br>&quot;; echo &quot;The second employee's name is &quot;.$employee_names[1]; echo &quot;<br>&quot;; echo &quot;The third employee's name is &quot;.$employee_names[2]; ?>
  • 58. 24.File Upload Script <html> <head> <title>A File Upload Script</title> </head> <body> <div> <?php if ( isset( $_FILES['fupload'] ) ) { print &quot;name: &quot;. $_FILES['fupload']['name'] .&quot;<br />&quot;; print &quot;size: &quot;. $_FILES['fupload']['size'] .&quot; bytes<br />&quot;; print &quot;temp name: &quot;.$_FILES['fupload']['tmp_name'] .&quot;<br />&quot;; print &quot;type: &quot;. $_FILES['fupload']['type'] .&quot;<br />&quot;; print &quot;error: &quot;. $_FILES['fupload']['error'] .&quot;<br />&quot;; if ( $_FILES['fupload']['type'] == &quot;image/gif&quot; ) { $source = $_FILES['fupload']['tmp_name']; $target = &quot;upload/&quot;.$_FILES['fupload']['name'];
  • 59. 24.File Upload Script move_uploaded_file( $source, $target );// or die (&quot;Couldn't copy&quot;); $size = getImageSize( $target ); $imgstr = &quot;<p><img width=&quot;$size[0]&quot; height=&quot;$size[1]&quot; &quot;; $imgstr .= &quot;src=&quot;$target&quot; alt=&quot;uploaded image&quot; /></p>&quot;; print $imgstr; } } ?> </div> <form enctype=&quot;multipart/form-data&quot; action=&quot;<?php print $_SERVER['PHP_SELF']?>&quot; method=&quot;post&quot;> <p> <input type=&quot;hidden&quot; name=&quot;MAX_FILE_SIZE&quot; value=&quot;102400&quot; /> <input type=&quot;file&quot; name=&quot;fupload&quot; /><br/> <input type=&quot;submit&quot; value=&quot;upload!&quot; /> </p> </form> </body> </html>
  • 60. 25.Simple File Upload Form <html> <head> <title>A Simple File Upload Form</title> </head> <body> <form enctype=&quot;multipart/form-data&quot; action=&quot;<?print $_SERVER['PHP_SELF']?>&quot; method=&quot;post&quot;> <p> <input type=&quot;hidden&quot; name=&quot;MAX_FILE_SIZE&quot; value=&quot;102400&quot; /> <input type=&quot;file&quot; name=&quot;fupload&quot; /><br/> <input type=&quot;submit&quot; value=&quot;upload!&quot; /> </p> </form> </body> </html>
  • 61. 26.Creating an HTML Form That Accepts Mail-Related Information <html> <head> <title>Simple Send Mail Form</title> </head><body> <h1>Mail Form</h1> <form name=&quot;form1&quot; method=&quot;post&quot; action=&quot;SimpleEmail.php&quot;> <table> <tr><td><b>To</b></td><td><input type=&quot;text&quot; name=&quot;mailto&quot; size=&quot;35&quot;></td></tr> <tr><td><b>Subject</b></td> <td><input type=&quot;text&quot; name=&quot;mailsubject&quot; size=&quot;35&quot;></td></tr> <tr><td><b>Message</b></td> <td><textarea name=&quot;mailbody&quot; cols=&quot;50&quot; rows=&quot;7&quot;></textarea></td> </tr> <tr><td colspan=&quot;2&quot;> <input type=&quot;submit&quot; name=&quot;Submit&quot; value=&quot;Send&quot;> </td></tr></table></form></body> </html>
  • 62. 26.Creating an HTML Form That Accepts Mail-Related Information <!-- SimpleEmail.php <?php if (empty ($mailto) ) { die ( &quot;Recipient is blank! &quot;) ; } if (empty ($mailsubject) ){ $mailsubject=&quot; &quot; ; } if (empty ($mailbody) ) { $mailbody=&quot; &quot; ; } $result = mail ($mailto, $mailsubject, $mailbody) ; if ($result) { echo &quot;Email sent successfully!&quot; ; }else{ echo &quot;Email could not be sent.&quot; ; } ?>
  • 63. 27.React to Form action <HTML> <BODY> <FORM METHOD=&quot;POST&quot; ACTION=&quot;GetFormValue.php&quot;> <H2>Contact List</H2> <BR>Nickname: <BR><INPUT TYPE=&quot;TEXT&quot; NAME=&quot;Nickname&quot;> <BR> <BR>Full Name: <BR><INPUT TYPE=&quot;TEXT&quot; NAME=&quot;Fullname&quot;>
  • 64. 27.React to Form action <BR> <BR>Memo: <BR><TEXTAREA NAME=&quot;Memo&quot; ROWS=&quot;4&quot; COLS=&quot;40&quot; WRAP=&quot;PHYSICAL&quot;> </TEXTAREA> <BR> <BR> <INPUT TYPE=&quot;SUBMIT&quot;> </FORM> </BODY> <!-- GetFormValue.php <?php echo &quot;<BR>Nickname=$Nickname&quot;; echo &quot;<BR>Fullname=$Fullname&quot;; echo &quot;<BR>Memo=$Memo&quot;; ?>
  • 65. 28. Build query string based on form input <?php if (isset($_POST['submit'])) { $rowID = $_POST['id']; mysql_connect(&quot;mysql153.secureserver.net&quot;,&quot;java2s&quot;,&quot;password&quot;); mysql_select_db(&quot;java2s&quot;); $query = &quot;SELECT * FROM Employee WHERE ID='$id'&quot;; $result = mysql_query($query); list($name,$productid,$price,$description) = mysql_fetch_row($result); }
  • 66. 28. Build query string based on form input ?> <form action=&quot;<?php echo $_SERVER['PHP_SELF']; ?>&quot; method=&quot;post&quot;> <select name=&quot;id&quot;> <option name=&quot;&quot;>Choose a employee:</option> <option name=&quot;1&quot;>1</option> <option name=&quot;2&quot;>2</option> <option name=&quot;3&quot;>3</option> </select> <input type=&quot;submit&quot; name=&quot;submit&quot; value=&quot;Submit&quot; /> </form>
  • 67. 29. Checking input from a checkbox or a multiple select <?php $options = array('option 1', 'option 2', 'option 3'); $valid = true; if (is_array($_GET['input'])) { $valid = true; foreach($_GET['input'] as $input) { if (!in_array($input, $options)) { $valid = false; } } if ($valid) { //process input } } ?>
  • 68. 30. Validating a single checkbox <?php $value = 'yes'; echo &quot;<input type='checkbox' name='subscribe' value='yes'/> Subscribe?&quot;; if (isset($_POST['subscribe'])) { if ($_POST['subscribe'] == $value) { $subscribed = true; } else { $subscribed = false;
  • 69. 30. Validating a single checkbox print 'Invalid checkbox value submitted.'; } } else { $subscribed = false; } if ($subscribed) { print 'You are subscribed.'; } else { print 'You are not subscribed'; }
  • 70. 31. Deal with Array Form Data <HTML> <BODY> <FORM METHOD=&quot;POST&quot; ACTION=&quot;DealWithArrayFormData.php&quot;> <H1>Contact Information</H1> <TABLE><TR> <TD>Childrens' Names:</TD> </TR> <TR> <TD><INPUT TYPE=&quot;TEXT&quot; NAME=&quot;children[]&quot;></TD> </TR> <TR> <TD><INPUT TYPE=&quot;TEXT&quot; NAME=&quot;children[]&quot;></TD> </TR> <TR> <TD><INPUT TYPE=&quot;TEXT&quot; NAME=&quot;children[]&quot;></TD> </TR>
  • 71. 31. Deal with Array Form Data <TR> <TD><INPUT TYPE=&quot;TEXT&quot; NAME=&quot;children[]&quot;></TD> </TR> <TR> <TD><INPUT TYPE=&quot;TEXT&quot; NAME=&quot;children[]&quot;></TD> </TR> </TABLE> <BR> <BR> <BR> <INPUT TYPE=&quot;SUBMIT&quot; VALUE=&quot;Submit&quot;> <BR> <BR> <INPUT TYPE=&quot;RESET&quot; VALUE=&quot;Clear the Form&quot;> </FORM> </BODY> </HTML>
  • 72. 31. Deal with Array Form Data <!-- DealWithArrayFormData.php <?php foreach ($children as $index => $child){ echo &quot;<BR>child[$index]=$child&quot;; } echo &quot;<BR>&quot;; sort($children); foreach ($children as $index => $child){ echo &quot;<BR>child[$index]=$child&quot;; } ?> -->
  • 73. 32. A process_form() Function <?php function process_form($data) { $msg = &quot;The form at {$_SERVER['PHP_SELF']} was submitted with these values: &quot;; foreach($data as $key=>$val) { $msg .= &quot;$key => $val &quot;; } mail(&quot;joeuser@somewhere.com&quot;, &quot;form submission&quot;, $msg); } ?>
  • 74. 33. Access widget's form value <INPUT TYPE=&quot;text&quot; NAME=&quot;myform.email&quot;> would be accessed in PHP as the following: <?php echo $_GET['myform_email']; ?>
  • 75. 34. Accessing multiple submitted values <form method=&quot;POST&quot; action=&quot;index.php&quot;> <select name=&quot;lunch[]&quot; multiple> <option value=&quot;a&quot;>A</option> <option value=&quot;b&quot;>B</option> <option value=&quot;c&quot;>C</option> <option value=&quot;d&quot;>D</option> <option value=&quot;e&quot;>E</option> </select> <input type=&quot;submit&quot; name=&quot;submit&quot;> </form> Selected buns: <br/><?php foreach ($_POST['lunch'] as $choice) { print &quot;You want a $choice bun. <br/>&quot;; } ?>
  • 76. 35. Forms and PHP //index.htm <html> <body> <form action=&quot;index.php&quot; method=&quot;post&quot;> Your Name:<br> <input type=&quot;text&quot; name=&quot;name&quot; size=&quot;20&quot; maxlength=&quot;20&quot; value=&quot;&quot;><br> Your Email:<br> <input type=&quot;text&quot; name=&quot;email&quot; size=&quot;20&quot; maxlength=&quot;40&quot; value=&quot;&quot;><br> <input type=&quot;submit&quot; value=&quot;go!&quot;> </form> </body> </html>
  • 77. 35. Forms and PHP //index.php <html> <head> <title></title> </head> <body> <? print &quot;Hi, $name!. Your email address is $email&quot;; ?> </body> </html>
  • 78. 36. Handling Special Characters <html> <body> <?php if ($_POST['submitted'] == &quot;yes&quot;){ $yourname = $_POST['yourname']; $yourname = trim ($yourname); $yourname = strip_tags ($yourname); $yourname = htmlspecialchars ($yourname); $yourname = addslashes ($yourname); echo $yourname . &quot;<br />&quot;; ?><a href=&quot;index.php&quot;>Try Again</a><?php }
  • 79. 36. Handling Special Characters if ($_POST['submitted'] != &quot;yes&quot;){ ?> <form action=&quot;index.php&quot; method=&quot;post&quot;> <p>Example:</p> <input type=&quot;hidden&quot; name=&quot;submitted&quot; value=&quot;yes&quot; /> Your Name: <input type=&quot;text&quot; name=&quot;yourname&quot; maxlength=&quot;150&quot; /><br /> <input type=&quot;submit&quot; value=&quot;Submit&quot; style=&quot;margin-top: 10px;&quot; /> </form> <?php } ?> </div> </body> </html>
  • 80. 37. Saying &quot;Hello&quot; <? if (array_key_exists('my_name',$_POST)) { print &quot;Hello, &quot;. $_POST['my_name']; } else { print<<<_HTML_ <form method=&quot;post&quot; action=&quot;$_SERVER[PHP_SELF]&quot;> Your name: <input type=&quot;text&quot; name=&quot;my_name&quot;> <br/> <input type=&quot;submit&quot; value=&quot;Say Hello&quot;> </form> _HTML_; } ?>
  • 81. 38. An HTML Form That Calls Itself <html> <head> <title>An HTML Form that Calls Itself</title> </head><body> <div> <?php if ( ! empty( $_POST['guess'] ) ) { print &quot;last guess: &quot;.$_POST['guess']; }?> <form method=&quot;post&quot; action=&quot;<?php print $_SERVER['PHP_SELF']?>&quot;> <p> Type your guess here: <input type=&quot;text&quot; name=&quot;guess&quot; /> </p></form></div> </body> </html>
  • 82. 39. Form submitting <?php if (isset($_POST['submit'])){ echo &quot;Hi &quot;.$_POST['name'].&quot;!<br />&quot;; echo &quot;The address &quot;.$_POST['email'].&quot; will soon be a spam-magnet!<br />&quot;; } ?> <form action=&quot;index.php&quot; method=&quot;post&quot;> Name:<input type=&quot;text&quot; name=&quot;name&quot; size=&quot;20&quot; maxlength=&quot;40&quot; value=&quot;&quot; /> Email Address: <input type=&quot;text&quot; name=&quot;email&quot; size=&quot;20&quot; maxlength=&quot;40&quot; value=&quot;&quot; /> <input type=&quot;submit&quot; name = &quot;submit&quot; value=&quot;Go!&quot; /> </form>
  • 83. 40. One-script form processing //File: index.php <html> <head> <title></title> </head> <body> <? $form = &quot;<form action=&quot;index.php&quot; method=&quot;post&quot;> <input type=&quot;hidden&quot; name=&quot;seenform&quot; value=&quot;y&quot;> <b>Give us some information!</b><br> Your Name:<br> <input type=&quot;text&quot; name=&quot;name&quot; size=&quot;20&quot; maxlength=&quot;20&quot; value=&quot;&quot;><br>
  • 84. 40. One-script form processing Your Email:<br> <input type=&quot;text&quot; name=&quot;email&quot; size=&quot;20&quot; maxlength=&quot;40&quot; value=&quot;&quot;><br> <input type=&quot;submit&quot; value=&quot;subscribe!&quot;> </form>&quot;; if ($seenform != &quot;y&quot;): print &quot;$form&quot;; else : print &quot;Hi, $name!. Your email address is $email&quot;; endif; ?> </body> </html>
  • 85. 41. Preventing Multiple Submissions on the Client Side <html> <script language=&quot;javascript&quot; type=&quot;text/javascript&quot;> function checkandsubmit() { document.test.submitbut.disabled = true; document.test.submit(); } </script> <body> <form action=&quot;index.php&quot; method=&quot;post&quot; name=&quot;test&quot; onsubmit=&quot;return checkandsubmit ()&quot;> <input type=&quot;hidden&quot; name=&quot;submitted&quot; value=&quot;yes&quot; /> Your Name: <input type=&quot;text&quot; name=&quot;yourname&quot; maxlength=&quot;150&quot; />
  • 86. 41. Preventing Multiple Submissions on the Client Side <input type=&quot;submit&quot; value=&quot;Submit&quot; id=&quot;submitbut&quot; name&quot;submitbut&quot; /></form> </body> </html> <?php if ($file = fopen ( &quot;test.txt&quot;, &quot;w+&quot; )) { fwrite ( $file, &quot;Processing&quot; ); } else { echo &quot;Error opening file.&quot;; } echo $_POST ['yourname']; ?>
  • 87. 42. Preventing Multiple Submissions on the Server Side <html> <body> <form action=&quot;index.php&quot; method=&quot;post&quot;> <input type=&quot;hidden&quot; name=&quot;submitted&quot; value=&quot;yes&quot; /> Your Name: <input type=&quot;text&quot; name=&quot;yourname&quot; maxlength=&quot;150&quot; /><br /> <input type=&quot;submit&quot; value=&quot;Submit&quot; style=&quot;margin-top: 10px;&quot; /></form> </body> </html> <?php session_start (); if (! isset ( $_SESSION ['processing'] )) { $_SESSION ['processing'] = false;}
  • 88. 42. Preventing Multiple Submissions on the Server Side if ($_SESSION ['processing'] == false) { $_SESSION ['processing'] = true; //validation if ($file = fopen ( &quot;test.txt&quot;, &quot;w+&quot; )) { fwrite ( $file, &quot;Processing&quot; ); } else { echo &quot;Error opening file.&quot;; } echo $_POST ['yourname']; unset ( $_SESSION ['processing'] ); } ?>
  • 89. 43. Authenticate user: Database based <?php function authenticate_user() { header('WWW-Authenticate: Basic realm=&quot;Secret Stash&quot;'); header(&quot;HTTP/1.0 401 Unauthorized&quot;); exit; } if (! isset($_SERVER['PHP_AUTH_USER'])) { authenticate_user(); } else { mysql_pconnect(&quot;localhost&quot;,&quot;authenticator&quot;,&quot;secret&quot;) or die(&quot;Can't connect to database server!&quot;); mysql_select_db(&quot;java2s&quot;) or die(&quot;Can't select authentication database!&quot;);
  • 90. 43. Authenticate user: Database based $query = &quot;SELECT username, pswd FROM user WHERE username='$_SERVER[PHP_AUTH_USER]' AND pswd=MD5('$_SERVER[PHP_AUTH_PW]')&quot;; $result = mysql_query($query); // If nothing was found, reprompt the user for the login information. if (mysql_num_rows($result) == 0) { authenticate_user(); } } ?>
  • 91. 44. Prompt Browser password dialog <?php if (($_SERVER['PHP_AUTH_USER'] != 'specialuser') || ($_SERVER['PHP_AUTH_PW'] != 'secretpassword')) { header('WWW-Authenticate: Basic Realm=&quot;Secret Stash&quot;'); header('HTTP/1.0 401 Unauthorized'); print('You must provide the proper credentials!'); exit; } ?>
  • 92. 45. Open browser password dialog and authenticate user based on database <?php function authenticate_user() { header('WWW-Authenticate: Basic realm=&quot;Secret Stash&quot;'); header(&quot;HTTP/1.0 401 Unauthorized&quot;); exit; } if(! isset($_SERVER['PHP_AUTH_USER'])) { authenticate_user(); }
  • 93. 45. Open browser password dialog and authenticate user based on database else { mysql_connect(&quot;localhost&quot;,&quot;authenticator&quot;,&quot;secret&quot;) or die(&quot;Can't connect to database server!&quot;); mysql_select_db(&quot;gilmorebook&quot;) or die(&quot;Can't select authentication database!&quot;); $query = &quot;SELECT username, pswd FROM user WHERE username='$_SERVER[PHP_AUTH_USER]' AND pswd=MD5('$_SERVER[PHP_AUTH_PW]') AND ipAddress='$_SERVER[REMOTE_ADDR]'&quot;; $result = mysql_query($query); if (mysql_num_rows($result) == 0) authenticate_user(); mysql_close(); }?>
  • 94. 46. Login ID Options <HTML> <HEAD> <TITLE>Displaying Login ID Options</TITLE> </HEAD> <BODY> <Hl><CENTER>Generating Login IDs for Dave</CENTER></Hl> <?php $loginfo = array ( FirstName => &quot;Joe&quot;, LastName => &quot;Howrod&quot;, Sex => &quot;Male&quot;, Location => &quot;Arizona&quot;, Age => &quot;24&quot;, MusicChoice => &quot;Pop&quot;, Hobby => &quot;Dance&quot;, Occupation => &quot;Author&quot;); echo &quot;<H3>The information entered by JOe:<BR></H3>&quot;; foreach ($loginfo as $key => $val) { echo &quot;<B>$key</B> => $val<BR>&quot;; }
  • 95. 46. Login ID Options echo &quot;<BR> &quot;; echo &quot;<H3>The login options are:<BR></H3>&quot;; $loginone = array_slice ($loginfo, 0, 2); $logintwo = array_slice ($loginfo, 3, 2); $loginthree = array_slice ($loginfo, 5, 2); $loginfour = array_slice ($loginfo, 6, 2); $loginfive = array_merge ($loginfour, &quot;2001&quot;); echo &quot;The first login option:<BR>&quot;; foreach ($loginone as $optionone) { echo &quot;<B>$optionone</B>&quot;; } echo &quot;<BR>&quot;; echo &quot;The second login option:<BR>&quot;; foreach ($loginone as $optiontwo) { echo &quot;<B>$optiontwo</B>&quot;; } echo &quot;<BR>&quot;;
  • 96. 46. Login ID Optionsn echo &quot;The third login option:<BR>&quot;; foreach ($loginthree as $optionthree) { echo &quot;<B>$optionthree</B>&quot;; } echo &quot;<BR>&quot;; echo &quot;The fourth login option:<BR>&quot;; foreach ($loginfour as $optionfour) { echo &quot;<B>$optionfour</B>&quot;; } echo &quot;<BR>&quot;; echo &quot;The fifth login option:<BR>&quot;; foreach ($loginfive as $optionfive) { echo &quot;<B>$optionfive</B>&quot;; } echo &quot;<BR>&quot;; ?> </BODY> </HTML>
  • 97. 47. Get uniqid <? $id = uniqid(&quot;phpuser&quot;); echo &quot;$id&quot;; ?>
  • 98. 48. Use md5 function to encrypt text <?php $val = &quot;secret&quot;; echo &quot;Pre-hash string: $val <br />&quot;; $hash_val = md5 ($val); echo &quot;Hashed outcome: $hash_val&quot;; ?>
  • 99. 49. md5 and uniqid <? $id = md5(uniqid(rand())); echo &quot;$id&quot;; ?>
  • 100. 50. A user registration process create table user_info ( user_id char(18), fname char(15), email char(35)); //File: index.php <? $form = &quot; <form action=&quot;index.php&quot; method=&quot;post&quot;> <input type=&quot;hidden&quot; name=&quot;seenform&quot; value=&quot;y&quot;> Your first name?:<br> <input type=&quot;text&quot; name=&quot;fname&quot; value=&quot;&quot;><br> Your email?:<br> <input type=&quot;text&quot; name=&quot;email&quot; value=&quot;&quot;><br> <input type=&quot;submit&quot; value=&quot;Register!&quot;> </form> &quot;;
  • 101. 50. A user registration process if ((! isset ($seenform)) && (! isset ($userid))) : print $form; elseif (isset ($seenform) && (! isset ($userid))) : $uniq_id = uniqid(rand()); @mysql_pconnect(&quot;localhost&quot;, &quot;root&quot;, &quot;&quot;) or die(&quot;Could not connect to MySQL server!&quot;); @mysql_select_db(&quot;user&quot;) or die(&quot;Could not select user database!&quot;); $query = &quot;INSERT INTO user_info VALUES('$uniq_id', '$fname', '$email')&quot;; $result = mysql_query($query) or die(&quot;Could not insert user information!&quot;); setcookie (&quot;userid&quot;, $uniq_id, time()+2592000); print &quot;Congratulations $fname! You are now registered!.&quot;;
  • 102. 50. A user registration process elseif (isset($userid)) : @mysql_pconnect(&quot;localhost&quot;, &quot;root&quot;, &quot;&quot;) or die(&quot;Could not connect to MySQL server!&quot;); @mysql_select_db(&quot;user&quot;) or die(&quot;Could not select user database!&quot;); $query = &quot;SELECT * FROM user_info WHERE user_id = '$userid'&quot;; $result = mysql_query($query) or die(&quot;Could not extract user information!&quot;); $row = mysql_fetch_array($result); print &quot;Hi &quot;.$row[&quot;fname&quot;].&quot;,<br>&quot;; print &quot;Your email address is &quot;.$row[&quot;email&quot;]; endif; ?>
  • 103. THANK YOU