- Forward


Server-Side PHP Programming
An Introduction with Examples


Prof. David Bernstein
James Madison University

Computer Science Department
bernstdh@jmu.edu

Print

PHP Programs and HTTP Servers
Back SMYC Forward
  • Overview:
    • Most HTTP servers can be configured to execute PHP programs
    • Most HTTP servers look for PHP code in files with a .php extension
  • The Process on the Server:
    • Some parts of the .php file (i.e., most HTML elements, CSS) are just inserted into the HTTP response
    • Some parts of the .php file (see below) are executed and the output is inserted into the HTTP response
  • Different Ways to Identify PHP Code:
    • Put it between a <?php and ?> (XML/XHTML style)
    • Put it in the body of a script element (HTML style)
    • Put it between a <? and ?>
    • Put it between a <% and %> (ASP style)
Inserting Output into the HTTP Response
Back SMYC Forward
  • Examples of the echo Statement:
    • echo "<p>Hello world!</p>"; echo "<p>Hello $name.</p>"; // $name is evaluated echo "<p>Hello find($name).</p>"; // find() is not evaluated but $name is evaluated echo $name; echo '<p>Hello $name.</p>'; // $name is not evaluated
  • Using print:
    • Has only one string parameter
    • Can be used as a statement like echo (i.e., without parentheses)
    • Can be used as a function (i.e., with parentheses)
  • Using printf():
    • printf(formatstring [,parameters]...)
An Example
Back SMYC Forward

How Code Fragments are Inserted into this Lecture's Slides

Loading the PHP Classes/Functions

<script language="PHP"> require_once('scripts/codeProcessor.phps'); </script>

Insertion of the Code Fragment

<pre> <script language="PHP"> $code = file('javaexamples/datahiding/Node.java'); processCode($code, ''); // Uses echo and/or printf </script> </pre>
Another Example
Back SMYC Forward

Inserting a "Directory" of (non-PHP) Files

<?php function createDirectoryListing() { $dir = opendir("."); while (($file = readdir($dir)) !== false) { if ((strpos($file, ".php")) !== false) { $text = ""; $code = file($file); for ($i=0; $i<count($code); $i++) { $text = $text . $code[$i]; } $start = strpos($text, "<title>"); $end = strpos($text, "</title>"); if (($start !== false) && ($end !== false)) { $length = $end - $start - 7; $title = substr($text, $start+7, $length); if ($length > 0) { $item[$file] = $title; } } } } asort($item, SORT_STRING); foreach ($item as $file => $title) { echo "<ul>"; echo "<a href=\"" . $file . "\">" . $title . "</a>"; echo "<br />"; echo "</ul>"; } } ?>
Getting Information about the Request
Back SMYC Forward
  • Overview of the $_SERVER Array:
    • Is an associative array that is populated with information about the request
    • Has superglobal scope
  • Important Elements of the $_SERVER Array:
    • ["REQUEST_METHOD"] - either "GET" or "POST"
    • ["SERVER_PROTOCOL"]
    • ["SERVER_NAME"]
    • ["SERVER_PORT"]
    • ["PATH_INFO"] and ["PATH_TRANSLATED"]
    • ["QUERY_STRING"]
    • ["CONTENT_TYPE"]
    • ["CONTENT_LENGTH"]
    • ["REMOTE_HOST"] and ["REMOTE_ADDR"]
Getting Detailed Information about a GET/POST Request
Back SMYC Forward
  • The $_GET Array:
    • Is an associative array that is populated with information from the query string
    • Has superglobal scope
  • The $_POST Array:
    • Is an associative array that is populated with form data from the data section (when the POST has a Content-Type of application/x-www-form-urlencoded or multipart/form-data)
    • Has superglobal scope
    • Some elements are themselves arrays (e.g., if a user checks more than one alternative in a select element or more than one checkbox)
Cookies
Back SMYC Forward
  • The $_COOKIE Array:
    • A superglobal associative array containing the HTTP cookies that have been set
  • The setcookie() Function:
    • Sets a cookie to be sent
    • Must be called before any output is generated (since cookies are part of the HTTP header)
    • Syntax: setcookie(name [, value [, expiration [, path [, domain [, secure [, httponly]]]]]] )
Sending Custom Header Information
Back SMYC Forward
  • The header() Function:
    • Sets a header to be sent (specified as a "name: value")
    • Must be called before any output is generated (since HTTP headers are at the beginning of the response)
  • A Warning:
    • Remember that some header information will be set automatically (e.g., Content-Length)
Some Useful Functions
Back SMYC Forward
  • Value Checking:
    • is_null() returns true if the parameter is NULL
    • isset() returns true if the parameter is set and not NULL
  • Type Checking/Conversion/Enforcement:
    • is_array(), is_bool(), is_float(), is_int(), is_object(), and is_string()
    • boolval(), floatval(), and intval()
  • Encoding Functions:
    • htmlentities()
    • urlencode()/urldecode() and rawurlencode()/rawurldecode()
  • Tokenizing/Parsing:
    • parse_url()
  • Validation/Sanitization:
    • filter_var()
There's Always More to Learn
Back -