1 / 19

Overview of Server-side Scripting

Overview of Server-side Scripting. http://www.flickr.com/photos/torkildr/3462607995/. Welcome to server-side scripting. Special commands mixed into HTML The server executes these commands when the page is loaded. There are many server-side options. PHP – Personal Home Pages

bgaston
Download Presentation

Overview of Server-side Scripting

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Overview of Server-side Scripting http://www.flickr.com/photos/torkildr/3462607995/

  2. Welcome to server-side scripting • Special commands mixed into HTML • The server executes these commands when the page is loaded.

  3. There are many server-side options. • PHP – Personal Home Pages • Very easy to get started and what we'll use • See "Setting up PHP" doc for info on how to start • Other available options • JSP – Java Server Pages • ASP.NET – Microsoft's server-side platform • Ruby – very popular, fairly easy to get started • … and more options every year

  4. The PHP tag Hi, my name is <b><?php echo "Bob" ?></b>

  5. Variables and strings <?php $x = 40; echo 'The meaning of life is '.($x+2).'<br>'; echo "But it is not $x"; ?> Variables start with dollar signs. Variables in double-quote strings are evaluated (but not in single-quote strings). Period concatenates.

  6. Conditionals and comparisons <?php $i = 40; if ($i.'' == '40' && $i.'' !== 40) echo 'looks nice'; else echo 'kinda weird'; ?> Conditionals, conversions, and comparisons are like JavaScript.

  7. Loops <?php for ($i = 0; $i < 100; $i++) { if ($i % 3 == 0) echo "$i<BR>"; } ?> Loops are also like JavaScript and other C-like languages.

  8. Arrays <?php $ranking = array( 1 => "OSU", 2 => "UO" ); echo 'The top school in Oregon is '.$ranking[1] ?> You can also just define $arr = array('value0', 'value1') to create an array indexed from 0.

  9. Associative arrays <?php $students = array( "bob" => "smith", "ricky" => "roller" ); echo 'The last name of ricky is '.$students["ricky"] ?> Keys can be strings, ints, whatever. But string, float, and boolean keys are auto-converted to ints whenever possible. Internally, actually, all arrays are associative.

  10. Iterating over an array <?php $ranking = array("zero", "one", "two"); for ($i = 0; $i < count($ranking); $i++) echo $i.':'.$ranking[$i]."<BR>"; ?> The count() function returns the number of elements. Note that this includes element at position 0!!! Another handy function is sort(), for sorting arrays.

  11. Iterating over an associative array <?php $ranking = array("zero", "one", "two"); foreach (array_keys($ranking) as $i) echo $i.':'.$ranking[$i]."<BR>"; ?> Internally, all arrays are associative. Here is how you can loop over the keys of an associative array. FYI, PHP also supports true objects, but we don't use 'em a lot.

  12. Iterating over an array of arrays <?php $mydata = array( array("label" => "Google", "url" => "http://www.google.com"), array("label" => "Oregon State", "url" => "http://www.oregonstate.edu"), array("label" => "New York Times", "url" => "http://www.nytimes.com"), array("label" => "Reddit", "url" => "http://www.reddit.com") ); // print_r($mydata); print("<ul>"); for ($i = 0; $i < count($mydata); $i++) { $item = $mydata[$i]; $label = $item["label"]; $url = $item["url"]; print("<li><a href='" . htmlspecialchars($url) . "'>" . htmlspecialchars($label) . "</a>"); } print("</ul>"); ?>

  13. htmlspecialchars <?php echo '<input value="'.htmlspecialchars('<"&').'">'; ?> This function escapes characters that have special meaning in HTML, such as those shown above. This helps to protect security and proper behavior.

  14. Checking if an array value is set <?php $test = array("OSU" => "Beavs", "UO" => "Ducks"); echo "UW exists? "; echo (array_key_exists("UW", $test) ? "yes" : "no"); ?> Another handy function for debugging is var_dump(), which prints an array's contents.

  15. Functions <?php function bestFriends($idx) { $tmp = array('Karen', 'Kelly', 'Brad'); return $tmp[$idx]; } echo "My best friend is ".bestFriends(0); ?> You can assign functions like pointers to variables. Like this… $fn = bestFriends; echo "My best friend is ".$fn(0);

  16. Accepting values from the browser <form> <input name="nm"><input type="submit"> </form> <?php if (array_key_exists("nm", $_REQUEST)) echo 'submitted:'.htmlspecialchars($_REQUEST["nm"]); ?> The $_GET and $_POST arrays contain values sent by GET or POST. The $_REQUEST array merges these into one.

  17. Very handy for structuring your site <?php include("_header.php");?> This will cause the entire contents of the specified file ("_header.php") to get inserted into your php at this point. For example, you could include a stylesheet.

  18. Let's walk through a site skeleton Browse at http://web.engr.oregonstate.edu/~scaffidc/courses/cs290/skeleton1 Download from http://web.engr.oregonstate.edu/~scaffidc/courses/cs290/lectures/site_skeleton1.zip Good about this skeleton: Shows how to do very basic PHP tasks and a simple site structure Bad about this skeleton: No database integration; same page title on every page

  19. Activity • Create a file on the server called "_header.php" that outputs some HTML, e.g., <style>body { font-family: sans-serif }</style> • Create a second file that does a PHP include <?php include("_header.php") ?>

More Related