SlideShare a Scribd company logo
1 of 103
PHP INSTALLATION & SAMPLES -BY H. ANKUSH. JAIN
WHAT IS PHP? ,[object Object],[object Object],[object Object]
INSTALLATION
Install PHP 5 on Windows 1- Download the latest Windows PHP Binaries (MSI file)
2- Right click on the file and click “Install“.
3- Click “Next ” once the Welcome page is displayed.
4- Select “I accept the license agreement” and click “Next “.
5- Change your PHP installation path OR just accept the default path - C:rogram FilesHPand click “Next
6- On the Web Server Setup screen, select the Apache 2.2.x Module and click “Next
7-On the Apache Configuration Directory screen, browse for the Apache configuration directory (conf) OR just enter C:rogram Filespache Software Foundationpache2.2onfand click “Next ” to proceed with the installation. select-apache-webserver-configuration-directory
8- For now, accept the default selection and click “Next”.
9-Click “Install ” to install PHP 5 on Windows.
10- Click “Finish ” once completed. PHP 5 is successfully installed.
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.
Test your PHP 5 Installation
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:rogram Filespache Software Foundationpache2.2tdocs .
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.
Congratulations! You have successfully installed and test PHP 5 on Windows!
PHP SAMPLES
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>
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;; }
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;;}
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=amp;quot;$bgcoloramp;quot;>&quot;); ?> <br>This just changes the color of the screen based on the day of the week </body> </html>
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>
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>
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;); } ?>
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;); ?>
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;); ?>
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;); ?>
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=amp;quot;33%amp;quot;><center>$slot[0]</td>&quot;); print(&quot;<td width=amp;quot;33%amp;quot;><center>$slot[1]</td>&quot;); print(&quot;<td width=amp;quot;33%amp;quot;><center>$slot[2]</td>&quot;); </tr>
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>
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;);
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;);
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=amp;quot;$random_url[$random]amp;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;);
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;; } }
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++; } ?>
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>
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);
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=amp;quot;6%amp;quot; bgcolor=amp;quot;$coloramp;quot;><center><small>&quot;); print(&quot;<font face=amp;quot;Verdanaamp;quot;>$rank</font></small></center></td>&quot;); print(&quot;<td width=amp;quot;7%amp;quot; bgcolor=amp;quot;$coloramp;quot;><center><small>&quot;); print(&quot;<font face=amp;quot;Verdanaamp;quot;><strong>$row[1]</strong></font></small></center></td>&quot;); print(&quot;<td width=amp;quot;11%amp;quot; bgcolor=amp;quot;$coloramp;quot;><center><small>&quot;); $url = $row[2] . &quot;.php3&quot;;
Reader rated most useful diary entries in descending order if(!file_exists($url)) { $url = $row[2] . &quot;.html&quot;; } print(&quot;<font face=amp;quot;Verdanaamp;quot;><a href=amp;quot;$urlamp;quot;>$row[2]</a></font></small></center></td>&quot;); print(&quot;<td width=amp;quot;76%amp;quot; bgcolor=amp;quot;$coloramp;quot;><left><small>&quot;); print(&quot;<font face=amp;quot;Verdanaamp;quot;>$row[3]</font></small></left></td>&quot;); $rank++; } } </script> </table> </BODY> </HTML>
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;;
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;); ?>
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 <?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;; }
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=amp;quot;$size[0]amp;quot; height=amp;quot;$size[1]amp;quot; &quot;; $imgstr .= &quot;src=amp;quot;$targetamp;quot; alt=amp;quot;uploaded imageamp;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=amp;quot;index.phpamp;quot; method=amp;quot;postamp;quot;> <input type=amp;quot;hiddenamp;quot; name=amp;quot;seenformamp;quot; value=amp;quot;yamp;quot;> <b>Give us some information!</b><br> Your Name:<br> <input type=amp;quot;textamp;quot; name=amp;quot;nameamp;quot; size=amp;quot;20amp;quot; maxlength=amp;quot;20amp;quot; value=amp;quot;amp;quot;><br>
40. One-script form processing Your Email:<br> <input type=amp;quot;textamp;quot; name=amp;quot;emailamp;quot; size=amp;quot;20amp;quot; maxlength=amp;quot;40amp;quot; value=amp;quot;amp;quot;><br> <input type=amp;quot;submitamp;quot; value=amp;quot;subscribe!amp;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=amp;quot;index.phpamp;quot; method=amp;quot;postamp;quot;> <input type=amp;quot;hiddenamp;quot; name=amp;quot;seenformamp;quot; value=amp;quot;yamp;quot;> Your first name?:<br> <input type=amp;quot;textamp;quot; name=amp;quot;fnameamp;quot; value=amp;quot;amp;quot;><br> Your email?:<br> <input type=amp;quot;textamp;quot; name=amp;quot;emailamp;quot; value=amp;quot;amp;quot;><br> <input type=amp;quot;submitamp;quot; value=amp;quot;Register!amp;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; ?>
THANK YOU

More Related Content

What's hot (15)

Php tutorial
Php  tutorialPhp  tutorial
Php tutorial
 
Overview of PHP and MYSQL
Overview of PHP and MYSQLOverview of PHP and MYSQL
Overview of PHP and MYSQL
 
Introduction to python scrapping
Introduction to python scrappingIntroduction to python scrapping
Introduction to python scrapping
 
Phpwebdevelping
PhpwebdevelpingPhpwebdevelping
Phpwebdevelping
 
Beginners PHP Tutorial
Beginners PHP TutorialBeginners PHP Tutorial
Beginners PHP Tutorial
 
Php mysql
Php mysqlPhp mysql
Php mysql
 
Introduction to PHP
Introduction to PHPIntroduction to PHP
Introduction to PHP
 
Php tutorial
Php tutorialPhp tutorial
Php tutorial
 
PHP NOTES FOR BEGGINERS
PHP NOTES FOR BEGGINERSPHP NOTES FOR BEGGINERS
PHP NOTES FOR BEGGINERS
 
Short Intro to PHP and MySQL
Short Intro to PHP and MySQLShort Intro to PHP and MySQL
Short Intro to PHP and MySQL
 
Article 01 What Is Php
Article 01   What Is PhpArticle 01   What Is Php
Article 01 What Is Php
 
XML and Web Services with PHP5 and PEAR
XML and Web Services with PHP5 and PEARXML and Web Services with PHP5 and PEAR
XML and Web Services with PHP5 and PEAR
 
Component and Event-Driven Architectures in PHP
Component and Event-Driven Architectures in PHPComponent and Event-Driven Architectures in PHP
Component and Event-Driven Architectures in PHP
 
Php tutorial(w3schools)
Php tutorial(w3schools)Php tutorial(w3schools)
Php tutorial(w3schools)
 
Doing more with LESS
Doing more with LESSDoing more with LESS
Doing more with LESS
 

Similar to PHP Presentation

Similar to PHP Presentation (20)

Php 3 1
Php 3 1Php 3 1
Php 3 1
 
Php
PhpPhp
Php
 
Introduction To Lamp
Introduction To LampIntroduction To Lamp
Introduction To Lamp
 
SlideShare Instant
SlideShare InstantSlideShare Instant
SlideShare Instant
 
SlideShare Instant
SlideShare InstantSlideShare Instant
SlideShare Instant
 
Web Scraper Shibuya.pm tech talk #8
Web Scraper Shibuya.pm tech talk #8Web Scraper Shibuya.pm tech talk #8
Web Scraper Shibuya.pm tech talk #8
 
Dynamic Web Pages Ch 1 V1.0
Dynamic Web Pages Ch 1 V1.0Dynamic Web Pages Ch 1 V1.0
Dynamic Web Pages Ch 1 V1.0
 
Php mysql ppt
Php mysql pptPhp mysql ppt
Php mysql ppt
 
Justmeans power point
Justmeans power pointJustmeans power point
Justmeans power point
 
Justmeans power point
Justmeans power pointJustmeans power point
Justmeans power point
 
Justmeans power point
Justmeans power pointJustmeans power point
Justmeans power point
 
Justmeans power point
Justmeans power pointJustmeans power point
Justmeans power point
 
Justmeans power point
Justmeans power pointJustmeans power point
Justmeans power point
 
Justmeans power point
Justmeans power pointJustmeans power point
Justmeans power point
 
Php Crash Course
Php Crash CoursePhp Crash Course
Php Crash Course
 
Web development
Web developmentWeb development
Web development
 
WordPress Development Confoo 2010
WordPress Development Confoo 2010WordPress Development Confoo 2010
WordPress Development Confoo 2010
 
Lecture 6 - Comm Lab: Web @ ITP
Lecture 6 - Comm Lab: Web @ ITPLecture 6 - Comm Lab: Web @ ITP
Lecture 6 - Comm Lab: Web @ ITP
 
Flash Templates- Joomla!Days NL 2009 #jd09nl
Flash Templates- Joomla!Days NL 2009 #jd09nlFlash Templates- Joomla!Days NL 2009 #jd09nl
Flash Templates- Joomla!Days NL 2009 #jd09nl
 
Flash templates for Joomla!
Flash templates for Joomla!Flash templates for Joomla!
Flash templates for Joomla!
 

PHP Presentation

  • 1. PHP INSTALLATION & SAMPLES -BY H. ANKUSH. JAIN
  • 2.
  • 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:rogram FilesHPand 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:rogram Filespache Software Foundationpache2.2onfand 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:rogram Filespache Software Foundationpache2.2tdocs .
  • 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!
  • 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=amp;quot;$bgcoloramp;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=amp;quot;33%amp;quot;><center>$slot[0]</td>&quot;); print(&quot;<td width=amp;quot;33%amp;quot;><center>$slot[1]</td>&quot;); print(&quot;<td width=amp;quot;33%amp;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=amp;quot;$random_url[$random]amp;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=amp;quot;6%amp;quot; bgcolor=amp;quot;$coloramp;quot;><center><small>&quot;); print(&quot;<font face=amp;quot;Verdanaamp;quot;>$rank</font></small></center></td>&quot;); print(&quot;<td width=amp;quot;7%amp;quot; bgcolor=amp;quot;$coloramp;quot;><center><small>&quot;); print(&quot;<font face=amp;quot;Verdanaamp;quot;><strong>$row[1]</strong></font></small></center></td>&quot;); print(&quot;<td width=amp;quot;11%amp;quot; bgcolor=amp;quot;$coloramp;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=amp;quot;Verdanaamp;quot;><a href=amp;quot;$urlamp;quot;>$row[2]</a></font></small></center></td>&quot;); print(&quot;<td width=amp;quot;76%amp;quot; bgcolor=amp;quot;$coloramp;quot;><left><small>&quot;); print(&quot;<font face=amp;quot;Verdanaamp;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=amp;quot;$size[0]amp;quot; height=amp;quot;$size[1]amp;quot; &quot;; $imgstr .= &quot;src=amp;quot;$targetamp;quot; alt=amp;quot;uploaded imageamp;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=amp;quot;index.phpamp;quot; method=amp;quot;postamp;quot;> <input type=amp;quot;hiddenamp;quot; name=amp;quot;seenformamp;quot; value=amp;quot;yamp;quot;> <b>Give us some information!</b><br> Your Name:<br> <input type=amp;quot;textamp;quot; name=amp;quot;nameamp;quot; size=amp;quot;20amp;quot; maxlength=amp;quot;20amp;quot; value=amp;quot;amp;quot;><br>
  • 84. 40. One-script form processing Your Email:<br> <input type=amp;quot;textamp;quot; name=amp;quot;emailamp;quot; size=amp;quot;20amp;quot; maxlength=amp;quot;40amp;quot; value=amp;quot;amp;quot;><br> <input type=amp;quot;submitamp;quot; value=amp;quot;subscribe!amp;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=amp;quot;index.phpamp;quot; method=amp;quot;postamp;quot;> <input type=amp;quot;hiddenamp;quot; name=amp;quot;seenformamp;quot; value=amp;quot;yamp;quot;> Your first name?:<br> <input type=amp;quot;textamp;quot; name=amp;quot;fnameamp;quot; value=amp;quot;amp;quot;><br> Your email?:<br> <input type=amp;quot;textamp;quot; name=amp;quot;emailamp;quot; value=amp;quot;amp;quot;><br> <input type=amp;quot;submitamp;quot; value=amp;quot;Register!amp;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; ?>