PHP Presentation
Upcoming SlideShare
Loading in...5
×
 

PHP Presentation

on

  • 12,640 views

 

Statistics

Views

Total Views
12,640
Views on SlideShare
12,606
Embed Views
34

Actions

Likes
10
Downloads
529
Comments
1

3 Embeds 34

http://nikhilvjain.wordpress.com 27
http://estructurasdatos2012.wikispaces.com 5
http://www.slideshare.net 2

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

PHP Presentation PHP Presentation Presentation Transcript

  • PHP SETUP AND SAMPLE PROGRAMS By, Nikhil v Jain
  • What's PHP? PHP: Hypertext Preprocessor is a widely used, general-purpose scripting language that was originally designed for web development to produce dynamic web pages. For this purpose, PHP code is embedded into the HTML source document and interpreted by a web server with a PHP processor module, which generates the web page document.
  • Downloading PHP To download PHP , go to http://www.php.net/downloads.php and choose the PHP ZIP package listed under the "Windows Binaries" heading. This will take you to a page listing various mirrors containing the file. You can choose any mirror listed on the page but you may get faster downloads from the more local file mirrors (the ones at the top of the list).
  • Installing PHP Installing PHP on Windows XP from the downloaded PHP zip file is just a matter of extracting the zip file to a directory and altering the Apache configuration file to use the PHP interpreter for PHP scripts.
  • Instructions 1. Right click on the PHP zip archive (retrieved in step 1 of this guide) and click " Extract All ..." and then " Next ". 2. Change C:Downloadsphp-4.3.3-Win32 to C: and click " Next ", followed by "Finish". There should now exist a folder C:php-4.3.3-Win32 . 3. Rename the this folder from C:php-4.3.3-Win32 to C:php.
  • 4. Open the httpd.conf (C:Program FilesApache GroupApacheconfhttpd.conf) file and find the line: <IfModule mod_alias.c> 5. Immediately under this line insert the following lines: ScriptAlias /php/ &quot;c:/php/&quot; AddType application/x-httpd-php .php .phtml Action application/x-httpd-php &quot;/php/php.exe&quot; 6. Save the file as httpd.conf.
  • 7. To test that PHP and Apache now work together, create a plain text file and insert the following: <?php phpinfo(); ?> 8. Save this file as test.php in the C:Program FilesApache GroupApachehtdocs folder. 9. Restart the Apache server (Start > Programs > Apache HTTP Server > Control Apache Server > Restart) so Apache will use the altered configuration file.
  • If you go to http://127.0.0.1/test.php in a web browser you should see a page like this: If “Virtual directory Spport” is marked as enabled, you have successfully installed MySQL and should now have a complete working Apache, MySQL, PHP environment.
  • SAMPLE PROGRAMS
  • <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> 1. Displaying the current date in an HTML page
  • 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>
  • 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>
  • 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;; } elseif($today == &quot;Monday&quot;) { $bgcolor = &quot;#FFFFFF&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> 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;; } elseif($today == &quot;Friday&quot;) { $bgcolor = &quot;#E9FFE6&quot;; }
  • 5. 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>
  • 6. 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>
  • 7. 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;); } ?>
  • 8. 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;); ?>
  • 9. 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;); ?>
  • 10. Generating a rand number from 0 to 9 <? srand(time()); $random = (rand()%9); print(&quot;Random number between 0 and 9 is: $random&quot;); ?>
  • 11. 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;); if($slot[0] == $slot[1] && $slot[0] == $slot[2]) { print(&quot;</td></tr>Winner! -- Hit refresh on your browser to play again&quot;); exit; } } ?>
  • <div align=&quot;center&quot;><center> <table border=&quot;1&quot; width=&quot;50%&quot;> <tr> <? slotnumber(); ?> </td> </tr> <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>
  • 12. Random text link advertising using predefined arrays <? $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;); $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;); 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;); ?>
  • 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;)); ?>
  • 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>
  • 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;); } ?>
  • 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;); 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;; } } 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++; } ?>
  • 17. Admin interface for adding and deleting users using mySQL <? $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); ?> <HTML> <BODY> <script language=&quot;php&quot;> if($react == &quot;delete_user&quot;) { if($user) { $query = &quot;DELETE from login WHERE user='$user' &quot;; $result = mysql_query($query, $mysql_link); if(mysql_num_rows($result)) { print(&quot;<strong>$user</strong> successfully deleted<p>&quot;); } }
  • else { print(&quot;<strong>no users are available to delete yet, sorry.</strong><p>&quot;); } } elseif ($react == &quot;add_user&quot;) { if(($user) and ($pass)) { $query = &quot;INSERT into login VALUES ( &quot;; $query .= &quot;0, SYSDATE(), '$username', '$password' )&quot;; mysql_query($query, $mysql_link); } else { print(&quot;<strong>either your user or password field was left blank</strong><p>&quot;); } } else { print(&quot;<center>Administration Area - Choose your option</center>&quot;); } </script> <form method=&quot;POST&quot; action=&quot;admin.php3&quot;><div align=&quot;center&quot;><center><p>Delete Users <input type=&quot;hidden&quot; name=&quot;react&quot; value=&quot;delete_user&quot;> <select name=&quot;user&quot; size=&quot;1&quot;>
  • <script language=&quot;php&quot;> $query = &quot;SELECT user FROM login ORDER BY user&quot;; $result = mysql_query($query, $mysql_link); if(mysql_num_rows($result)) { // we have at least one user, so show all users as options in select form while($row = mysql_fetch_row($result)) { print(&quot;<option value=&quot;$row[0]&quot;>$row[0]</option>&quot;); } } else { print(&quot;<option value=&quot;&quot;>No users created yet</option>&quot;); } </script> </select><input type=&quot;submit&quot; value=&quot;Submit&quot;></p></center></div> </form> <form method=&quot;POST&quot; action=&quot;admin.php3&quot;> <input type=&quot;hidden&quot; name=&quot;react&quot; value=&quot;add_user&quot;> <div align=&quot;center&quot;><center><p>ADD A New User<br> User: <input type=&quot;text&quot; name=&quot;user&quot; size=&quot;20&quot;><br> Pass: <input type=&quot;text&quot; name=&quot;pass&quot; size=&quot;20&quot;><br> <input type=&quot;submit&quot; value=&quot;Submit&quot;></p></center></div> </form> </BODY> </HTML>
  • 18. 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> <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); $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;; 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>
  • 19. 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;;
  • 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;); ?>
  • 20. MySQL based counter script for multiple pages <? $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;);    } } ?>
  • 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;);
  • 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; }
  • 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;; }
  • 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;); } }?>
  • 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;; }
  • 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;; }
  • 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;;} ?>
  • 22. program to print a message <html> <h3>My first PHP Program</h3> <? echo &quot;Hello world!&quot;; ?> </html>
  • 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]; ?>
  • 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'];
  • 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>
  • 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>
  • 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>
  • 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; ; } ?>
  • 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;>
  • 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;; ?>
  • 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); }
  • 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>
  • 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 } } ?>
  • 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;
  • 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'; }
  • 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>
  • 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>
  • 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;; } ?> -->
  • 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); } ?>
  • 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']; ?>
  • 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;; } ?>
  • 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>
  • 35. Forms and PHP //index.php <html> <head> <title></title> </head> <body> <? print &quot;Hi, $name!. Your email address is $email&quot;; ?> </body> </html>
  • 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 }
  • 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>
  • 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_; } ?>
  • 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>
  • 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>
  • 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>
  • 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>
  • 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; />
  • 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']; ?>
  • 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;}
  • 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'] ); } ?>
  • 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;);
  • 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(); } } ?>
  • 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; } ?>
  • 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(); }
  • 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(); }?>
  • 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;; }
  • 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;;
  • 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>
  • 47. Get uniqid <? $id = uniqid(&quot;phpuser&quot;); echo &quot;$id&quot;; ?>
  • 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;; ?>
  • 49. md5 and uniqid <? $id = md5(uniqid(rand())); echo &quot;$id&quot;; ?>
  • 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;;
  • 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;;
  • 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; ?>
  • T h a n k Y o u