Documente Academic
Documente Profesional
Documente Cultură
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.
1. Right click on the PHP zip archive (retrieved in step 1 of this guide) and click "Extract All..." and then "Next".
Instructions
2. Change C:\Downloads\php-4.3.3-Win32 to C:\ and click "Next", followed by "Finish". There should now exist a folder C:\php-4.3.3Win32 .
3. Rename the this folder from C:\php-4.3.3Win32 to C:\php.
4. Open the httpd.conf (C:\Program Files\Apache Group\Apache\conf\httpd.conf) file and find the line:
<IfModule mod_alias.c>
5. Immediately under this line insert the following lines: ScriptAlias /php/ "c:/php/" AddType application/x-httpd-php .php .phtml Action application/x-httpd-php "/php/php.exe"
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 Files\Apache Group\Apache\htdocs 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
<head> <title>Example #3 TDavid's Very First PHP Script ever!</title> </head> <? print(Date("m/j/y")); ?>
<body> </body> </html>
<html> <head> 4. Change background based day of the <title>Background Colors change color based on the day on of the week</title> </head> week using if else elseif statements <? $today = date("l"); print("$today"); if($today == "Sunday") { $bgcolor = "#FEF0C5"; } elseif($today == "Monday") { $bgcolor = "#FFFFFF"; } else { // Since it is not any of the days above it must be Saturday $bgcolor = "#F0F4F1"; }
print("<body bgcolor=\"$bgcolor\">\n"); ?> <br>This just changes the color of the screen based on the day of the week </body> </html> elseif($today == "Tuesday") { $bgcolor = "#FBFFC4"; } elseif($today == "Wednesday") { $bgcolor = "#FFE0DD"; } elseif($today == "Thursday") { $bgcolor = "#E6EDFF"; } elseif($today == "Friday") { $bgcolor = "#E9FFE6"; }
6. Add date time stamp "page last updated on..." 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("example7.php3"); print("Last Modified "); print(date("m/j/y h:i", $last_modified)); ?> </body> </html>
11. PHP simple slot machine <? function slotnumber() multiple rand number generation {
srand(time()); for ($i=0; $i < 3; $i++) { $random = (rand()%3); $slot[] = $random; } print("<td width=\"33%\"><center>$slot[0]</td>"); print("<td width=\"33%\"><center>$slot[1]</td>"); print("<td width=\"33%\"><center>$slot[2]</td>"); if($slot[0] == $slot[1] && $slot[0] == $slot[2]) { print("</td></tr>Winner! -- Hit refresh on your browser to play again"); exit; } } ?>
<div align="center"><center>
<table border="1" width="50%"> <tr> <? slotnumber(); ?> </td> </tr> <tr> <td width="100%" colspan="3" bgcolor="#008080"><form method="POST" action="example13.php3"><div align="center"><center><p><input type="submit" value="Spin!"></p> </center></div> </form> </td> </tr> </table> </center></div>
$url_title = array("Link Organizer", "TD Keno", "eziBill *Free Promotion!", "TD Forum", "TD Pic of Day PHP", "Guts or Glory Poker PHP"); $url_desc = array("- A comprehensive link list organizer",
"- Offer your site visitors an engaging Keno game without the monetary risk", "- Sell access to and protect your membership area using iBill and our eziBill script", "- An unthreaded messageboard script to exchange ideas with your site visitors", "- Run your own picture of the day script from any site anywhere with this handy script", "- A casino-style card game written entirely in PHP"); srand(time()); $sizeof = count($random_url); $random = (rand()%$sizeof); print("<center><a href=\"$random_url[$random]\">$url_title[$random]</a> $url_desc[$random]</center>"); ?>
15. Searching through a directory and picking out files using a regular expression
<? $diary_directory = opendir("."); while($filename = readdir($diary_directory)) { $filesplit = explode(".", $filename); $check_filename = $filesplit[0]; if(ereg("[0-9]{6}", $check_filename)) { $check_filename .= ".$filesplit[1]"; $valid_filename[] = $check_filename; } } closedir($diary_directory); for($index = 0; $index < count($valid_filename); $index++) { print("$valid_filename[$index]<br>"); } ?>
if($cards[$random] == "") { $i--; } else { $deck[] = $cards[$random]; $cards[$random] = ""; } } srand(time()); $starting_point = (rand()%51); print("Starting point for cut cards is: $starting_point<p>"); // display shuffled cards (EXAMPLE ONLY) for ($index = 0; $index < 52; $index++) { if ($starting_point == 52) { $starting_point = 0; } print("Uncut Point: <strong>$deck[$index]</strong> "); print("Starting Point: <strong>$deck[$starting_point]</strong><br>"); $starting_point++; } ?>
17. Admin interface for adding and deleting users using mySQL
<? $db = "DATABASE NAME"; $admin = "MYSQL USER NAME"; $adpass = "MYSQL PASSWORD"; $mysql_link = mysql_connect("localhost", $admin, $adpass); mysql_select_db($db, $mysql_link); ?> <HTML> <BODY> <script language="php"> if($react == "delete_user") { if($user) { $query = "DELETE from login WHERE user='$user' "; $result = mysql_query($query, $mysql_link); if(mysql_num_rows($result)) { print("<strong>$user</strong> successfully deleted<p>"); } }
else { print("<strong>no users are available to delete yet, sorry.</strong><p>"); } } elseif ($react == "add_user") { if(($user) and ($pass)) { $query = "INSERT into login VALUES ( "; $query .= "0, SYSDATE(), '$username', '$password' )"; mysql_query($query, $mysql_link); } else { print("<strong>either your user or password field was left blank</strong><p>"); } } else { print("<center>Administration Area - Choose your option</center>"); } </script> <form method="POST" action="admin.php3"><div align="center"><center><p>Delete Users <input type="hidden" name="react" value="delete_user"> <select name="user" size="1">
<script language="php"> $query = "SELECT user FROM login ORDER BY user"; $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("<option value=\"$row[0]\">$row[0]</option>"); } } else { print("<option value=\"\">No users created yet</option>"); } </script> </select><input type="submit" value="Submit"></p></center></div> </form> <form method="POST" action="admin.php3"> <input type="hidden" name="react" value="add_user"> <div align="center"><center><p>ADD A New User<br> User: <input type="text" name="user" size="20"><br> Pass: <input type="text" name="pass" size="20"><br> <input type="submit" value="Submit"></p></center></div> </form> </BODY> </HTML>
<script language="php"> $db = "DATABASE NAME"; $admin = "MYSQL USER NAME"; $adpass = "MYSQL PASSWORD"; $mysql_link = mysql_connect("localhost", $admin, $adpass); mysql_select_db($db, $mysql_link); $query = "SELECT * FROM avg_tally ORDER BY average DESC"; $result = mysql_query($query, $mysql_link); if(mysql_num_rows($result)) { $rank = 1; while($row = mysql_fetch_row($result)) { print("</tr><tr>"); if($color == "#D8DBFE") { $color = "#A6ACFD"; } else { $color = "#D8DBFE"; } print("<td width=\"6%\" bgcolor=\"$color\"><center><small>"); print("<font face=\"Verdana\">$rank</font></small></center></td>"); print("<td width=\"7%\" bgcolor=\"$color\"><center><small>"); print("<font face=\"Verdana\"><strong>$row[1]</strong></font></small></center></td>");
print("<td width=\"11%\" bgcolor=\"$color\"><center><small>"); $url = $row[2] . ".php3"; if(!file_exists($url)) { $url = $row[2] . ".html"; } print("<font face=\"Verdana\"><a href=\"$url\">$row[2]</a></font></small></center></td>"); print("<td width=\"76%\" bgcolor=\"$color\"><left><small>"); print("<font face=\"Verdana\">$row[3]</font></small></left></td>"); $rank++; } } </script> </table> </BODY> </HTML>
mysql_query($create_query, $mysql_link); print("Table Creation for <b>tds_counter</b> successful!<p>"); $insert = "INSERT into tds_counter VALUES ( 0, '/php_diary/021901.php3', 0, SYSDATE() )"; mysql_query($insert, $mysql_link); print("Inserted new counter successfully for this page: http://www.php-scripts.com/php_diary/021901.php3<p>"); ?>
20. mySQL based counter script for multiple pagesAutomating manual tasks, date sorting, mktime()
<? $db = "DATABASE NAME"; $admin = "MYSQL USER NAME"; $adpass = "MYSQL PASSWORD"; $mysql_link = mysql_connect("localhost", $admin, $adpass); mysql_select_db($db, $mysql_link); $result = mysql_query("SELECT impressions from tds_counter where COUNT_ID='$cid'", $mysql_link); if(mysql_num_rows($result)) { mysql_query("UPDATE tds_counter set impressions=impressions+1 where COUNT_ID='$cid'", $mysql_link); $row = mysql_fetch_row($result); if($inv != 1) { print("$row[0]"); } }?>
<html> <h3>My first PHP Program</h3> <? echo "Hello world!"; ?> </html>
<HTML> <BODY> <FORM METHOD="POST" ACTION="GetFormValue.php"> <H2>Contact List</H2> <BR>Nickname: <BR><INPUT TYPE="TEXT" NAME="Nickname"> <BR> <BR>Full Name: <BR><INPUT TYPE="TEXT" NAME="Fullname">
?> <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"> <select name="id"> <option name="">Choose a employee:</option> <option name="1">1</option> <option name="2">2</option> <option name="3">3</option> </select> <input type="submit" name="submit" value="Submit" /> </form>
<?php $value = 'yes'; echo "<input type='checkbox' name='subscribe' value='yes'/> Subscribe?"; if (isset($_POST['subscribe'])) { if ($_POST['subscribe'] == $value) { $subscribed = true; } else { $subscribed = false;
print 'Invalid checkbox value submitted.'; } } else { $subscribed = false; } if ($subscribed) { print 'You are subscribed.'; } else { print 'You are not subscribed'; }
<input type="submit" value="Submit" id="submitbut" name"submitbut" /></form> </body> </html> <?php if ($file = fopen ( "test.txt", "w+" )) { fwrite ( $file, "Processing" ); } else { echo "Error opening file."; } echo $_POST ['yourname']; ?>
$result = mysql_query($query);
// If nothing was found, reprompt the user for the login information. if (mysql_num_rows($result) == 0) { authenticate_user(); } } ?>
<?php if (($_SERVER['PHP_AUTH_USER'] != 'specialuser') || ($_SERVER['PHP_AUTH_PW'] != 'secretpassword')) { header('WWW-Authenticate: Basic Realm="Secret Stash"'); 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="Secret Stash"'); header("HTTP/1.0 401 Unauthorized"); exit; } if(! isset($_SERVER['PHP_AUTH_USER'])) { authenticate_user(); }
45. Open browser password dialog and authenticate user based on database
else { mysql_connect("localhost","authenticator","secret") or die("Can't connect to database server!"); mysql_select_db("gilmorebook") or die("Can't select authentication database!"); $query = "SELECT username, pswd FROM user WHERE username='$_SERVER[PHP_AUTH_USER]' AND pswd=MD5('$_SERVER[PHP_AUTH_PW]') AND ipAddress='$_SERVER[REMOTE_ADDR]'"; $result = mysql_query($query); if (mysql_num_rows($result) == 0) authenticate_user(); mysql_close(); }?>
<?php $val = "secret"; echo "Pre-hash string: $val <br />"; $hash_val = md5 ($val); echo "Hashed outcome: $hash_val"; ?>
$row = mysql_fetch_array($result); print "Hi ".$row["fname"].",<br>"; print "Your email address is ".$row["email"]; endif; ?>
Thank You