Introduction to PHP Bill Boyd CEE Webmaster CE
Introduction to PHP Bill Boyd CEE Webmaster CE 169 A October 12, 2004
How Does a Web Server Work? A web server is a program that continually runs on a computer—a server—connected to the internet l It watches for requests for files from a client l Files in HTML and similar formats appear on a web browser as a web page l Files in other formats—e. g. , PDF—are handled by the web browser according to the client machine configuration l
Content: Static vs. Dynamically Generated l Static HTML file: delivered by the server with no modifications 1. 2. l File requested by client File delivered by server Dynamically generated HTML file: the server intervenes 1. 2. 3. “File” requested by client HTML output created/modified by the server “File” delivered by server
Methods of Creating Dynamic Content l CGI – Common Gateway Interface – External program (Perl, C, shell script) is run – Only programs in designated directories may run – Output is directed by the web server l Web Server Extensions – ASP – PHP – Others: Cold. Fusion, Front. Page extensions – Programs may be run from any location
ASP (Active Server Pages) l Limited to MS Windows servers running IIS l Visual Basic script l Easy interfacing with MS formats such as Word documents, Excel and Access files l Easiest route in some cases l Not free
PHP (PHP Hypertext Preprocessor) l Open source, runs on most operating systems, extensible via Pear modules l Designed to be fast and easy to use l C/C++, Perl, Bourne shell-like syntax l Programs often run the first time without errors! l Free, very popular l www. php. net
PHP Example 1 l Hello, World <html> <body> <h 3>PHP Example: Hello</h 3> <? php print “Hello, World! ”; ? > </body> </html>
PHP Example 2 l Date <? php print "The time is "; print date('H: i a'); print " "; print "The date is "; print date('M d, Y'); ? >
PHP: Form Processing l Two parts: – Form action (in this case, a PHP script)
The Form l Simple Form <form method=“POST” action=“form_proc 1. php”> Please enter your name: <input name=“first. Name”> <input type=“submit”> </form>
Processing the Form 1 l The Form Action <? php print "Your name is $first. Name!"; ? >
Processing the Form 2 l Sending e-mail <? php print "Your name is $first. Name!"; $to = "bboyd@ce. berkeley. edu"; $subject = "A new name"; $the. Date = date('H: i a - d M, Y'); $body = "$first. Name visited the form at $the. Date. "; if (mail($to, $subject, $body, "From: you")) { print " A notification has been sent to the webmaster. "; } ? >
Database Access <? php $server = "localhost: /tmp/mysql-student. sql"; l$user = "ce 169"; $pass = "AH-fall 2004"; $db = "bill"; $connection = mysql_connect($server, $user, $pass); $query = "select first. Name, color from colors where first. Name='$first. Name'"; $result = mysql_db_query($db, $query, $connection); $row = mysql_fetch_array($result, MYSQL_ASSOC); if ($row[color]) { print "<body bgcolor='$row[color]'>"; } else { print "<body bgcolor='#dddddd'>"; } print "<h 3>PHP Example: Form Processing 3</h 3> Your name is $first. Name!"; ? > My. Sql Example
Other Examples l CEE faculty page http: //www. ce. berkeley. edu/faculty. php? name=Horvath l File Uploads l PHP Graphical Database Manager php. My. Admin l Seminar Database
Designing a Website for a Large Company l l l Need a directory hierarchy for storage and management of the content Need a hierarchy for site visitors (not necessarily the same as above) Need graphics that project the message or philosophy of the company to the target audience Navigation Maintenance Style Sheets
CEE Internal Site Hierarchy Home Undergrad Admissions Graduate Admissions Ugrad People Faculty Courses Research CEEC
CEE Site External Hierarchy Home Admissions Graduate Adm. Undergrad Adm. Graduate Faculty People Courses Staff Grad Students Research Resources CEEC
Navigation l Construct scenarios l How many clicks? l Do you want the user to take a certain path? l Multiple routes l External hierarchy needs to be apparent as the user navigates through the site
Maintenance l Use common elements whenever possible to avoid multiple changes l Templates l Style Sheets l Edit local copy, not “live” file
Cascading Style Sheets (CSS) l One set of style definitions determine the appearance of elements in a document or in a whole web site l Saves a lot of time!
CSS Example l Style Sheet BODY { font: 12 px verdana, arial, sans serif; text-decoration : none; color: #547 da 7; font-weight: normal; }
Link to Style Sheet l In the HTML <head> tag: <head> <title>My Styled Page</title> <link rel="stylesheet" href="style. css" type="text/css"> </head>
- Slides: 22